Calculating memory usage with a set of processes on Linux

I'm having trouble calculating the actually used (resident) memory using a set of processes.

The problem that has just appeared is a user with a set of processes that exchange memory among themselves, so simply adding used memory ends in a pointless number (> 60 GB when the machine has only 48 GB of memory).

Is there an easy way to approach this problem?

I can probably do some approximation. Take (res mem - shared mem) * num proc + shared mem . But not all processes necessarily use the same memory block.

I am looking for a POSIX or Linux solution for this problem for C / C ++.

+4
source share
1 answer

You need to iterate through each process /proc/[pid]/smaps

It will contain an entry for each virtual machine mapping:

 7ffffffe7000-7ffffffff000 rw-p 00000000 00:00 0 [stack] Size: 100 kB Rss: 20 kB Pss: 20 kB Shared_Clean: 0 kB Shared_Dirty: 0 kB Private_Clean: 0 kB Private_Dirty: 20 kB Referenced: 20 kB Anonymous: 20 kB AnonHugePages: 0 kB Swap: 0 kB KernelPageSize: 4 kB MMUPageSize: 4 kB 

Private_Dirty memory is what interests you.

If there is a Pss field in your smaps file, then this is the amount of resident memory divided by the number of processes sharing physical memory.

Private_Clean can be copy to write mappings. They are usually used for shared libraries and are usually read / not written / executed.

+3
source

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


All Articles