private memory calculated by pmap, a combination of virtual memory size and resident memory size?

5,435

I found this post titled: Understanding memory usage on Linux which I think does a better job than the one I'd found previously on the linuxquestions.org site, titled: How to accurately measure memory usage?.

excerpt from the Understanding memory ... post

ps output
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
dbunker   3468  0.0  2.7  25400 14452 ?        S    20:19   0:00 kdeinit: kedit
pmap output
Address   Kbytes Mode  Offset           Device    Mapping
08048000      40 r-x-- 0000000000000000 0fe:00000 kdeinit
08052000       4 rw--- 0000000000009000 0fe:00000 kdeinit
08053000    1164 rw--- 0000000008053000 000:00000   [ anon ]
40000000      84 r-x-- 0000000000000000 0fe:00000 ld-2.3.5.so
40015000       8 rw--- 0000000000014000 0fe:00000 ld-2.3.5.so
40017000       4 rw--- 0000000040017000 000:00000   [ anon ]
... (trimmed) ...
mapped: 25404K    writeable/private: 2432K    shared: 0K
description of what's going on

If you go through the output, you will find that the lines with the largest Kbytes number are usually the code segments of the included shared libraries (the ones that start with "lib" are the shared libraries). What is great about that is that they are the ones that can be shared between processes. If you factor out all of the parts that are shared between processes, you end up with the "writeable/private" total, which is shown at the bottom of the output. This is what can be considered the incremental cost of this process, factoring out the shared libraries. Therefore, the cost to run this instance of KEdit (assuming that all of the shared libraries were already loaded) is around 2 megabytes. That is quite a different story from the 14 or 25 megabytes that ps reported.

How much swap is process X using?

You can find out a processes' swap space that it's using with this command:

$ grep VmSwap /proc/$(pidof chrome | awk '{print $1}')/status
VmSwap:     1324 kB

The above is getting the first PID of chrome returning the VmSwap value for it.

memstat

You might want to check out this tool instead if you're looking for accurate measures of memory, it's callled memstat:

There's a tutorial on cyberciti.biz titled: Linux: Find Out What’s Using Up All Virtual Memory that shows memstat in action.

References

Share:
5,435

Related videos on Youtube

JohnMerlino
Author by

JohnMerlino

Updated on September 18, 2022

Comments

  • JohnMerlino
    JohnMerlino over 1 year

    You can use the ps command to get the virtual memory size and the resident memory size of a process:

    $ ps aux | grep apache2
    USER       PID  %CPU %MEM  VSZ  RSS     TTY   STAT START    TIME    COMMAND
    www-data 31141  0.0  0.3 163864 24008 ?        S    16:15   0:01 /usr/sbin/apache2 -k start
    

    Now virtual memory and resident memory doesn't provide accurate precision of a process's true memory footprint, because it also factors in shared libraries.

    So a better solution is to use pmap to get the true memory usage of a process. So here is the output of pmap using the same process as above:

    $ sudo pmap -d 31141
    31141:   /usr/sbin/apache2 -k start
    Address           Kbytes Mode  Offset           Device    Mapping
    00007f85bbb82000      28 r-x-- 0000000000000000 0fc:00000 libnss_dns-2.15.so
    ...
    ...
    mapped: 163864K    writeable/private: 28420K    shared: 592K
    

    So the "writable/private" value is the private memory of the process, which is almost 28 mb. My question: Is this private memory only main memory (RAM) or does this number also include swap space possibly?

  • JohnMerlino
    JohnMerlino almost 11 years
    Private resident size would be the size of memory the process is using on main memory, excluding any shared libraries. But not sure what is meant by "mapped total".
  • slm
    slm almost 11 years
    @JohnMerlino - let me know if the updates make more sense. The previous version of the answer was too vague.
  • JohnMerlino
    JohnMerlino almost 11 years
    "This is what can be considered the incremental cost of this process, factoring out the shared libraries." But that doesn't tell me what percentage of that incremental cost is residant memory and disk memory.
  • slm
    slm almost 11 years
    @JohnMerlino - when you say disk memory you're talking about swap?
  • slm
    slm almost 11 years
    I think you might have to calculate that manually. You can get a processes VmSwap that it's using with this command: grep VmSwap /proc/$(pidof chrome | awk '{print $1}')/status: VmSwap: 1324 kB
  • JohnMerlino
    JohnMerlino almost 11 years
    Yes I was reffering to disk space as swap above.