Skip to main content

Lab 2.2 - Memory Management Analysis

Objectives​

By the end of this lab, you will be able to:

  • Analyze memory usage
  • Understand swap
  • Monitor memory by process
  • Identify memory leaks
  • Optimize memory usage

Prerequisites​

  • Functional Linux system
  • Root or sudo access
  • Open terminal

Exercise 1: Basic Memory Information​

Step 1: Overview​

# Complete memory information
free -h

# Detailed information
cat /proc/meminfo

# Simple view
free

Question: How much RAM do you have? How much is used?

Step 2: Detailed Analysis​

# View all information
cat /proc/meminfo | head -20

# Total memory
grep MemTotal /proc/meminfo

# Free memory
grep MemFree /proc/meminfo

# Available memory (different from free)
grep MemAvailable /proc/meminfo

Explanation:

  • MemTotal: Total RAM
  • MemFree: Completely free RAM
  • MemAvailable: RAM available for new processes (includes reclaimable cache)

Exercise 2: Swap​

Step 1: Swap Information​

# View swap
swapon --show

# Swap details
cat /proc/swaps

# Swap usage
free -h | grep Swap

Step 2: Create a Swap File (Optional)​

# Create a 1GB file for swap
sudo fallocate -l 1G /swapfile

# Secure permissions
sudo chmod 600 /swapfile

# Format as swap
sudo mkswap /swapfile

# Activate
sudo swapon /swapfile

# Verify
swapon --show

Step 3: Permanent Swap​

# Add to /etc/fstab
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

# Verify
cat /etc/fstab | grep swap

Exercise 3: Memory by Process​

Step 1: Top Consumers​

# Top 10 processes by memory
ps aux --sort=-%mem | head -11

# With details
ps aux --sort=-%mem -o pid,user,%mem,%cpu,cmd | head -11

Step 2: Memory of a Specific Process​

# Find a process (e.g., firefox)
ps aux | grep firefox

# View memory of a PID
cat /proc/<PID>/status | grep -E 'VmSize|VmRSS|VmData'

# Or with pmap
pmap <PID>

Explanation:

  • VmSize: Total virtual size
  • VmRSS: Physical RAM used
  • VmData: Data memory

Step 3: Analysis Script​

# Create a script
cat > mem-analysis.sh << 'EOF'
#!/bin/bash
echo "=== Top 5 Memory Processes ==="
ps aux --sort=-%mem | head -6
echo
echo "=== Memory Usage ==="
free -h
echo
echo "=== Swap ==="
swapon --show
EOF

chmod +x mem-analysis.sh
./mem-analysis.sh

Exercise 4: Real-Time Monitoring​

Step 1: vmstat​

# Statistics every second
vmstat 1

# 10 samples
vmstat 1 10

# Interpret columns:
# - swpd: Swap used
# - free: Free memory
# - buff: Buffer cache
# - cache: Page cache
# - si/so: Swap in/out

Step 2: sar (if installed)​

# Install sysstat
sudo apt install sysstat

# Memory statistics
sar -r 1 10

# History
sar -r

Step 3: Monitoring Script​

# Continuous monitoring
watch -n 1 'free -h && echo && ps aux --sort=-%mem | head -6'

Exercise 5: Memory Leak Detection​

Step 1: Continuous Monitoring​

# Create a monitoring script
cat > mem-watch.sh << 'EOF'
#!/bin/bash
while true; do
clear
echo "=== $(date) ==="
free -h
echo
echo "=== Top 5 Memory ==="
ps aux --sort=-%mem | head -6
sleep 2
done
EOF

chmod +x mem-watch.sh
# Run: ./mem-watch.sh

Step 2: Process Analysis​

# Track memory evolution of a process
watch -n 1 'ps -p <PID> -o pid,%mem,%cpu,rss,vsz,cmd'

Step 3: Alerts​

# Alert script if memory > 80%
cat > mem-alert.sh << 'EOF'
#!/bin/bash
THRESHOLD=80
MEM_USED=$(free | grep Mem | awk '{printf "%.0f", $3/$2 * 100}')
if [ "$MEM_USED" -gt "$THRESHOLD" ]; then
echo "ALERT: Memory at ${MEM_USED}%"
ps aux --sort=-%mem | head -6
fi
EOF

chmod +x mem-alert.sh
./mem-alert.sh

Exercise 6: Memory Optimization​

Step 1: Clear Cache (Caution!)​

# View cache
free -h

# Clear cache (if really necessary)
# CAUTION: May temporarily slow down the system
sudo sync
sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'

# Verify
free -h

Step 2: Adjust Swappiness​

# View current swappiness
cat /proc/sys/vm/swappiness

# Change temporarily (0-100, lower = less swap)
sudo sysctl vm.swappiness=10

# Permanent: /etc/sysctl.conf
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

Step 3: Memory Limits​

# View process limits
cat /proc/<PID>/limits | grep memory

# Limit with systemd
systemd-run --slice=my-slice --property=MemoryLimit=512M command

Verification​

Checklist​

Ensure you can:

  • View total and available memory usage
  • Analyze swap
  • Identify top memory-consuming processes
  • Monitor memory in real-time
  • Detect memory issues
  • Adjust memory settings

Reflection Questions​

  1. What is the difference between MemFree and MemAvailable?

    • MemFree: Completely free memory
    • MemAvailable: Available memory (includes reclaimable cache)
  2. When is swap used?

    • When physical RAM is full
    • The kernel moves inactive pages to swap
  3. Why can clearing the cache be dangerous?

    • Cache improves performance
    • Clearing it can temporarily slow down the system

Next Steps​

  • Lab 2.3: Advanced System Monitoring
  • Chapter 2.4: Virtual File System (VFS)
  • Chapter 2.5: Process Management

Lab created on: December 2024