CrazyCoder
CrazyCoder

Reputation: 2605

Calculating memory of a Process using Proc file system

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

Answers (3)

MarkR
MarkR

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:

  • Multiple processes can share the same pages. You can add up the RSS of all running processes, and end up with much more than the physical memory of your machine (this is before kernel data structures are counted)
  • Private pages belonging to the process can be swapped out. Or they might not be initialised yet. Do they count?
  • How exactly do you count memory-mapped file pages? Dirty ones? Clean ones? MAP_SHARED or MAP_PRIVATE ones?

So you really need to think about what counts as "memory usage".

It seems to me that logically:

  • Private pages which are not shared with any other processes (NB: private pages can STILL be copy-on-write!) must count even if swapped out
  • Shared pages should count divided by the number of processes they're shared by e.g. a page shared by two processes counts half
  • File-backed pages which are resident can count in the same way
  • File-backed non-resident pages can be ignored
  • If the same page is mapped more than once into the address-space of the same process, it can be ignored the 2nd and subsequent time. This means that if proc 1 has page X mapped twice, and proc 2 has page X mapped once, they are both "charged" half a page.

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

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

cnicutar
cnicutar

Reputation: 182609

You likely want resident or size. From kernel.org.

  • size total program size
    • This is the whole program, including stuff never swapped in
  • resident resident set size
    • Stuff in RAM at the current moment (this does not include pages swapped out)

Upvotes: 2

Related Questions