When opening multiple Chrome tabs alongside my local dev server, my Ubuntu machine regularly froze or stuttered. Swapping between tabs took seconds, and terminal input lagged.
Here is how I traced the issue to swap thrashing and fixed it without buying extra RAM.
The Problem
My laptop has 8 GB of RAM. Under daily development load, Chrome caused severe system lag.
Checking system memory showed heavy pressure:
free -h
total used free shared buff/cache available
Mem: 7.6Gi 5.6Gi 686Mi 1.2Gi 3.0Gi 2.1Gi
Swap: 4.0Gi 697Mi 3.3Gi
Out of 7.6 GiB usable RAM, 5.6 GiB was in use, leaving only 2.1 GiB available. The system pushed 697 MiB into disk swap.
Checking top memory processes revealed the culprit:
ps -eo pid,user,%mem,rss,comm --sort=-%mem | head -n 10
PID USER %MEM RSS COMMAND
4489 coder 8.3 663660 chrome
4868 coder 7.5 604032 chrome
5003 coder 7.3 586812 chrome
127211 coder 4.6 368332 chrome
4933 coder 4.4 357628 chrome
Chrome processes consumed 37%+ (~3 GB) of RAM. Combined with 1.2 GiB in shared memory (tmpfs), Ubuntu was forced to write memory pages to slow disk swap, causing UI freezes (swap thrashing).
Prerequisites
- Ubuntu 20.04 / 22.04 / 24.04 on an 8 GB RAM setup.
- Terminal access with
sudoprivileges.
Step-by-Step Fix
Step 1: Lower Swappiness
Ubuntu defaults to vm.swappiness=60, which swaps memory pages to disk too aggressively. Lowering it to 10 forces the kernel to prefer physical RAM.
- Apply temporarily:
sudo sysctl vm.swappiness=10
- Make it permanent:
sudo nano /etc/sysctl.conf
Add to the bottom:
vm.swappiness=10
- Apply changes:
sudo sysctl -p
Step 2: Set Up ZRAM (Compressed RAM Swap)
Instead of swapping to a slow SSD/HDD, ZRAM creates a compressed swap space directly in physical RAM (10x-20x faster than disk swap).
- Install
zram-tools:
sudo apt update && sudo apt install zram-tools -y
- Edit the config file:
sudo nano /etc/default/zramswap
- Configure allocation and compression algorithm:
ALGO=zstd
PERCENT=50
(Note: The configuration key in Ubuntu is ALGO, not ALGORITHM).
- Restart the service:
sudo service zramswap restart
Step 3: Optimize Chrome Settings
- Open Chrome and go to
chrome://settings/performance. - Turn Memory Saver to On (suspends inactive background tabs).
- Go to
chrome://settings/systemand confirm Use graphics acceleration when available is turned On (offloads rendering to GPU).
Step 4: Install btop for Real-Time Monitoring
To track RAM, ZRAM, and process trees visually without complex CLI parameters:
- Install
btop:
sudo apt install btop -y
- Launch
btop:
btop
- Press
minsidebtopto focus on RAM and swap compression in real time.
Step 5: Flush Slow Disk Swap
Once memory pressure drops, clear old swap data sitting on disk back into RAM:
sudo swapoff -a && sudo swapon -a
Warning: Check
free -hfirst! Only runswapoffif Available RAM is larger than Used Swap (e.g., >1.5 GB available), or the OS will run out of memory and freeze.
Common Pitfall & Solution
System freezes when running swapoff -a
- Cause: Physical RAM is too full to absorb swapped-out data.
- Fix: Kill heavy memory hogs first before resetting swap:
# Find heavy process IDs
ps -eo pid,user,%mem,rss,comm --sort=-%mem | head -n 5
# Terminate top memory hogs
kill -9 <PID_1> <PID_2>
# Confirm available memory increased, then reset swap
sudo swapoff -a && sudo swapon -a
Verification
- Verify ZRAM status:
zramctl
Expected Output:
NAME ALGORITHM DISKSIZE DATA COMPR TOTAL STYPE PRIORITY
/dev/zram0 zstd 3.8G 4K 59B 12K partition 100
-
Verify live memory metrics:
Run
btopor checkfree -h. You should seeSwap Usedremain near 0 MB or managed entirely by ZRAM (/dev/zram0).
Key Takeaways
- Available RAM > Free RAM: Ignore "Free" memory in Linux; "Available" is what determines whether your apps will lag.
- ZRAM is essential for 8GB dev setups: Compressing RAM in-memory eliminates disk I/O bottlenecks without requiring hardware upgrades.
-
Default swappiness is too high: Dropping
vm.swappinessfrom60to10stops premature disk swapping. -
Shared memory (
tmpfs) locks RAM: Always clean unused containers (docker system prune) and temporary files (/tmp) to free shared RAM allocations.
Top comments (0)