How is memory usage reported in Linux?
In short:
Virtual size: is the amount of address space that a process is managing. The virtual address space contains everything that the process can access through pointers (memory address references). For example, if your program gets access to the framebuffer of your video card, that memory is mapped to the process virtual space and receives an address that is stored to a pointer. Memory-mapped files and anonymous mappings are also accounted into the virtual address space size. Pretty much everything is in the virtual size. If you sum up the size of all address ranges listed in
/proc/<pid>/maps
, it should return you roughly the same value of the virtual size.Resident size: is the amount of memory that belongs specifically to that process that is currently resident in memory. That means, the amount of memory that is not in swap. Note that parts of the process can be in swap memory even when the process is running. The operating system will pull these regions from the swap when the process tries to access it. This should include the heap, the stacks of all threads and other private mappings. If you look in
/proc/<pid>/maps
, the[stack]
,[heap]
and other anonymous mappings (those without file paths) are either swapped or accounted in the resident size.Shared size: is the amount of memory that may belong to multiple processes. For example, if you have four instances of the same application loaded in memory, you will have four instances of the heap and at least four stacks, one for each process (this is the resident memory), but you will have only one instance of the binary code of the program and its libraries. This is the shared space. Not only it includes the program binary code and its libraries, but also localization files, read-only program data, SysV and POSIX shared memory segments, semaphores, etc... If you look in
/proc/<pid>/maps
, most mappings tied to library and program files are shared.
Note that VIRT contains the union of RSS and SHR, and will always be greater than any one of them. There may be regions accounted as both RSS and SHR.
In Juliano answer:
Note that RSS + SHR <= VIRT, always.
This is just false. SHR contains all virtual memory that could be shared with other processes, and RSS contains all memory physically in RAM that is used by the process.
Thus all shared memory currently in RAM is counted both in SHR and in RSS, so SHR + RSS has no meaning since it can contain duplicates counts.
To construct a process with RSS + SHR > VIRT, just mmap a large file (1GB), then read it completely: the mmaped file will be loaded in RAM, and VIRT, SHR and RSS will each be slighly larger than 1GB, so SHR + RSS > VIRT.