DEV Community

Cover image for How I Fixed Ubuntu Freezes and High Swap Usage on an 8GB RAM Laptop
Md Asaduzzaman
Md Asaduzzaman

Posted on

How I Fixed Ubuntu Freezes and High Swap Usage on an 8GB RAM Laptop

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
Enter fullscreen mode Exit fullscreen mode
               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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
    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
Enter fullscreen mode Exit fullscreen mode

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 sudo privileges.

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.

  1. Apply temporarily:
   sudo sysctl vm.swappiness=10
Enter fullscreen mode Exit fullscreen mode
  1. Make it permanent:
   sudo nano /etc/sysctl.conf
Enter fullscreen mode Exit fullscreen mode

Add to the bottom:

   vm.swappiness=10
Enter fullscreen mode Exit fullscreen mode
  1. Apply changes:
   sudo sysctl -p
Enter fullscreen mode Exit fullscreen mode

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).

  1. Install zram-tools:
   sudo apt update && sudo apt install zram-tools -y
Enter fullscreen mode Exit fullscreen mode
  1. Edit the config file:
   sudo nano /etc/default/zramswap
Enter fullscreen mode Exit fullscreen mode
  1. Configure allocation and compression algorithm:
   ALGO=zstd
   PERCENT=50
Enter fullscreen mode Exit fullscreen mode

(Note: The configuration key in Ubuntu is ALGO, not ALGORITHM).

  1. Restart the service:
   sudo service zramswap restart
Enter fullscreen mode Exit fullscreen mode

Step 3: Optimize Chrome Settings

  1. Open Chrome and go to chrome://settings/performance.
  2. Turn Memory Saver to On (suspends inactive background tabs).
  3. Go to chrome://settings/system and 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:

  1. Install btop:
   sudo apt install btop -y
Enter fullscreen mode Exit fullscreen mode
  1. Launch btop:
   btop
Enter fullscreen mode Exit fullscreen mode
  1. Press m inside btop to 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
Enter fullscreen mode Exit fullscreen mode

Warning: Check free -h first! Only run swapoff if 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
Enter fullscreen mode Exit fullscreen mode

Verification

  1. Verify ZRAM status:
   zramctl
Enter fullscreen mode Exit fullscreen mode

Expected Output:

   NAME       ALGORITHM DISKSIZE DATA COMPR TOTAL STYPE      PRIORITY
   /dev/zram0 zstd          3.8G   4K    59B    12K partition       100
Enter fullscreen mode Exit fullscreen mode
  1. Verify live memory metrics: Run btop or check free -h. You should see Swap Used remain 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.swappiness from 60 to 10 stops 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)