4

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

The issue that just came up is a user with a set of processes that share memory between themselves, so a simple addition of used memory ends up with a nonsense number (>60gb when the machine only has 48gb memory).

Is there any simple 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 share the same memory block.

I'm looking for a POSIX or Linux solution to this problem for C/C++.

5
  • Far from an answer, but I think you could determine what processes share what memory based on /proc/${PID}/maps. Commented Aug 7, 2013 at 13:27
  • You can also try the ps command, which displays memory used by different processes. Commented Aug 7, 2013 at 14:27
  • @ArunR I don't see such option in ps, could you please provide it in an answer? Commented Aug 7, 2013 at 14:49
  • Can you try ps v. Also top command should list the top 30(I think) cpu users, with info on their mem usage. Commented Aug 7, 2013 at 21:52
  • @ArunR Great, please read the question first. Commented Aug 7, 2013 at 23:09

1 Answer 1

3

You will want to iterate through each processes /proc/[pid]/smaps

It will contain an entry for each VM mapping of the likes:

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 you are interested in.

If you have the Pss field in your smaps file then this is the amount of resident memory divided by the amount of processes that share the physical memory.

Private_Clean could be copy-on-write mappings. Those are commonly used for shared libraries and are generally read/no-write/execute.

Sign up to request clarification or add additional context in comments.

2 Comments

Man, I sort of hoped that there would be something slightly simpler :-D But well, thanks for the info :-)
@Let_Me_Be in bash (set $PIDS comma separated pids): grep -h -s Pss: /proc/{$PIDS}/smaps | awk '{sum += $2}END{print sum " kB"}'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.