Size of memory used by a program

Hello,

Here is a portion of my code:

a=(int *) malloc(dim*dim*sizeof(int));
b=(int *) malloc(dim*dim*sizeof(int));
c=(int *) malloc(dim*dim*sizeof(int));

for(i=0;i<dim;i++)
 for(j=0;j<dim;j++)
  c[i*dim+j]= rand();


for(i=0;i<dim;i++)
 for(j=0;j<dim;j++)
      b[i*dim+j]=rand();

for(i=0;i<dim;i++)
 for(j=0;j<dim;j++)
  a[i*dim+j]=0;


for(i=0;i<dim;i++)
 for(j=0;j<dim;j++)
  for(k=0;k<dim;k++)
  
{
 a[i*dim+j]+=b[i*dim+k]*c[k*dim+j];
}


How can i get the size of the data used by my program?

Thank you in advance.
Best Regards.

Assuming i , j , and k have been declared as type int; a portion of the data used by your program is:
3*dim*dim*sizeof(int) + a little bit of space for malloc() bookkeeping {for the malloc() 'ed arrays} + 3*sizeof(int *)) {for the pointers} + 3*sizeof(int) {for your loop control variables}.

1 Like

Running the size command on your executable lists the sizes of the text/data/bss segments...the last 2 cols gives the size of the total data segment consumed by your executable...

size a.out  
8283 + 308 + 84 = 8675

Not quite. It shows the size of the static data segment loaded when the process starts; it doesn't show the size of data allocated while the process is running (as in the 3 calls to malloc() in the given code snippet).

It is correct that the size command wont tell us anything about the runtime size of the heap allocated...but even accounting for malloc wont tell us how much data is allocated on the stack where function arguments and local variables are pushed...

Have you looked at Valgrind? When I took C we used
valgrind to see if we had any memory leaks.

Valgrind Home

pmap PID