LinuxTutorial2 min read

How to Free Up Memory on Linux

Understand and manage Linux memory usage with free, sync, and drop_caches — plus tips on identifying memory-hungry processes before you run out of RAM.

Developer terminal on a laptop in low light

How Linux Uses Memory

Linux aggressively caches disk reads in RAM to speed up future access. That's why free often shows very little "free" memory even on a lightly-loaded system — most of it is used as cache. This is normal and intentional. The cache is released automatically when real applications need the memory.

free — Check Memory Usage

free -h            # human-readable (MB/GB)
free -m            # in megabytes
watch -n 2 free -h # refresh every 2 seconds

Sample output:

              total   used   free  shared  buff/cache  available
Mem:           15Gi   4.2Gi  2.1Gi  312Mi    9.2Gi       10Gi
Swap:           2Gi   0B     2Gi

The available column is the most useful — it shows how much memory applications can actually use right now (free + reclaimable cache).

Find Memory-Hungry Processes

ps aux --sort=-%mem | head -15      # top 15 by memory use
top                                  # interactive; press M to sort by memory
htop                                 # friendlier; F6 to sort by MEM%

Kill a Runaway Process

Once you identify the process consuming excessive memory:

kill 1234           # graceful stop
kill -9 1234        # force kill

Drop Page Cache (Use With Caution)

You can manually tell the kernel to release file cache. This is rarely necessary — the kernel handles it automatically — but it can help in specific server scenarios:

sync                              # flush pending disk writes first
sudo sh -c 'echo 1 > /proc/sys/vm/drop_caches'   # drop page cache
sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'   # drop all caches

Note: Dropping caches doesn't free memory used by running programs — only disk cache. Performance may temporarily decrease as the cache warms back up.

Check and Add Swap

Swap acts as overflow when RAM fills up. Check it:

swapon --show
cat /proc/swaps

Add a 2 GB swap file if you don't have one:

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Monitor Memory Over Time

vmstat 5           # print memory, CPU, I/O stats every 5 seconds
sar -r 1 10        # 10 memory snapshots, 1 second apart (needs sysstat)