Reputation: 8447
I would like to get some information about the memory usage of my C++ program. The way I do this is by accessing /proc/self/stat
and printing the virtual and resident set size.
You can find an example here.
Is this a good way to go? How accurate is the information I am accessing*?
Could someone recommend a better way to measure memory usage programmatically?
*Asking, because I get unexpected, sudden jumps of mem usage. My expectation was that the information is perfectly accurate.
OS: I am running inside a docker container, which is based on RHEL.
Additional info: If I limit the memory usage of the container with docker run -m
, the printed memory is greater than the limit I set.
Upvotes: 0
Views: 272
Reputation: 238351
How to programatically get the memory usage of the current program?
There is no standard way to get memory usage of a program in C++.
The concept of "memory usage" itself is somewhat vague and can mean different things. Depending on what you mean, there may or might not be a system specific way to get the information.
The way I do this is by accessing
/proc/self/stat
Is this a good way to go?
I don't think so. As far as I know, /proc filesystem is not portable. Use the getrusage
function on POSIX systems.
Upvotes: 2