Physical And Swap Memory

 Understanding Physical Memory and Swap Memory is foundational for system performance tuning, debugging, and SRE/DevOps roles—crucial for FAANG-level interviews and production troubleshooting.


✅ 1. Fundamentals: Physical Memory vs Swap Memory

Concept

Description

Physical Memory (RAM)

Actual memory (volatile) used by OS and applications for runtime processing

Swap Memory

A portion of disk space used when RAM is full, acts as overflow (slower)


✅ 2. Physical Memory (RAM)

🔹 How It Works

  • RAM stores active processes, kernel code, file caches.

  • Fast, volatile memory.

  • When a process is launched, its binary and libraries are loaded into RAM.

  • Linux tries to use all available RAM for performance (file cache/buffers).

🔹 RAM Is Categorized Into:

Type

Description

Used

Currently in use by processes

Free

Not used (usually minimal)

Buffers

Metadata and block mapping

Cache

Recently used file data

Available

Estimation of memory available for new applications

🛠️ Check RAM:

free -h

vmstat -s

top



✅ 3. Swap Memory

🔹 Purpose

  • Acts as backup memory when RAM is exhausted.

  • Lives on disk: much slower (~100x) than RAM.

  • Prevents OOM (Out of Memory) kills.

🔹 Swap Use Cases

  • Systems with limited RAM

  • Heavy workloads temporarily exceeding RAM

  • Memory overcommit situations


🔹 Swap Can Be:

Type

Description

Swap Partition

Dedicated disk partition

Swap File

File on filesystem acting as swap

🛠️ Check Swap:

swapon --show

free -h

cat /proc/swaps


🛠️ Create Swap File:

sudo fallocate -l 2G /swapfile

sudo chmod 600 /swapfile

sudo mkswap /swapfile

sudo swapon /swapfile


🛠️ Make Persistent:
Add to /etc/fstab:

/swapfile none swap sw 0 0



✅ 4. Linux Memory Management Flow

User Process

    ↓

Used RAM

    ↓

File Cache / Buffers

    ↓

If RAM full → Swapping

    ↓

Swap Used



✅ 5. Key Kernel Parameters (sysctl)

Parameter

Description

vm.swappiness

Tendency to use swap (0 = avoid, 100 = aggressive)

vm.overcommit_memory

Controls memory allocation behavior

vm.dirty_ratio

Limit for dirty pages before flush to disk

🛠️ View & Set:

sysctl vm.swappiness

sysctl -w vm.swappiness=10



✅ 6. Performance & Debugging Tools

Tool

Purpose

free -h

Check memory + swap usage

top / htop

Real-time memory monitoring

vmstat

Virtual memory stats

dstat

Disk, mem, CPU stats

smem

Memory usage per process

slabtop

Kernel slab cache details


✅ 7. Hands-On Use Cases

🔹 1. Check available physical + swap memory

free -h


🔹 2. See if swap is being used

swapon --show

vmstat 1


🔹 3. Add a temporary 1GB swap

fallocate -l 1G /swaptemp

chmod 600 /swaptemp

mkswap /swaptemp

swapon /swaptemp


🔹 4. Disable swap temporarily

swapoff -a


🔹 5. Persist low swappiness value

echo "vm.swappiness=10" >> /etc/sysctl.conf

sysctl -p



🔥 FAANG-Level 20 Questions & Answers


🔸 Conceptual

  1. Q: What is the difference between physical memory and swap?
    A: RAM is fast, volatile memory. Swap is slower, disk-based fallback when RAM is full.

  2. Q: What is vm.swappiness, and how does it affect memory usage?
    A: It controls how aggressively the kernel uses swap. 0 = rarely, 100 = often.

  3. Q: Why does free -h show high memory usage even if you're idle?
    A: Linux uses free RAM for caching to improve performance.

  4. Q: When does Linux start using swap?
    A: When free RAM drops below a threshold and swappiness encourages swapping.

  5. Q: What does available memory in free -h represent?
    A: An estimate of how much memory is safely usable without swapping.


🔸 Debugging & System Management

  1. Q: How to find which processes are using the most memory?
    A: Use top, htop, or smem.

  2. Q: Why might swap be used even if RAM is available?
    A: Kernel may proactively swap based on swappiness or inactive memory.

  3. Q: How to disable swap without reboot?
    A: sudo swapoff -a

  4. Q: How to make a swap file persistent?
    A: Add entry in /etc/fstab.

  5. Q: How does overcommit memory work?
    A: Allows allocating more memory than physically available, based on heuristic or strict mode.


🔸 Performance & Design-Level

  1. Q: What are risks of high swappiness in web servers?
    A: Latency spikes due to disk I/O; better to keep active data in RAM.

  2. Q: How to identify excessive swapping?
    A: vmstat, high si/so values or top showing high swap usage.

  3. Q: How to reduce swapping behavior in containers?
    A: Use --memory-swappiness=0 with Docker or limit RAM tightly.

  4. Q: Should DB servers use swap?
    A: Avoid if possible—use tuned RAM sizing; swap causes latency.

  5. Q: How does NUMA affect memory allocation?
    A: Memory locality affects latency in multi-socket systems; tuning needed for performance.


🔸 Edge Cases & Advanced Internals

  1. Q: What is "OOM killer", and when does it trigger?
    A: If memory + swap are exhausted, kernel kills processes to free RAM.

  2. Q: How do you tune swap usage for high-performance workloads?
    A: Lower vm.swappiness, add more RAM, disable swap if safe.

  3. Q: Can you have swap without a partition?
    A: Yes, using a swap file.

  4. Q: What is memory ballooning in virtual machines?
    A: Dynamic memory resizing—can cause swap usage if host is oversubscribed.

  5. Q: How does memory fragmentation impact performance?
    A: Kernel may fail to allocate large contiguous memory blocks → can be tuned via hugepages.


✅ Summary Cheat Sheet

Command

Purpose

free -h

RAM and swap overview

vmstat 1

Memory behavior over time

swapon --show

Show active swap

mkswap /swapfile

Create swap file

sysctl vm.swappiness=10

Lower swap aggressiveness

top / htop

Real-time monitoring


Would you like:

  • A Bash script to monitor memory and auto-disable swap if overused?

  • A memory tuning checklist for Kubernetes/containers?

  • Or a live performance lab with memory exhaustion simulation?



Distributed by Gooyaabi Templates | Designed by OddThemes