When working with the top
command in Linux, you'll notice two primary memory columns:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
The VIRT
column shows virtual memory usage (VSZ) while RES
shows resident memory (RSS). By default, pressing M
sorts by RSS, but sometimes you need to analyze VSZ instead.
For immediate VSZ sorting in an active top
session:
1. Launch top:top
2. PressShift+O
to open the sort field menu 3. Typevirt
and press Enter
This will instantly re-sort processes by virtual memory consumption.
To make VSZ sorting the default:
1. Create or edit~/.toprc
2. Add these lines:RCfile for "top with windows" # shameless bullshit line global "Mem_sort=2" # 0=pid, 1=res, 2=virt, etc.
3. Save and restart top
For scripting or one-off analysis, use ps
with custom sorting:
ps aux --sort=-vsz | head -n 10
This shows the top 10 processes by virtual memory usage. The -vsz
parameter sorts in descending order.
Create an alias for VSZ-focused monitoring:
alias vsztop='top -o VIRT -c -n 1 -b | head -n 20'
This shows a static snapshot of the top 20 processes sorted by VSZ.
Remember that VSZ includes:
- Allocated but unused memory
- Shared libraries
- Memory-mapped files
High VSZ doesn't necessarily indicate a memory leak - it might just be pre-allocated address space.
Use this bash snippet to track VSZ growth:
watch -n 5 'ps -eo pid,vsz,cmd --sort=-vsz | head -n 5'
Updates the top 5 VSZ consumers every 5 seconds.
When investigating high VSZ processes:
pmap -x [PID] | less
This breaks down memory allocation by the process, helping identify memory-hungry components.
When analyzing process memory usage in Linux, it's crucial to distinguish between:
- RES (Resident Memory): Physical RAM currently used
- VSZ (Virtual Memory): Total address space allocated (RAM + swap)
The default M
sort in top
only sorts by RES. To sort by VSZ (Virtual Size), we need deeper control.
1. Run top command
2. Press Shift + F to enter field management
3. Navigate to VIRT (Virtual Memory) column using arrow keys
4. Press s to select as sort field
5. Press Enter to confirm
6. Press Esc to return to main view
For persistent VSZ sorting, create or modify ~/.toprc
:
RCfile for "top with windows" # shameless bullshit line
Id:a, Mode_altscr=0, Mode_irixps=1, Delay_time=3.0, Curwin=0
Def fieldscur=AEHIOQTWKNMbcdfgjplrsuvyzX
winflags=30427, sortindx=10, maxtasks=0
summclr=1, msgsclr=1, headclr=3, taskclr=1
Key parameters:
sortindx=10
: Sets VIRT as default sort column (adjust index based on your fieldscur)- Verify column order with
top -O
to list available fields
For scriptable solutions, use ps
with VSZ sort:
ps aux --sort=-vsz | head -n 10
Or more detailed output:
ps -eo pid,ppid,cmd,%mem,%cpu,vsz --sort=-vsz | head -n 20
When troubleshooting JVM memory leaks, VSZ provides better visibility:
top -p $(pgrep -d',' java) -o VIRT
This shows all Java processes sorted by virtual memory allocation.
For comprehensive analysis, combine with pmap
:
top -o VIRT -b -n1 | grep high_memory_process | awk '{print $1}' | xargs pmap -x
This reveals detailed memory mapping of high-VSZ processes.