Reputation: 2605
I am writing small process monitor script in Perl by reading values from Proc file system. Right now I am able to fetch number of threads, process state, number of bytes read and write using /proc/[pid]/status and /proc/[pid]/io files. Now I want to calculate the memory usage of a process. After searching, I came to know memory usage will be present /proc/[pid]/statm. But I still can't figure out what are necessary fields needed from that file to calculate the memory usage. Can anyone help me on this? Thanks in advance.
Upvotes: 1
Views: 5241
Reputation: 63538
It is extremely difficult to know what the "memory usage" of a process is. VM size and RSS are known, measurable values.
But what you probably want is something else. In practice, "VM size" seems too high and RSS often seems too low.
The main problems are:
So you really need to think about what counts as "memory usage".
It seems to me that logically:
I don't know of any utility which does this. It seems nontrivial though, and involves (at least) reading /proc/pid/pagemap and possibly some other /proc interfaces, some of which are root-only.
Upvotes: 2
Reputation: 1
Another (less simple, but more precise) possibility would be to parse the /proc/123/maps
file, perhaps by using the pmap
utility. It gives you information about the "virtual memory" (i.e. the address space of the process).
Upvotes: 1
Reputation: 182609
You likely want resident
or size
. From kernel.org.
Upvotes: 2