tags : Virtual Memory, Computer Memory, Infrastructure

Most common memory metric tools report usage are given in KiB because of /proc/<pid>/smaps reports stuff.

Common metrics

Virtual Memory Size(VIRT/VSZ)

Most of the time, this is not a useful number.

  • VSZ in ps = VIRT in top
  • This relates to vitual Memory mechanism in linux which allows a process to access the entire address range that the processor allows.
  • Represents how much memory the program is able to access at the present moment, including ,mmapped files and swapped out pages, shared libraries, memory that’s allocated but not used.
  • It includes all memory that the process can access, including memory that is swapped out, memory that is allocated, but not used, and memory that is from shared libraries.
  • Examples
    • An application requests 1 GB of memory but uses only 1 MB, VIRT will report 1 GB.
    • An application mmaps a 1 GB file and never uses it, VIRT will report 1 GB.

Resident Set Size (RES/RSS)

Better indicator of inuse memory than VIRT/VSZ, but

  • Some of the memory may be shared with other processes
    • Since part of the memory is shared
    • Many processes may use it
    • If you add up all of the RSS values
    • You can easily end up with more space than your system has.
  • Evicting/Reclaim of memory caches is not guaranteed
    • RSS does not include cache in its calculation
    • Cache used by the program might not be reclaimable at that point.
    • So the actual memory consumption can be way higher than RSS.
    • eg. Something w 200MB RSS can be actually at 2GB because of cache
  • RSS in ps = RES in top
  • An representation of how much actual physical memory a process is consuming, can be 0 for a process that’s been sleeping.
  • Number of memory pages the process has in real memory multiplied by pagesize. Does not include the swapped out memory.

Shared Memory (SHR)

  • Indicates how much of the VIRT size is actually sharable memory or libraries.
  • Whole library is mapped and counted in VIRT and SHR, but only parts of the shared library used are counted in RES

WSS

A working set(WSS) is the set of memory chunks required to complete an operation. In other words, number of pages a process needs in memory to keep “working”. These are also most frequently accessed.

  • It includes active and recently used pages, which also includes cache. active/ inactive statistics are orthogonal to the buffer/cache
  • The page fault handler attempts to keep each currently running process’s working set of pages in physical memory until the process blocks awaiting input or exits.
  • The working set is determined dynamically at runtime, and can change over time as a program does different things.
  • Working Set Size Estimation

WSS vs RSS

  • RSS is the number of pages of a process that actually reside in main memory.
  • WSS is the number of pages a process needs in memory to keep “working”.
  • Meaning that RSS may include some pages that the process doesn’t really need right now. So usually RSS >= WSS.

PSS

In 2009, Matt Mackall (Developer of Mercurial, 2005) began looking at the problem of accounting for shared pages in process memory measurement and added two new metrics called the unique set size/USS, and the proportional set size/PSS

PSS: In kilobytes, from /proc/<pid>/smaps. Tracks the shared memory as a proportion used by the current process.

USS

USS: Amount of memory that is committed to physical memory and is unique to a process; it is not shared with any other. It is the amount of memory that would be freed if the process were to terminate.

Memory pressure

  • Good metric
  • Identifying efficient use of a system vs actual pressure
  • Sort of signals us about the things that the system is currently doing that we wouldn’t have to do if we had more physical memory
  • Helpful: Find bottlenecks, load shedding, pre-OOM detection

Scan rate

how often cpu looking at page table

Interesting Takeaways

  • Processes that are forked have different address spaces but threads share the same address space. so the RSS, VSZ and PSS for each thread is identical to all of the other threads in the process.
  • See the “Linux Memory Types” section when you do man top