lurscher
lurscher

Reputation: 26983

linux mechanism to measure process memory consumption f

what is the most efficient and accurate way/ API to measure heap memory consumption from the same running process programmatically? I want to estimate (as accurately as is reasonably possible) how much memory has been new or malloc since startup, minus the memory that has been free or delete

the scope of the question is linux and possibly other linux environments. The language is either C or C++


EDIT It is enough for my purposes to know the actual number (and size) of allocated/held blocks by any malloc implementation, i don't need the detail of actual malloc memory minus the the freed memory

Upvotes: 3

Views: 803

Answers (5)

Nemo
Nemo

Reputation: 71595

You can get quite a bit of information about your heap usage by linking against tcmalloc from Google Perftools. It is designed to locate memory leaks and determine "who the heck allocated all that RAM", but it provides enough instrumentation to answer most questions you might have about your heap.

Upvotes: 0

Karoly Horvath
Karoly Horvath

Reputation: 96306

You can use mallinfo for an estimation. I've just found this, not sure whether this is process or system.. :/

I'm not totally sure what you are asking, malloc minus freed is less than the actual usage because of the memory fragmentation, if you really need that number you have to use custom allocators (which are tiny wrappers around existing ones) everywhere in your code which is going to be painful.

Upvotes: 3

Scott C Wilson
Scott C Wilson

Reputation: 20036

Have you considered reading from /proc/u/stat? (where "u" is your pid)

Upvotes: 1

doron
doron

Reputation: 28932

Assuming new uses malloc look here.

For more details of a processes memory allocation, look at the /proc/[pid]/maps.

Also note that linux implements copy-on-write. This means that sometimes processes can share memory. This is especially true if the process was forked without calling exec afterwards.

Upvotes: 3

Scott C Wilson
Scott C Wilson

Reputation: 20036

If you use valgrind and run your program to completion, it gives you a report on memory usage.

http://valgrind.org/

Upvotes: 0

Related Questions