'difference between memory measured by statm and by top

The amount of memory used by my program reported by top is more than twice the amount reported in /proc/[pid]/statm. What explains this difference?



Solution 1:[1]

From the "man" page:

https://man7.org/linux/man-pages/man5/proc.5.html

/proc/[pid]/statm
Provides information about memory usage, measured in pages.  
The columns are:

        size       (1) total program size
                   (same as VmSize in /proc/[pid]/status)
        resident   (2) resident set size
                   (inaccurate; same as VmRSS in /proc/[pid]/status)
        shared     (3) number of resident shared pages
                   (i.e., backed by a file)
                   (inaccurate; same as RssFile+RssShmem in
                   /proc/[pid]/status)
        text       (4) text (code)
        lib        (5) library (unused since Linux 2.6; always 0)
        data       (6) data + stack
        dt         (7) dirty pages (unused since Linux 2.6; always 0)

Some of these values are inaccurate because of a kernel-
internal scalability optimization.  If accurate values are
required, use /proc/[pid]/smaps or
/proc/[pid]/smaps_rollup instead, which are much slower
but provide accurate, detailed information.

ALSO: VMsize is the "address space" that the process has in use: the number of available adresses. These addresses do not have to have any physical memory attached to them. Attached physical memory is the RSS figure.

Q: So what specific values did you have questions about?


Please see my comments below, and please refer to these links:

Short answer:

  • RES in top (RSS is ps), this is the sum of memory that is mapped to physical pages of memory.
  • VMsize is the "address space" that the process has in use: the number of available adresses.
  • "VMSize" addresses do not (necessarily) have any physical memory attached to them.
  • Attached physical memory ("Resident memory") is the RSS figure.

It would also be helpful to see your actual values from top, /proc/nnn/stat and from ps.


Here's a great article (one of many):

Process Memory Management in Linux - Baeldung.com

  • VSZ Memory

    VSZ is short for Virtual Memory Size. It’s the total amount of memory a process may hypothetically access.

    It accounts for the size of the binary itself, any linked libraries, and any stack or heap allocations.

    When a process is started, VSZ memory becomes RSS memory...

  • RSS Memory

    As opposed to VSZ, RSS, also short for Resident Set Size, is a measurement that shows how much RAM has been allocated to a process during its execution

    But, it isn’t actually an accurate measurement for RAM usage for a process since it doesn’t take into account libraries already loaded in memory...

    Because RSS shows the whole memory output, even for information that has already been preloaded, if we add up everything in the processes list, we’d get more memory than the total memory allocated.

I hope that all helps ... at least a little.

If you've still got questions, please copy/paste some actual values from your system, so we can discuss specific examples.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1