High memory usage on Linux can slow interactive workloads, increase latency, and cause applications to compete more aggressively for RAM. Reclaiming memory from file caches, idle processes, and swap keeps systems responsive, especially on shared servers and virtual machines with limited resources.
The Linux kernel aggressively uses otherwise free RAM as page cache for files and filesystem metadata, exposing the current state through tools such as free, top, and htop. Cached memory is automatically released when applications need space, but explicit cache dropping through /proc/sys/vm/drop_caches and careful management of heavy processes and swap allows tighter control in constrained environments.
Manual cache clearing and process termination require root privileges and must be used with awareness of workload patterns. Excessive cache flushing can degrade performance by forcing repeated disk reads, disabling swap on a busy host can trigger out-of-memory kills, and killing critical daemons can interrupt services. Targeted cache clearing, cautious swap refresh, and verification commands minimise disruption on production systems while recovering memory.
Related: How to check memory usage in Linux
Related: How to check memory pressure in Linux
Steps to clear memory usage in Linux:
- Open a terminal with sudo privileges on the Linux host.
- Display current memory usage to determine existing RAM, cache, and swap utilisation.
$ free -h total used free shared buff/cache available Mem: 23Gi 1.2Gi 20Gi 13Mi 1.7Gi 21Gi Swap: 1.0Gi 0B 1.0GiThe buff/cache column shows memory that can be reclaimed if applications need more space.
- Clear the page cache to reclaim file-backed memory without touching filesystem metadata.
$ sudo sync $ echo 1 | sudo tee /proc/sys/vm/drop_caches 1
Value 1 for vm.drop_caches clears only the page cache; using tee with sudo ensures /proc/sys/vm/drop_caches is written with root privileges.
- Clear dentries and inodes when a workload has created or accessed many filesystem entries.
$ sudo sync $ echo 2 | sudo tee /proc/sys/vm/drop_caches 2
Value 2 for vm.drop_caches drops cached directory entries and inodes while leaving the page cache intact.
- Clear all cached memory, including page cache, dentries, and inodes, when a more aggressive cleanup is acceptable.
$ sudo sync $ echo 3 | sudo tee /proc/sys/vm/drop_caches 3
Value 3 clears page cache, dentries, and inodes; only one vm.drop_caches value is needed per cache clear, and aggressive clearing can temporarily reduce performance as caches are rebuilt from disk.
- Check memory usage again to confirm that cache usage has decreased and free memory has increased.
$ free -h total used free shared buff/cache available Mem: 23Gi 1.0Gi 22Gi 13Mi 389Mi 22Gi Swap: 1.0Gi 0B 1.0GiA lower buff/cache value or higher free and available values indicate that caches were successfully dropped.
- Start a process monitor such as top or htop to identify processes with high memory usage.
$ top -b -n 1 | head -n 12 top - 08:03:55 up 1 day, 18:54, 0 user, load average: 0.20, 0.20, 0.19 Tasks: 11 total, 1 running, 10 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.9 us, 0.0 sy, 0.0 ni, 99.1 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st MiB Mem : 23745.0 total, 22640.5 free, 1038.3 used, 391.3 buff/cache MiB Swap: 1024.0 total, 1024.0 free, 0.0 used. 22706.7 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 20928 11752 8988 S 0.0 0.0 0:01.41 systemd 23 root 19 -1 42740 12620 11028 S 0.0 0.1 0:00.18 systemd-j+ 171 message+ 20 0 9748 4648 3844 S 0.0 0.0 0:00.70 dbus-daem+ 174 root 20 0 17788 7812 6792 S 0.0 0.0 0:00.24 systemd-l+ 1426 systemd+ 20 0 90764 7200 6268 S 0.0 0.0 0:01.34 systemd-t+ $ htopSort by RES or MEM% to locate processes that reserve the most memory and note their PID values.
- Terminate selected memory-heavy processes using their PID values when reclaiming cache alone is insufficient.
$ sudo kill [PID] $ sudo kill -9 [PID]
Use kill with the default signal first and reserve kill -9 for processes that refuse to exit, because forced termination can interrupt in-flight work and leave temporary files or locks behind.
- Disable swap to clear its contents and force swapped-out pages back into RAM where possible.
$ sudo swapoff -a
Disabling swap on a heavily loaded system can trigger out-of-memory kills if available RAM is insufficient, so perform this action during low utilisation periods.
- Re-enable swap after clearing to restore additional virtual memory capacity.
$ sudo swapon -a
Confirm swap state with free -h or swapon --show if detailed information about swap devices is required.
- Reboot the system when a full reset of memory, swap, and caches is acceptable during a maintenance window.
$ sudo reboot
Rebooting stops all running processes and services, so schedule this action to avoid interrupting users or critical workloads.
- Monitor memory usage over time to confirm that cache, swap, and process behaviour remain stable after cleanup.
$ htop $ top
Interactive tools such as htop provide an ongoing view of CPU, RAM, swap, and per-process usage that is useful for capacity planning and spotting memory leaks.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
