Calculating virtual memory reserved by a process
Learn how to calculate virtual memory reserved by a process using the pmap file. A brief description of the parameters found in the file is given along with an example of how to read the output.
Virtual memory reservation from reading the vmstat file
The value of the as_stats.anon.rsv parameter represents the virtual memory reserved by the process with the process ID pid. See the vmsat page to learn more about the file output.
Virtual memory reservation from reading the pmap file
Reading the pmap file gives you a more detailed look at the memory mapping of the process. To find
how much virtual memory is reserved by a process, sum up the values of all the rsv parameters
in the file's output. You can read the file using cat:
cat /proc/<pid>/pmap
Below is a quick description of each parameter:
- vaddr
- Virtual memory address.
- size
- The maximum size of virtual memory that can be reserved by that region.
- flags
- Mapped memory flags. See mmap() for details on the flags.
- prot
- Page protection flags, right shifted by 8 bits. See mmap() for details on the flags.
- maxprot
- The maximum value that prot can have, right shifted by 8 bits.
- dev
- Device ID of mapped objects.
- ino
- Inode of mapped objects.
- offset
- The offset from the start of the mapped object.
- rsv
- The amount of virtual memory reserved for the region. Adheres to the following rules:
- A non-lazy private region's rsv value should be equal to its size value. The flags parameter contains the MAP_PRIVATE flag, but not the MAP_LAZY flag.
- A lazy region shows how much memory was referenced. The flags parameter contains the MAP_LAZY flag.
- A shared region can have a zero (0) value for reasons including, but not limited to: being backed by a non-allocated, explicit physical address range, or being backed by a file or a shared object. The flags parameter may contain the MAP_PHYS flag.
- guardsize
- Stack guard size.
- refcnt
- Number of references to each object.
- mapcnt
- Number of unique mappings to the object.
- path
- What the region is used for.
Example:
In this example, we are using the pmap file method for the cat process.
This is done by running
the following code, where self is the pid for the cat command:
cat /proc/self/pmap
The following example output is returned by running the command above:
# cat /proc/self/pmap
vaddr,size,flags,prot,maxprot,dev,ino,offset,rsv,guardsize,refcnt,mapcnt,path
0x0000001c0ae92000,0x0000000000002000,0x00010071,0x05,0x0d,0x00000802,0x00000000c00000ab,\z
0x0000000000000000,0x0000000000000000,0x00000000,0x0000017e,0x0000017c,/bin/cat
0x0000001c0ae94000,0x000000000000f000,0x000800a2,0x00,0x0f,0x00000001,0x0000000000000003,\z
0x0000000000002000,0x0000000000000000,0x00000000,0x00000000,0x000001de,{bss}
0x0000001c0aea3000,0x0000000000001000,0x00010032,0x01,0x0f,0x00000802,0x00000000c00000ab,\z
0x0000000000001000,0x0000000000001000,0x00000000,0x0000017e,0x0000017c,/bin/cat
0x0000001c0aea4000,0x0000000000001000,0x00010032,0x03,0x0f,0x00000802,0x00000000c00000ab,\z
0x0000000000002000,0x0000000000001000,0x00000000,0x0000017e,0x0000017c,/bin/cat
0x0000001c0aea5000,0x0000000000008000,0x00080032,0x03,0x0f,0x00000001,0x0000000000000003,\z
0x0000000000000000,0x0000000000008000,0x00000000,0x00000000,0x000001de,{bss}
0x0000002f26c08000,0x0000000000037000,0x00010071,0x05,0x0d,0x00000802,0x00000000c000000a,\z
0x0000000000000000,0x0000000000000000,0x00000000,0x0000017e,0x0000017c,/usr/lib/ldqnx-64.so.2
0x0000002f26c3f000,0x000000000000f000,0x000800a2,0x00,0x0f,0x00000001,0x0000000000000003,\z
0x0000000000037000,0x0000000000000000,0x00000000,0x00000000,0x000001de,{bss}
0x0000002f26c4e000,0x0000000000002000,0x00010072,0x01,0x0f,0x00000802,0x00000000c000000a,\z
0x0000000000036000,0x0000000000002000,0x00000000,0x0000017e,0x0000017c,/usr/lib/ldqnx-64.so.2
0x0000002f26c50000,0x0000000000001000,0x00010072,0x03,0x0f,0x00000802,0x00000000c000000a,\z
0x0000000000038000,0x0000000000001000,0x00000000,0x0000017e,0x0000017c,/usr/lib/ldqnx-64.so.2
0x0000002f26c51000,0x0000000000001000,0x00080032,0x03,0x0f,0x00000001,0x0000000000000003,\z
0x0000000000000000,0x0000000000001000,0x00000000,0x00000000,0x000001de,{bss}
0x0000002f26c52000,0x00000000000b0000,0x00010071,0x05,0x0d,0x00000802,0x00000000c000000c,\z
0x0000000000000000,0x0000000000000000,0x00000000,0x0000017e,0x0000017c,/proc/boot/libc.so.5
0x0000002f26d02000,0x0000000000010000,0x00080022,0x00,0x0f,0x00000001,0x0000000000000003,\z
0x00000000000b0000,0x0000000000000000,0x00000000,0x00000000,0x000001de,{bss}
0x0000002f26d12000,0x0000000000004000,0x00080032,0x01,0x0f,0x00000001,0x0000000000000003,\z
0x0000000000000000,0x0000000000004000,0x00000000,0x00000000,0x000001de,{bss}
0x0000002f26d16000,0x0000000000002000,0x00010072,0x03,0x0f,0x00000802,0x00000000c000000c,\z
0x00000000000b4000,0x0000000000002000,0x00000000,0x0000017e,0x0000017c,/proc/boot/libc.so.5
0x0000002f26d18000,0x0000000000002000,0x00080032,0x03,0x0f,0x00000001,0x0000000000000003,\z
0x0000000000000000,0x0000000000002000,0x00000000,0x00000000,0x000001de,{bss}
0x0000002f26d1a000,0x0000000000013000,0x00010071,0x05,0x0d,0x00000802,0x00000000c000000e,\z
0x0000000000000000,0x0000000000000000,0x00000000,0x0000017e,0x0000017c,/proc/boot/libgcc_s.so.1
0x0000002f26d2d000,0x000000000000f000,0x00080022,0x00,0x0f,0x00000001,0x0000000000000003,\z
0x0000000000013000,0x0000000000000000,0x00000000,0x00000000,0x000001de,{bss}
0x0000002f26d3c000,0x0000000000001000,0x00080032,0x01,0x0f,0x00000001,0x0000000000000003,\z
0x0000000000000000,0x0000000000001000,0x00000000,0x00000000,0x000001de,{bss}
0x0000002f26d3d000,0x0000000000001000,0x00010072,0x03,0x0f,0x00000802,0x00000000c000000e,\z
0x0000000000013000,0x0000000000001000,0x00000000,0x0000017e,0x0000017c,/proc/boot/libgcc_s.so.1
0x00000035f9b83000,0x0000000000080000,0x00081082,0x03,0x0f,0x00000001,0x0000000000000003,\z
0x0000000000000000,0x0000000000002000,0x00001000,0x00000000,0x000001de,{stack}
0x0000004af7d62000,0x000000000000b000,0x00080002,0x03,0x0f,0x00000001,0x0000000000000003,\z
0x0000000000000000,0x000000000000b000,0x00000000,0x00000000,0x000001de,{heap}
Consider the size, flags, prot, and rsv
parameter in the example above. You need them to calculate the reserved virtual memory of the cat
command. The line at the top lists the parameters in the order in which they appear.
If you take a look at the first row in the figure, under the parameter names, you will find that the four parameters have
the following values:
- size
- 0x0000000000002000 (8KB)
- flags
- 0x00010071 (MAP_PHYS, MAP_FIXED, MAP_ELF, MAP_NOSYNCFILE, MAP_SHARED)
- prot
- 0x05 (PROT_READ, PROT_EXEC)
- rsv
- 0x0000000000000000 (0KB)
This region is of type code. It is a shared read-only mapping of an object and has reserved 0KB of virtual memory.
The second row is a region of type other that also reserves 0KB of virtual memory.
Skipping ahead to the third row, you can see the following parameter values:
- size
- 0x0000000000001000 (4KB)
- flags
- 0x00010032 (MAP_PHYS, MAP_FIXED, MAP_ELF, MAP_PRIVATE)
- prot
- 0x01 (PROT_READ)
- rsv
- 0x0000000000001000 (4KB)
This region is of type data (read only) and has reserved its maximum of 4KB of memory.
Analyzing the output line by line produces the table below with the total virtual
memory reserved at the bottom:
| Region type | Row numbers | Reserved memory in KB | Total in KB |
|---|---|---|---|
| Code | 1, 6, 11, 16 | 0, 0, 0, 0 | 0 |
| Data (Read Only) | 3, 8, 13, 18 | 4, 8, 16, 4 | 32 |
| Data (Read/Write) | 4, 5, 9, 10, 15, 14, 19 | 4, 32, 4, 4, 8, 8, 4 | 64 |
| Other | 2, 7, 12, 17 | 0, 0, 0, 0 | 0 |
| Heap | 21 | 44 | 44 |
| Stack | 20 | 8 | 8 |
| Total virtual memory reserved by cat: | 148 | ||
Reading the vmstat file for the cat command, you can see that the amount of reserved
virtual memory (as_stat.anon_rsv) is indeed 148KB:
# cat /proc/self/vmstat
as_stats.vm_region=22
as_stats.vm_map=15
as_stats.map_size=0x1a2 (1.632MB)
as_stats.map_phys=0xff (1020.000kB)
as_stats.map_shared=0xff (1020.000kB)
as_stats.map_private=0xa3 (652.000kB)
as_stats.rss=0x1e (120.000kB)
as_stats.flt_read=0
as_stats.flt_write=3
as_stats.flt_exec=0
as_stats.flt_zero=0
as_stats.flt_zfod=24
as_stats.flt_cow=13
as_stats.flt_hit=0
as_stats.flt_miss=0
as_stats.anon_rsv=0x25 (148.000kB)
as_stats.rlimit_data=0xb (44.000kB)
Page updated:
