i need to have reliable measurement of allocated memory in a linux process. I’ve been looking into mallinfo but i’ve read that it is deprecated. What is the state of the art alternative for this sort of statistics?
basically i’m interested in at least two numbers:
-
number (and size) of allocated memory blocks/pages from the kernel by any
mallocor whatever implementation uses the C library of choice -
(optional but still important) number of allocated memory by userspace code (via
malloc,new, etc.) minus the deallocated memory by it (viafree,delete, etc.)
one possibility i have is to override malloc calls with LD_PRELOAD, but it might introduce an unwanted overhead at runtime, also it might not interact properly with other libraries i’m using that also rely on LD_PRELOAD aop-ness.
another possibility i’ve read is with rusage.
To be clear, this is NOT for debugging purposes, the memory usage is intrinsic feature of the application (similar to Mathematica or Matlab that display the amount of memory used, only that more precise at the block-level)
/proc/PID/statuscontains a few useful pieces of information (try runningcat /proc/$$/statusfor example).VmPeakis the largest your process’s virtual memory space ever became during its execution. This includes all pages mapped into your process, including executable pages, mmap’ed files, stack, and heap.VmSizeis the current size of your process’s virtual memory space.VmRSSis the Resident Set Size of your process; i.e., how much of it is taking up physical RAM right now. (A typical process will have lots of stuff mapped that it never uses, like most of the C library. If no processes need a page, eventually it will be evicted and become non-resident. RSS measures the pages that remain resident and are mapped into your process.)VmHWMis the High Water Mark ofVmRSS; i.e. the highest that number has been during the lifetime of the process.VmDatais the size of your process’s “data” segment; i.e., roughly its heap usage. Note that small blocks on which you have donemallocand thenfreewill still be in use from the kernel’s point of view; large blocks will actually be returned to the kernel when freed. (If memory serves, “large” means greater than 128k for current glibc.) This is probably the closest to what you are looking for.These measurements are probably better than trying to track malloc and free, since they indicate what is “really going on” from a system-wide point of view. Just because you have called
free()on some memory, that does not mean it has been returned to the system for other processes to use.