Memory metrics
Common memory query commands and related metrics in Linux.
Linux memory overview
The MemTotal value retrieved by the free command is less than the total RAM capacity because the BIOS and kernel consume some memory during boot.
dmesg | grep Memory
Memory: 131604168K/134217136K available (14346K kernel code, 9546K rwdata, 9084K rodata, 2660K init, 7556K bss, 2612708K reserved, 0K cma-reserved)
The following commands query Linux memory:
The Linux memory formula is:
total = used + free + buff/cache // Total memory = Used memory + Free memory + Buffer/Cache
The used memory includes the memory consumed by the kernel and all processes.
kernel used = Slab + VmallocUsed + PageTables + KernelStack + HardwareCorrupted + Bounce + X
Process memory
The memory consumed by a process includes:
-
Physical memory mapped from the virtual address space.
-
Memory consumed by the page cache, which is generated during disk reads and writes.
Physical memory mapped from the virtual address space
-
Physical memory: The installed hardware memory, such as RAM sticks.
-
Virtual memory: The memory that the operating system provides for a program to run. A program's running space includes user space (user mode) and kernel space (kernel mode).

-
User mode: Programs run with low privileges.
The data storage area includes:
-
Stack: The function stack for function calls.
-
Memory Mapping Segment (MMap): The memory mapping area.
-
Heap: The dynamically allocated memory.
-
BSS segment: The storage area for uninitialized static variables.
-
Data segment: The storage area for initialized static constants.
-
Text segment: The storage area for binary executable code.
Programs running in user mode use MMap to map virtual addresses to physical memory.
-
-
Kernel mode: Programs running in this mode need to access operating system kernel data.
The data storage area includes:
-
Direct mapping area: Maps virtual addresses to physical memory using a simple mapping.
-
VMALLOC: The kernel's dynamic mapping space. It maps contiguous virtual addresses to non-contiguous physical memory.
-
Persistent kernel mapping area: Maps virtual addresses to the high-end memory of the physical memory.
-
Fixed mapping area: Fulfills special mapping requirements.
-
-
Physical memory mapped from virtual addresses can be divided into shared physical memory and exclusive physical memory. As shown in the following figure, Physical Memory 1 and 3 are exclusively used by Process A, Physical Memory 2 is exclusively used by Process B, and Physical Memory 4 is shared by Process A and Process B.
Page cache
In addition to direct mapping with MMap files, a process can use buffered I/O system calls to write data to the page cache. Therefore, the page cache also consumes a portion of memory.
Process memory metrics
Memory metrics for a single process
Processes use the following resource storage types:
-
Anonymous pages: The heap and stack space used by the program, which does not have a corresponding file on disk.
-
File-backed pages: Resources stored in disk files. These files contain content such as code segments and font information.
Related metrics:
-
anno_rss (RSan): The exclusive memory for all types of resources.
-
file_rss (RSfd): All memory occupied by file-backed resources.
-
shmem_rss (RSsh): The shared memory of anonymous resources.
Use the following commands to query metrics:
|
Command |
Metric |
Description |
Formula |
|
|
VIRT |
The virtual address space. |
None |
|
RES |
The physical memory mapped by RSS. |
anno_rss + file_rss + shmem_rss |
|
|
SHR |
The shared memory. |
file_rss + shmem_rss |
|
|
MEM% |
The memory usage. |
RES / MemTotal |
|
|
|
VSZ |
The virtual address space. |
None |
|
RSS |
The physical memory mapped by RSS. |
anno_rss + file_rss + shmem_rss |
|
|
MEM% |
The memory usage. |
RSS / MemTotal |
|
|
|
USS |
The exclusive memory. |
anno_rss |
|
PSS |
The proportionally allocated memory. |
anno_rss + file_rss/m + shmem_rss/n |
|
|
RSS |
The physical memory mapped by RSS. |
anno_rss + file_rss + shmem_rss |

WSS (Working Set Size) provides a more reasonable measure of a process's actual memory usage. However, due to limitations in the Linux page reclaim mechanism, WSS remains a conceptual metric. No tool can calculate it accurately; tools can only approximate it.
Memory metrics for a process control group
Control groups (cgroups) are used to limit, account for, and isolate a group of Linux process resources. For more information, see the official documentation.
Cgroups are managed hierarchically. Each node contains files that report metrics for the control group it represents. For example, a Memory Control Group (memcg) reports memory-related metrics.
The Memory Cgroup file contains the following metrics:
cgroup.event_control # Interface for eventfd
memory.usage_in_bytes # Displays the current memory usage
memory.limit_in_bytes # Sets or displays the current memory limit
memory.failcnt # Displays the number of times the memory usage reached the limit
memory.max_usage_in_bytes # The historical maximum memory usage
memory.soft_limit_in_bytes # Sets or displays the current memory soft limit
memory.stat # Displays the memory usage of the current cgroup
memory.use_hierarchy # Sets or displays whether to include the memory usage of child cgroups in the current cgroup
memory.force_empty # Triggers the system to immediately reclaim as much memory as possible from the current cgroup
memory.pressure_level # Sets memory pressure notification events. Used with cgroup.event_control
memory.swappiness # Sets and displays the current swappiness value
memory.move_charge_at_immigrate # Sets whether the memory charge of a process is moved when the process moves to another cgroup
memory.oom_control # Sets or displays OOM control configurations
memory.numa_stat # Displays NUMA-related memory
The following three metrics are important:
-
memory.limit_in_bytes: Limits the amount of memory that the current control group can use. This corresponds to the memory limit metric in Kubernetes and Docker.
-
memory.usage_in_bytes: The total memory actually used by all processes in the current control group. The value is approximately equal to the sum of RSS and Cache from the memory.stat file.
-
memory.stat: Detailed memory statistics for the current control group.
Field in the memory.stat file
Description
cache
The size of the page cache.
rss
The sum of anno_rss memory for all processes in the control group.
mapped_file
The sum of file_rss and shmem_rss memory for all processes in the control group.
active_anon
The memory and swap cache used by all anonymous processes in the active least-recently-used (LRU) list, including
tmpfs(shmem). The unit is bytes.inactive_anon
The memory and swap cache used by all anonymous processes in the inactive LRU list, including
tmpfs(shmem). The unit is bytes.active_file
The memory used by all file-backed processes in the active LRU list. The unit is bytes.
inactive_file
The memory used by all file-backed processes in the inactive LRU list. The unit is bytes.
unevictable
The unevictable memory. The unit is bytes.
If a metric has the
total_prefix, it represents the sum of that metric for the current control group and all its descendant control groups. For example, the total_rss metric is the sum of the RSS metrics for the current control group and all its descendant control groups.
Summary
The following table compares single-process metrics with cgroup metrics.
|
Metric |
Single process |
Process control group (memcg) |
|
RSS |
anon_rss + file_rss + shmem_rss |
anon_rss |
|
mapped_file |
None |
file_rss + shmem_rss |
|
cache |
None |
PageCache |
-
The RSS metric for a control group includes only anon_rss, which corresponds to the USS metric for a single process. Therefore, for a control group, mapped_file + RSS corresponds to the RSS metric for a single process.
-
For a single process, the page cache must be calculated separately. For a control group, the memory reported in the memcg file already includes the page cache.
Memory statistics in Docker and Kubernetes
Memory statistics in Docker and Kubernetes are the same as Linux memcg statistics, but their definitions of memory usage are different.
docker stat command
Sample response:
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
87c1d1871d2d amazing_shtern 0.37% 475.7MiB / 1.939GiB 23.96% 2.26MB / 860kB 4.99MB / 77MB 38
For more information about how the docker stat command works, see the official documentation.
func calculateMemUsageUnixNoCache(mem types.MemoryStats) float64 {
return float64(mem.Usage - mem.Stats["cache"])
}
-
LIMIT corresponds to the control group's memory.limit_in_bytes.
-
MEM USAGE corresponds to the control group's memory.usage_in_bytes - memory.stat[total_cache].
kubectl top pod command
The kubectl top command uses Metrics Server and Heapster to retrieve the working_set value from cAdvisor. This value represents the amount of memory used by a pod instance, excluding the pause container. The following code shows how Metrics Server retrieves pod memory. For more information, see the official documentation.
func decodeMemory(target *resource.Quantity, memStats *stats.MemoryStats) error {
if memStats == nil || memStats.WorkingSetBytes == nil {
return fmt.Errorf("missing memory usage metric")
}
*target = *uint64Quantity(*memStats.WorkingSetBytes, 0)
target.Format = resource.BinarySI
return nil
}
The following is the cAdvisor memory working set algorithm. For more information, see the official documentation.
func setMemoryStats(s *cgroups.Stats, ret *info.ContainerStats) {
ret.Memory.Usage = s.MemoryStats.Usage.Usage
ret.Memory.MaxUsage = s.MemoryStats.Usage.MaxUsage
ret.Memory.Failcnt = s.MemoryStats.Usage.Failcnt
if s.MemoryStats.UseHierarchy {
ret.Memory.Cache = s.MemoryStats.Stats["total_cache"]
ret.Memory.RSS = s.MemoryStats.Stats["total_rss"]
ret.Memory.Swap = s.MemoryStats.Stats["total_swap"]
ret.Memory.MappedFile = s.MemoryStats.Stats["total_mapped_file"]
} else {
ret.Memory.Cache = s.MemoryStats.Stats["cache"]
ret.Memory.RSS = s.MemoryStats.Stats["rss"]
ret.Memory.Swap = s.MemoryStats.Stats["swap"]
ret.Memory.MappedFile = s.MemoryStats.Stats["mapped_file"]
}
if v, ok := s.MemoryStats.Stats["pgfault"]; ok {
ret.Memory.ContainerData.Pgfault = v
ret.Memory.HierarchicalData.Pgfault = v
}
if v, ok := s.MemoryStats.Stats["pgmajfault"]; ok {
ret.Memory.ContainerData.Pgmajfault = v
ret.Memory.HierarchicalData.Pgmajfault = v
}
workingSet := ret.Memory.Usage
if v, ok := s.MemoryStats.Stats["total_inactive_file"]; ok {
if workingSet < v {
workingSet = 0
} else {
workingSet -= v
}
}
ret.Memory.WorkingSet = workingSet
}
Based on this algorithm, the kubectl top pod Memory Usage is calculated as follows: Memory Usage = Memory WorkingSet = memory.usage_in_bytes - memory.stat[total_inactive_file].
Summary
|
Command |
Ecosystem |
Memory Usage calculation |
|
|
Docker |
memory.usage_in_bytes - memory.stat[total_cache] |
|
|
K8s |
memory.usage_in_bytes - memory.stat[total_inactive_file] |
To match values across ecosystems when querying memory with top or ps, calculate the process group Memory Usage as follows:
|
Process group ecosystem |
Formula |
|
Memcg |
rss + cache (active cache + inactive cache) |
|
Docker |
rss |
|
K8s |
rss + active cache |
Java memory statistics
Virtual address space of a Java process
The following figure shows data storage areas in the virtual address space of a Java process:
Obtain memory metrics using JMX
JMX exposes memory metrics for Java processes, which you can view with a tool like JConsole. On the Memory tab in JConsole, you can select metrics to monitor from the Chart drop-down list. Available metrics include Heap Memory Usage, Non-Heap Memory Usage, and various memory pools such as G1 Eden Space, G1 Old Gen, G1 Survivor Space, Metaspace, CodeHeap 'profiled nmethods', and CodeHeap 'non-nmethods'. A line chart displays the historical trend of the selected metric. The details area shows used memory, committed memory, maximum value, and garbage collection statistics for G1 Young Generation and G1 Old Generation. You can also click the Perform GC button to manually trigger garbage collection.
Memory-related data is exposed through MBeans. On the MBeans tab in JConsole, you can find memory-related MBeans in the navigation tree on the left. Expand the java.lang node to find MBeans such as Memory (which includes attributes, operations, and notifications), MemoryManager (which includes CodeCacheManager and Metaspace Manager), and MemoryPool (which includes pools like G1 Eden Space, G1 Old Gen, G1 Survivor Space, Metaspace, and Compressed Class Space).
JMX does not expose all memory metrics of a JVM process. For example, thread memory is not included. Therefore, the sum of JMX memory data does not equal the RSS of the JVM process.
JMX MemoryUsage tool
JMX exposes the MemoryUsage concept through MemoryPool MBeans. For more information, see the official documentation.
The used metric indicates the consumed physical memory.
NMT tool
Java Hotspot VM provides a memory tracking tool called Native Memory Tracking (NMT). For more information about how to use it, see the official documentation.
NMT has its own overhead and is not suitable for use in production environments.
The following metrics are obtained through NMT:
jcmd 7 VM.native_memory
Native Memory Tracking:
Total: reserved=5948141KB, committed=4674781KB
- Java Heap (reserved=4194304KB, committed=4194304KB)
(mmap: reserved=4194304KB, committed=4194304KB)
- Class (reserved=1139893KB, committed=104885KB)
(classes #21183)
( instance classes #20113, array classes #1070)
(malloc=5301KB #81169)
(mmap: reserved=1134592KB, committed=99584KB)
( Metadata: )
( reserved=86016KB, committed=84992KB)
( used=80663KB)
( free=4329KB)
( waste=0KB =0.00%)
( Class space:)
( reserved=1048576KB, committed=14592KB)
( used=12806KB)
( free=1786KB)
( waste=0KB =0.00%)
- Thread (reserved=228211KB, committed=36879KB)
(thread #221)
(stack: reserved=227148KB, committed=35816KB)
(malloc=803KB #1327)
(arena=260KB #443)
- Code (reserved=49597KB, committed=2577KB)
(malloc=61KB #800)
(mmap: reserved=49536KB, committed=2516KB)
- GC (reserved=206786KB, committed=206786KB)
(malloc=18094KB #16888)
(mmap: reserved=188692KB, committed=188692KB)
- Compiler (reserved=1KB, committed=1KB)
(malloc=1KB #20)
- Internal (reserved=45418KB, committed=45418KB)
(malloc=45386KB #30497)
(mmap: reserved=32KB, committed=32KB)
- Other (reserved=30498KB, committed=30498KB)
(malloc=30498KB #234)
- Symbol (reserved=19265KB, committed=19265KB)
(malloc=16796KB #212667)
(arena=2469KB #1)
- Native Memory Tracking (reserved=5602KB, committed=5602KB)
(malloc=55KB #747)
(tracking overhead=5546KB)
- Shared class space (reserved=10836KB, committed=10836KB)
(mmap: reserved=10836KB, committed=10836KB)
- Arena Chunk (reserved=169KB, committed=169KB)
(malloc=169KB)
- Tracing (reserved=16642KB, committed=16642KB)
(malloc=16642KB #2270)
- Logging (reserved=7KB, committed=7KB)
(malloc=7KB #267)
- Arguments (reserved=19KB, committed=19KB)
(malloc=19KB #514)
- Module (reserved=463KB, committed=463KB)
(malloc=463KB #3527)
- Synchronizer (reserved=423KB, committed=423KB)
(malloc=423KB #3525)
- Safepoint (reserved=8KB, committed=8KB)
(mmap: reserved=8KB, committed=8KB)
The output shows that the JVM divides memory into areas such as Java Heap and Class, along with many additional memory blocks. JMX does not expose thread memory usage, yet a typical Java program has tens of thousands of threads that consume significant memory.
For more information about the definitions of memory types within HotSpot, see the official documentation.
Reserved and Committed metrics
NMT output exposes two concepts: Reserved and Committed. Neither maps directly to actual physical memory usage (Used).
The following figure shows how Reserved and Committed virtual address states map to physical memory. Committed is always greater than Used, and Used is essentially equivalent to the RSS of the JVM process.
Summary
-
Common Java monitoring tools collect metrics primarily through JMX. For memory, JMX exposes certain trackable memory pools within the JVM, but their sum does not map to the RSS of the JVM process.
-
NMT exposes details of memory used within the JVM, but reports Committed rather than Used. The total Committed value is therefore slightly larger than the RSS value.
-
NMT cannot track memory allocated outside the JVM, such as through malloc. It is therefore normal to see an RSS value larger than the NMT total.
-
A significant gap between NMT Committed and RSS may indicate a memory leak.
You can use other NMT metrics to investigate further:
-
Use NMT's baseline and diff features to identify which area within the JVM is causing the problem.
-
Use NMT in combination with pmap to investigate memory issues outside the JVM.
-