Analyzing Discrepancies in Memory Usage Reporting: Process Resident Memory vs. System Swap in Ubuntu Linux


3 views

When monitoring memory usage on Ubuntu Linux, you might encounter discrepancies between what individual processes report and what system tools like free display. This occurs because Linux handles memory management in complex ways:

# Sample outputs showing discrepancy:
ps -e --format rss | awk 'BEGIN{c=0} {c+=$1} END{print c/1024}'
# Output: 2750.29 MB

free -m
# Output shows ~2461 MB used after buffers/cache

Several factors contribute to memory reporting differences:

  • Resident Set Size (RSS): Physical memory used by process
  • Shared Memory: Counted in multiple processes' RSS
  • Page Cache: Reclaimable memory for disk caching
  • Swap Usage: Memory pages moved to disk

To get true memory consumption (physical + swap) per process:

# Method 1: Using smem (install via 'apt install smem')
smem -t -k -P "process_name"

# Method 2: Custom ps command showing PSS (Proportional Set Size)
ps -eo pid,comm,rss,vsz,size,pmem,pcpu --sort=-rss | head -n 10

# Method 3: Detailed per-process breakdown
for pid in $(ps -eo pid); do 
    echo -n "$pid: "
    awk '/Rss/ {rss+=$2} /Swap/ {swap+=$2} END {print rss+swap " kB"}' /proc/$pid/smaps 2>/dev/null
done | sort -n -k2 -r | head -n 20

Key fields in your /proc/meminfo reveal important insights:

AnonPages:    2085472 kB  # Non-file backed memory
SwapCached:   160980 kB   # Memory swapped out but still in cache
Committed_AS: 9522364 kB  # Total allocated memory (including swap)

For identifying and managing memory-heavy processes:

# Find top 10 memory consumers (RSS + Swap)
ps -eo pid,comm,rss,vsz,size,pmem,pcpu --sort=-rss | head -n 10

# Alternative using htop (interactive)
sudo apt install htop
htop --sort-key=PERCENT_MEM

# Kernel same-page merging (KSM) can help reduce duplicates
sudo bash -c 'echo 1 > /sys/kernel/mm/ksm/run'

For deeper investigation:

  • valgrind --tool=massif for application-level profiling
  • pmap -x [pid] for detailed process memory mapping
  • slabtop for kernel slab cache analysis

When running free -m and summing RSS values with ps, you'll often notice discrepancies. Here's why:

# Summing RSS (Resident Set Size)
sudo ps -e --format rss | awk 'BEGIN{c=0} {c+=$1} END{print c/1024}'

# System-wide view
free -m

The difference comes from how Linux manages memory:

  • RSS (Resident Set Size): Physical memory actually held in RAM
  • Swap: Memory pages moved to disk
  • Shared Memory: Counted multiple times in process RSS
  • Page Cache: Reclaimable memory used for disk caching

For true memory consumption (RAM + swap), use:

ps -eo pid,comm,rss,vsize,sz,swap | awk 'NR==1 || $6>0'

Or examine detailed per-process data:

cat /proc/[pid]/smaps | grep -i swap

Here's a comprehensive script to identify memory hogs:

#!/bin/bash
echo "PID\tProcess\tRSS(MB)\tSwap(MB)\tTotal"
ps -eo pid,comm,rss,sz \
| awk 'NR>1 {rss=$3/1024; swap=($4-$3)/1024; total=rss+swap; 
           printf "%s\t%s\t%.1f\t%.1f\t\t%.1f\n",$1,$2,rss,swap,total}' \
| sort -k5 -nr | head -20

Key indicators from your output:

  • AnonPages: 2085472 kB - Non-file backed memory
  • SwapCached: 160980 kB - Memory both in swap and RAM
  • Active(anon): 1745820 kB - Frequently used anonymous pages

When facing high swap usage:

  1. Identify and restart leaky processes
  2. Adjust swappiness: sysctl vm.swappiness=10
  3. Consider zswap or zram for compression
  4. Monitor with: vmstat 1 or smem -t -k