0

I've simple program e.g. in C++

#include <iostream>  
int main()  
{  
    int a = 1000;  
    std::cout << a << std::endl;  
    return 0;  
}  

and i'm trying to calculate memory usage with GNU time. But in "time" output (with my format, it doesn't matter) maximum size of process in memory is calculated with libc.so which has printf function (call to std::cout) and is equal to 3.5 Mb.
Is there a way to calculate process memory without loaded shared libs?

UPD I can't do it while process, which memory i wanna measure, runs, for several reasons. I'm asking if there's a way to do it with outer wrapper tool (like time is)

Ribtoks
  • 6,634
  • 1
  • 25
  • 37

1 Answers1

0

The pmap utility, or just reading the file /proc/123/maps for process of pid 123, and /proc/self/maps for your own process, gives you the detailed map of the memory. You could then ignore the lines concerning libraries you don't want to measure.

Try cat /proc/self/maps to understand more... (it displays the map of that cat command).

It is Linux specific (probably won't work on Solaris or FreeBSD).

There is also the more standard getrusage system call (which don't differentiate memory used by shared libraries from other memory use).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I know about `pmap`. In this case you want me to add memory sizes myself? parse lines and add only entries i need? Maybe there is a better way (like `time` util). I mean i won't have ability to measure memory while program runs (cause i don't know how many time program will run) – Ribtoks Nov 29 '11 at 19:02
  • Why do you want to remove the space needed for shared libraries? Why is this space special to you? – Basile Starynkevitch Nov 29 '11 at 19:08
  • I'm developing a contester system for ACM solutions. I need to know if user solution fits problem restrictions on memory and time. And if user solution is like this one in question, really it has small size (11Kb) but when `printf` is called, maximum size will be 3.5Mb – Ribtoks Nov 29 '11 at 19:11
  • Looks like GNU time just uses `getrusage` call or smth like that... Because cmdline args are the same as struct fields. – Ribtoks Nov 29 '11 at 19:46