I want to calculate my process memory (rss) at runtime in my application (c++/unix/multithreaded).Do we have any API to use for that.Please note that , I am aware of reading /proc/stat and getrusage() , but dont want to read/parse a system file from appication and getrusage() does not work in my linux distribution.
The whole intent was to check for memory leak caused by my application . I have even tried tracking memory by overloading new/malloc/calloc/realloc and get the memory allocation trakced, but even with thsese I am not able to track the whole memory allocated by process. It would be also helpfull if you can suggest the other probable areas where I should look for memory allocation/ memory leak other than the above stated APIs. I am aware of Valgrind/mpatrol type of memory monitor tools .. but unfortunately it does not work with my application.. Thanks in advance
You can read
/proc/${pid}/status
, it looks likeYou can parse the
VmRSS
value.You could use valgrind. By setting it in monitor mode and calling remote method
(gdb) monitor full
, it would give you the total, allocated, memory at run time. See this page for more information.First, this kind of information is operating system specific. It has to be done differently on Linux, on MacOSX, on FreeBSD...
On Linux, the blessed way, is as every one told you, to use the /proc file system, which is how all the system utilities (e.g.
top
orps
) are retrieving that information (perhaps by usinglibproc
which is just a wrapper around reads of/proc/
files).Could you explain why reading e.g.
/proc/self/statm
or/proc/self/stat
or/proc/self/status
or/proc/self/maps
is not possible for you?Remember that these
/proc/
files are pseudo-files, and no actual slow I/O operation to disk is involved in reading them. And you have to read them sequentially, seeking (orstat
-ing) them does not work.It seems to me that
is the fastest way to retrieve that information. Why can't you do that?