🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

Linux Swap Analysis with Python: Find Swap Consumers, Tune Swappiness (Free CLI Tool)

Linux Swap Analysis with Python: Find Swap Consumers, Tune Swappiness (Free CLI Tool)

Understanding Linux Swap

Swap space is an extension of physical RAM that the Linux kernel uses to store memory pages that are not actively being used. When physical memory runs low, the kernel moves less-frequently-accessed pages to swap, freeing RAM for active processes. While swap prevents out-of-memory (OOM) kills, excessive swap usage dramatically degrades performance because disk I/O is orders of magnitude slower than RAM access.

The key question is not whether you are using swap (some swap usage is normal), but which processes are using it and whether the swap usage indicates a memory pressure problem. The standard free -h command only shows total swap usage — it does not tell you which processes are responsible or whether your swappiness setting is appropriate.

dargslan-swap-analyzer provides deep swap analysis: per-process swap usage from /proc, swap device details, memory pressure metrics, swappiness evaluation, and actionable optimization recommendations.

Install dargslan-swap-analyzer

pip install dargslan-swap-analyzer

Zero dependencies. Reads directly from /proc/meminfo, /proc/swaps, /proc/[pid]/status, and /proc/pressure/memory.

CLI Usage

# Full swap analysis report
dargslan-swap report

# Quick swap summary
dargslan-swap info

# Per-process swap usage (top consumers)
dargslan-swap processes

# Swap partitions and files
dargslan-swap devices

# Memory pressure (PSI) metrics
dargslan-swap pressure

# Issues and recommendations
dargslan-swap issues

# JSON output
dargslan-swap json

Python API

from dargslan_swap_analyzer import SwapAnalyzer

sa = SwapAnalyzer()

# Overall swap info
info = sa.get_swap_info()
print(f"Swap: {info[\"used_human\"]}/{info[\"total_human\"]} ({info[\"usage_percent\"]}%)")

# vm.swappiness value
swappiness = sa.get_swappiness()
print(f"Swappiness: {swappiness}")

# Top swap-consuming processes
for proc in sa.get_process_swap()[:10]:
    print(f"PID {proc[\"pid\"]}: {proc[\"name\"]} using {proc[\"swap_human\"]}")

# Swap devices
for dev in sa.get_swap_devices():
    print(f"{dev[\"device\"]}: {dev[\"used_human\"]}/{dev[\"size_human\"]}")

# Memory pressure
pressure = sa.get_pressure()
print(f"Memory pressure: {pressure}")

# Recommendations
issues = sa.audit()
for i in issues:
    print(f"[{i[\"severity\"]}] {i[\"message\"]}")

Swappiness Tuning Guide

The vm.swappiness parameter (0-200, default 60) controls how aggressively the kernel swaps memory pages:

  • Desktop (60) — Default value, balanced between RAM and swap
  • Server (10-30) — Prefer keeping data in RAM, only swap when necessary
  • Database server (1-10) — Minimize swap to keep database buffers in RAM
  • 0 — Do not swap unless absolutely necessary (OOM risk)
# Check current value
cat /proc/sys/vm/swappiness

# Set temporarily
sudo sysctl vm.swappiness=10

# Set permanently
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p

Download the Swap Analysis Cheat Sheet

Get our Swap Analysis & Optimization Cheat Sheet — covering swap configuration, swappiness tuning, and swap file management.

Related Tools

See all Linux performance Python tools at dargslan.com. Our Linux performance tuning eBooks cover memory management, swap optimization, and kernel tuning.

Share this article:
Dargslan Editorial Team (Dargslan)
About the Author

Dargslan Editorial Team (Dargslan)

Collective of Software Developers, System Administrators, DevOps Engineers, and IT Authors

Dargslan is an independent technology publishing collective formed by experienced software developers, system administrators, and IT specialists.

The Dargslan editorial team works collaboratively to create practical, hands-on technology books focused on real-world use cases. Each publication is developed, reviewed, and...

Programming Languages Linux Administration Web Development Cybersecurity Networking

Stay Updated

Subscribe to our newsletter for the latest tutorials, tips, and exclusive offers.