Galle, LK--:--5/6 sites live

Linux Virtual Memory, Paging, Swap & The Out-of-Memory (OOM) Killer

පිටුව 06: Virtual Memory, Paging, Swap සහ Out-of-Memory (OOM) Killer මෙහෙයුම

RAM, Swap, Virtual Memory, 4KB Paging, Buffers/Cache කියන්නේ මොනවද? RAM එක පිරුණම Linux Kernel එකේ OOM Killer ක්‍රියාත්මක වන හැටි.

7 Sept 20264 min readSelf Guide · Stage 10#Memory#OOM#SRE Labs

Lab 06 of 20 · Memory & OOM · 5 hands-on activities · bilingual (English + සිංහල)

පිටුව 06: Virtual Memory, Paging, Swap සහ Out-of-Memory (OOM) Killer මෙහෙයුම

RAM, Swap, Virtual Memory, 4KB Paging, Buffers/Cache කියන්නේ මොනවද? RAM එක පිරුණම Linux Kernel එකේ OOM Killer ක්‍රියාත්මක වන හැටි.

💡 සරල සිංහලෙන්: RAM එක පිරුණම OOM Killer ඇවිත් Process මරලා දාන්නේ ඇයි?

ගිලෙන්න යන නැවක උපමාව:

නැවකට දරාගන්න පුළුවන් බර (RAM) උපරිමයට ආවම, නැව ගිලෙන්න ඔන්න මෙන්න තියෙද්දි කැප්ටන් (Linux Kernel) තීරණය කරනවා නැවේ තියෙන බරම බඩු මුට්ටියක් මුහුදට විසි කරන්න. එතකොට නැව බේරෙනවා!

Linux එකෙත් වෙන්නේ ඒකයි:
RAM එකයි Swap එකයි සම්පූර්ණයෙන්ම පිරිලා CPU එක හිරවෙන්න ආවම, Kernel එකේ ඉන්න OOM Killer ඇවිත් RAM එක වැඩිපුරම කන Process එක (උදා: Java, Python හරි Nginx හරි) එක ක්ෂණිකව මරලා දාලා මුළු Server එක Crash නොවී බේරගන්නවා. Jenkins එක හිටපු ගමන් වැහුනොත් dmesg | grep oom ගහලා බලන්නේ ඒකයි!

Architectural Foundation & Systems Diagram

Memory management is one of the most critical responsibilities of the Linux kernel. Applications never access physical RAM chips directly; they work in a Virtual Address Space divided into fixed-size Pages (typically 4 KB).

  • 🧠 RAM vs Swap Space — RAM is high-speed physical memory. Swap is emergency overflow space on disk. When RAM is exhausted, inactive memory pages are paged out to swap to keep the server alive.

  • 📦 Buffers & Page Cache — Linux aggressively uses unused RAM to cache recently read disk files. If an application requests memory, the kernel instantly frees cache. "Free RAM is wasted RAM!"

  • 🎯 The OOM Killer Heuristic — When physical RAM and swap are 100% full, the kernel invokes the Out-Of-Memory (OOM) Killer. It computes an oom_score for every process and terminates the biggest memory hog to rescue the OS!

Step-by-step practical lab

Step 01: List All Active System Processes

සිංහලෙන් — Server එකේ දුවන සියලුම Processes පරීක්ෂා කරමු.

Display a comprehensive snapshot of every process running on the system across all users, with CPU and memory consumption.

ExampleShell
ps aux
  • a : සියලුම පරිශීලකයින්ගේ processes පෙන්වයි.
  • u : User-friendly format එකෙන් CPU% සහ RAM% පෙන්වයි.
  • x : Terminal එකකට සම්බන්ධ නොවූ background daemons ද ඇතුළත් කරයි.

Expected output

Output
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1  22584 11420 ?        Ss   Sep14   0:18 /sbin/init
nginx     1042  1.3  0.2 145280 12480 ?        S    Sep14   4:12 nginx: worker process

SRE Tip: In the STAT column: R = Running, S = Sleeping, Z = Zombie (defunct), T = Stopped.

Step 02: Gracefully Terminate a Process via SIGTERM

සිංහලෙන් — Process එකක් ක්‍රමවත්ව නවතා දැමීමට Signal 15 (SIGTERM) යවමු.

Send signal 15 (SIGTERM) requesting the target process to flush its memory buffers, close file handles, and cleanly exit.

ExampleShell
kill -15 1042
  • -15 : SIGTERM: Application එකට දත්ත save කර ක්‍රමවත්ව exit වීමට ඉඩ දෙයි.

Expected output

Output
Signal SIGTERM (15) sent to PID 1042.

SRE Tip: Always try kill -15 first. Only use kill -9 (SIGKILL) if the process is completely hung, because SIGKILL does not allow buffer flushes.

Hands-on practice activities

Work through these yourself — tick them off as you go.

  • Activity 26: Inspect Physical RAM & Swap Allocation (Drill) — Display total, used, free, shared, buff/cache, and available memory in megabytes.
  • Activity 27: Read Kernel Memory Telemetry (Drill) — Query detailed virtual memory statistics directly from kernel instrumentation.
  • Activity 28: Search Kernel Ring Buffer for OOM Kill Events (Drill) — Search dmesg kernel logs for Out of Memory killer process terminations.
  • Activity 29: Differentiate 'Free' vs 'Available' Memory (Checkpoint) — Never alert on low 'Free' memory! Always alert on low 'Available' memory, which accounts for reclaimable page cache.
  • Activity 30: Linux Memory Page Size Quiz (Quiz) — What is the standard architecture page size for Linux on x86_64 systems? Answer: 4 KB (4096 bytes).

Keep going: ← Lab 05 · Lab 07 → · Track your progress in the Self Guide

ExerciseKnowledge check

When RAM is completely exhausted, what Linux kernel mechanism terminates processes to prevent panic? / RAM ඉවර වූ විට processes මරන්නේ කවුද?

Finished this one?

Part of Self Guide · Stage 10: SRE Labs I — the machine underneath. Progress is saved in this browser.

Guide