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.
Markr source share