calculating memory usage ?

Is there a way to calculate the memory consumed by my program in linux ? What method does codechef use to get the memory used, can I do that on my pc ? I read something about bsd-process-management, but could not get it. Can someone please help ?


if i were asked to implement it, i would do it with process accounting.

of course, to limit the program resource usage while running, you can use several methods, including shell ulimiting.

good luck :slight_smile:

Can you give an illustration, like a method as simple as “time ./yourfile” if it exists for memory calculation ?

1 Like

Try to read this thread -

I used this "pmap " it returned total memory about 12M whereas in codechef it returned the memory to be 2.6M . where am I wrong ?

I’m not sure if memory is max of or sum for all test cases. Maybe if you allocate memory dynamically you are not testing it with same test case as CodeChef is…

You can try some hacks to find out how many test cases CodeChef uses to find out more :wink:

At first you have to know what kind of memory you wanna report :

  • memory only used by this process ?
  • memory used by it + shared memory with other processes ?
  • virtual memory allocated ?
  • real memory actually used ?

The answers would be very different actually.

In my opinion :

  • valgrind adds too much overhead on execution time
  • solutions based on /proc are not convenient for fast-closing processes
  • only kernel is really aware of what memory is really used

I would do a C wrapper that :

  • setrlimits it to prevent it from using too much memory
  • adds an alarm-signal raise to prevent it from using too much time
  • forks the process you wanna profile
  • reports the time and memory taken at the end

I’ll provide some code if i find some time. :slight_smile:

I wish to know the memory that is shown in codechef! I dont know which kind, could u please tell ?

What i only know is that the memory shown beside each program is the max memory used out of all the test files your program has run and not the sum of all the memory . I have no idea of what method they use.
Happy Coding.

1 Like

@mjnovice Read the links given below having information how memory usage is calculated by SPOJ online judge.


have gone through all the above links, still no help! Could you illustrate some ?

oh thanks ! the third link says it all. :slight_smile:

@cyberax would be great, if you can illustrate one simple hello world program. I still can’r figure out. thanx a lot!