How to stop your Ubuntu laptop from freezing after it eats your whole RAM

tl;dr: run the bash script at the bottom.

I’m a software developer who has been using Ubuntu on his laptop for 20+ years now. I run heavy RAM-consuming tools — Docker, IDEs, 50+ browser tabs, multiple Claude agents working in parallel, etc. The machine is fairly old, but still good enough that I don’t want to buy a new one. One of my major problems was “sometimes, under pressure, it just freezes.” The only solution was a hard reset, which killed my current flow and desktop state. I looked into it a few years ago and ended up upgrading my RAM from 16 GB to 32 GB. That didn’t solve the issue, but it limited the freezes to an acceptable number. Now, after a few years, the freezes came back — so my frustration forced me, once again, to find a real fix. My coworkers keep telling me to buy a Mac, because it just works (which is not true 😄). I have to admit, I considered it carefully. I almost purchased one. Fortunately, the voice in my head said:

Why? It works. It just freezes sometimes.

And I found the solution.

My machine is a Dell G3 3579 — i7–8750H, 32 GB RAM, Nvidia GeForce GTX 1050 Ti (which has its own problems, probably worth another article 😸), Ubuntu 24.04.

First layer: systemd-oomd

It monitors PSI (Pressure Stall Information). When memory pressure grows above a threshold, it kills the most resource-consuming process before the system freezes.

Second layer: zram

It creates a swap device inside your RAM and stores compressed data there. It’s faster than regular disk swap, and thanks to compression, it lets you fit more data into your RAM than you otherwise could.

Third layer: sysctl tuning

Normally vm.swappiness=60 is calibrated for disk swap. For zram you want vm.swappiness=180 to make compression more aggressive, and vm.page-cluster=0 because read-ahead is useless when the swap already lives in RAM.

Heads-up

In Ubuntu 24.04 the zram-tools package leaves everything commented out in /etc/default/zramswap, which falls back to a hardcoded:

SIZE=256        # MB — useless for a 32 GB machine
ALGO=lz4        # faster, but worse compression than zstd

The script below fixes that as part of step 2.

The script

Save it as setup-memory.sh and run sudo bash setup-memory.sh. It’s idempotent — safe to re-run.

#!/bin/bash
# Aggressive memory management for Ubuntu 24.04 desktop with 32GB RAM.
# Installs systemd-oomd (proactive OOM killer) + zram swap (compressed RAM
# swap, 50% RAM, zstd) and tunes vm sysctls for zram-friendly behavior.

set -e

echo "=== 1/4 install systemd-oomd + zram-tools ==="
apt update
apt install -y systemd-oomd zram-tools

echo
echo "=== 2/4 configure zramswap (50% RAM, zstd) ==="
# Backup current config
cp /etc/default/zramswap /etc/default/zramswap.bak.$(date +%s) 2>/dev/null || true

# Override the broken defaults
sed -i 's/^#\?ALGO=.*/ALGO=zstd/' /etc/default/zramswap
sed -i 's/^#\?PERCENT=.*/PERCENT=50/' /etc/default/zramswap

echo "Current /etc/default/zramswap:"
grep -E "^[^#]" /etc/default/zramswap

echo
echo "=== 3/4 enable services (start + run on boot) ==="
systemctl enable --now systemd-oomd
systemctl enable --now zramswap
systemctl restart zramswap

echo
echo "=== 4/4 sysctl tuning ==="
cat > /etc/sysctl.d/99-zram-tuning.conf <<'EOF'
vm.swappiness=180
vm.page-cluster=0
vm.watermark_boost_factor=0
vm.watermark_scale_factor=125
EOF
sysctl --system

echo
echo "=== verify ==="
sleep 1

echo "--- services ---"
for s in systemd-oomd zramswap; do
  printf "  %-15s  active=%-8s enabled=%s\n" \
    "$s" \
    "$(systemctl is-active $s)" \
    "$(systemctl is-enabled $s)"
done

echo
echo "--- zram device ---"
swapon --show
for z in /sys/block/zram*; do
  [ -d "$z" ] || continue
  name=$(basename "$z")
  algo=$(cat "$z/comp_algorithm" 2>/dev/null | grep -oE '\[[^]]+\]')
  size=$(numfmt --to=iec --suffix=B < "$z/disksize" 2>/dev/null)
  echo "  $name: size=$size, algo=$algo"
done

echo
echo "--- free -h ---"
free -h

echo
echo "--- sysctl ---"
sysctl vm.swappiness vm.page-cluster vm.watermark_boost_factor vm.watermark_scale_factor

echo
echo "=== ready ==="

Enjoy your new laptop experience!

Comments (2)

T

Test

Test komentarza

↳ Reply

T

test2

test2

Add a comment

Comment will appear after moderation.

Subscribe to newsletter

New posts once a week, straight to your inbox. No spam, no tracking — unsubscribe anytime.