Memory Monitoring in Linux, Understanding calloc(), Thrashing,
Thrashing and Working Set Model,
Paging and Segmentation, Memory Management
Memory Monitoring in Linux
Memory monitoring means observing how RAM, swap,
cache, and processes use memory in a Linux system.
It helps detect:
Memory leaks
High RAM usage
Thrashing
Swap overuse
Performance bottlenecks
Common Linux Memory Monitoring Tools
|
Tool |
Purpose |
|
free |
Overall memory usage |
|
top |
Real-time process monitoring |
|
htop |
Interactive process monitor |
|
vmstat |
Virtual memory statistics |
|
sar |
Historical memory stats |
|
ps |
Process memory usage |
|
/proc/meminfo |
Detailed kernel memory info |
|
pmap |
Process memory map |
|
smem |
Accurate proportional memory |
|
iotop |
Detect swap/thrashing I/O |
1. Using free
Command:
free -h
Example output:
total used free
shared buff/cache available
Mem: 7.7G 2.1G
1.2G 200M 4.4G 5.0G
Swap: 2.0G 100M
1.9G
Important Columns
|
Column |
Meaning |
|
used |
Currently used memory |
|
free |
Completely unused RAM |
|
buff/cache |
Kernel cache |
|
available |
Memory available to apps |
2. Using top
Command:
top
Shows:
CPU usage
RAM usage
Running processes
Important fields:
|
Field |
Meaning |
|
%MEM |
Process memory percentage |
|
RES |
Resident memory |
|
VIRT |
Virtual memory |
|
SHR |
Shared memory |
📌 3. Using htop
Interactive version of top.
Install:
sudo apt install htop
Run:
htop
Features:
Colorful UI
Process tree
Easy sorting/filtering
4. Using vmstat
Command:
vmstat 1
Updates every second.
Important columns:
|
Column |
Meaning |
|
si |
Swap in |
|
so |
Swap out |
|
free |
Free memory |
|
us |
User CPU |
|
id |
Idle CPU |
Detecting Thrashing
High:
si
so
indicates excessive swapping.
5. /proc/meminfo
Command:
cat /proc/meminfo
Provides detailed memory statistics.
Useful fields:
MemAvailable
Cached
Buffers
SwapFree
6. Monitoring Specific Process Memory
Using ps
ps aux --sort=-%mem | head
Shows top memory-consuming processes.
Using pmap
pmap <PID>
Displays:
Memory map of process
7. Using smem
Better memory accounting.
Install:
sudo apt install smem
Run:
smem
Provides:
USS
PSS
RSS
📌 8. Historical Monitoring with sar
Install:
sudo apt install sysstat
View memory statistics:
sar -r
Useful for:
Performance analysis over time
9. Monitoring Swap Usage
Command:
swapon --show
or:
cat /proc/swaps
10. Detecting Memory Leaks
Signs:
Memory usage continuously increases
System slows over time
Swap usage rises
Tools:
valgrind
top
smem
Example:
valgrind ./program
Important Memory Metrics
|
Metric |
Meaning |
|
RSS |
Physical RAM used |
|
VSZ/VIRT |
Virtual memory size |
|
PSS |
Shared memory proportion |
|
Swap usage |
Disk-backed memory |
|
Page faults |
Missing pages loaded |
Understanding Linux Cache
Linux aggressively uses RAM for caching.
So:
High memory usage is often normal.
Cached memory can be reclaimed when needed.
Warning Signs
|
Symptom |
Possible Cause |
|
High swap activity |
Low RAM |
|
System freezes |
Thrashing |
|
OOM kills |
Memory exhaustion |
|
Constant page faults |
Insufficient working set |
Example Monitoring Workflow
Check free memory
↓
Identify heavy processes
↓
Check swap usage
↓
Inspect page faults/swapping
↓
Find leaks or bottlenecks
📌 Useful Real-Time Commands
Watch Memory Continuously
watch -n 1 free -h
Top Memory Processes
ps aux --sort=-rss | head
Page Fault Statistics
vmstat -s
Summary
Linux memory monitoring helps:
Optimize performance
Detect memory leaks
Prevent thrashing
Analyze swap activity
Core tools:
free
top
htop
vmstat
/proc/meminfo
sar
smem
are essential for system administration and debugging.
No comments:
Post a Comment