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.
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: 15Gi 2.3Gi 9.6Gi 221Mi 3.3Gi 12Gi Swap: 2Gi 0B 2GiThe 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: 15Gi 2.3Gi 12.5Gi 221Mi 256Mi 14Gi Swap: 2Gi 0B 2GiA 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 ##### snipped ##### KiB Mem : 16333752 total, 2425848 used, 9692196 free, 2211320 buff/cache KiB Swap: 2097148 total, 0 used, 2097148 free ##### snipped ##### $ htop
Sort 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.
Comment anonymously. Login not required.
