Computing process memory using the Proc file system

I am writing a small script process monitor in Perl, reading values โ€‹โ€‹from the Proc file system. Now I can get the number of threads, the state of the process, the number of bytes read and written using the / proc / [pid] / status and / proc / [pid] / io files. Now I want to calculate the memory usage in the process. After searching, I realized that memory usage would be present / proc / [pid] / statm . But I still canโ€™t understand what necessary fields are needed for this file to calculate memory usage. Can someone help me with this? Thanks in advance.

+1
source share
3 answers

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

  • program size
    • This is the whole program, including things that never changed in
  • resident size
    • Thing in RAM at the moment (this does not include pages uploaded)
+2
source

It is extremely difficult to understand what โ€œmemory usage" is in a process. VM size and RSS are known, measured values.

But what you probably want is something else. In practice, the "VM size" seems too high, and RSS often seems too low.

Main problems:

  • Several processes can share the same pages. You can add RSS of all running processes and get much more than the physical memory of your computer (this happens before the kernel data structures are calculated).
  • Personal pages belonging to the process can be replaced. Or they have not yet been initialized. Do they count?
  • How exactly do you think pages displaying memory? Dirty? Clean? MAP_SHARED or MAP_PRIVATE?

So, you really need to think about what is considered "memory usage".

It seems to me that logically:

  • Personal pages that are not shared with any other processes (NB: personal pages can be STILL copy to write!) Must be taken into account even if they have been replaced
  • Shared pages should be counted by the number of processes that they share, for example. a page shared by two processes takes up half
  • File-supported pages that are resident may be the same.
  • Pages with non-resident files can be ignored
  • If the same page is displayed more than once in the address space of the same process, it can be ignored for the 2nd and subsequent time. This means that if proc 1 displays page X twice, and in proc 2 displays page X displayed once, they both โ€œchargeโ€ half the page.

I do not know any utility that does this. This seems nontrivial, although it does include (at least) reading / proc / pid / pagemap, and possibly some other / proc interfaces, some of which are only root.

+2
source

Another (less simple but more accurate) possibility is to analyze the /proc/123/maps file, possibly using the pmap utility. It gives you information about "virtual memory" (ie the process address space).

+1
source

Source: https://habr.com/ru/post/1396235/


All Articles