Memory usage in Linux systems can gradually increase as more processes run, leading to reduced performance over time. Clearing cached memory can help free up system resources without impacting active processes. Linux manages memory efficiently by caching data to speed up operations, but at times, manually clearing memory might be necessary to optimize performance, especially on systems with limited RAM.
The Linux kernel uses cached memory to store frequently accessed data, reducing the need to retrieve information from disk. However, when system memory is fully utilized, you might want to clear the cache to make room for new processes or to improve system responsiveness. Linux provides several methods to clear memory usage, which can be done using simple terminal commands.
This article explains the different methods to clear cached memory, kill unnecessary processes, and improve overall system performance. Each method is safe to use and does not impact active processes or services unless specified.
Steps to clear memory usage in Linux:
- Open the terminal on your Linux system.
- Display the current memory usage to review cached memory and buffers.
$ free -h total used free shared buff/cache available Mem: 15Gi 2.3Gi 9.6Gi 221Mi 3.3Gi 12Gi Swap: 2.0Gi 0B 2.0Gi
Check the values under buff/cache to see how much memory is being used for caching.
- Clear the page cache.
$ sudo sync; echo 1 > /proc/sys/vm/drop_caches
The sync command ensures all data is written to disk before clearing the cache. The drop_caches command with `1` clears only the page cache.
- Clear the dentries and inodes to free up additional memory.
$ sudo sync; echo 2 > /proc/sys/vm/drop_caches
Using `2` with drop_caches clears both the page cache and dentries/inodes, which store filesystem objects.
- Clear all cached memory, including page cache, dentries, and inodes.
$ sudo sync; echo 3 > /proc/sys/vm/drop_caches
Use this command carefully as it clears all types of cache. This can momentarily reduce system performance while the cache is repopulated.
- Check the memory usage again to confirm the cache has been cleared.
$ free -h total used free shared buff/cache available Mem: 15Gi 2.3Gi 12.5Gi 221Mi 256Mi 14Gi Swap: 2.0Gi 0B 2.0Gi
- Kill processes consuming excessive memory using the top or htop command to identify resource-hungry processes.
$ top $ htop
Use top or htop to identify processes with high memory usage. Press `q` to exit the top command. Press `F10` to exit htop.
- Terminate memory-intensive processes by using the kill command.
$ sudo kill -9 [PID]
Replace `[PID]` with the Process ID of the memory-hogging process found in the previous step. Use this command cautiously, as it forcefully stops the process.
- Free up swap memory by disabling and re-enabling swap.
$ sudo swapoff -a $ sudo swapon -a
This command disables and re-enables swap, which clears swap memory usage.
- If necessary, reboot the system to reset all memory usage and cache.
$ sudo reboot
Rebooting the system clears all memory, cache, and processes. This should be done as a last resort or during scheduled downtime to avoid disrupting services.
- Use the htop or top command to monitor memory usage in real-time.
$ htop $ top
These commands display real-time memory and process information, allowing you to monitor the effects of clearing memory and killing processes.
Mohd Shakir Zakaria is an experienced cloud architect with a strong development and open-source advocacy background. He boasts multiple certifications in AWS, Red Hat, VMware, ITIL, and Linux, underscoring his expertise in cloud architecture and system administration.
Comment anonymously. Login not required.