How to Clear Ram Memory Cache, Swap Space, and Buffer on Linux Systems?
Overview
In Linux systems, the RAM is used as a storage space for data currently being accessed by the system. Cache memory, swap space, and buffer are storage spaces that are used for fast access and are essential to be considered in optimizing system performance. Over time, these spaces can accumulate unnecessary data, leading to a decrease in system performance.
Linux provides several methods to clear the data in cache memory, buffer cache, and swap space. Let us explore how to clear the cache in linux in this article.
How to Clear Cache Memory in Linux?
Before exploring methods on How to clear cache in linux, we have to understand what is a cache in Linux. Cache memory is a fast storage area located between the CPU and the main memory, that holds frequently accessed data, allowing for faster retrieval. It helps reduce the latency of accessing data from the main memory. There are different levels of cache memory such as,
- L1 cache is the smallest and the fastest cache, located closest to the CPU.
- L2 cache is larger but slightly slower than the L1 cache and acts as a backup for the L1 cache. Some systems also have L3 or even L4 levels of caches.
When the CPU requests any data, the cache checks if the required information is present in any of its cache storage. If the data is found in the cache, it is called a cache hit. If the data is not found, it is accessed from the slower main memory.
For clearing cache memory in Linux, we can use the /proc/sys/vm/drop_caches file, which is a special file in the proc filesystem of Linux systems. It allows users to control and manage the kernel's behavior related to cache management. When a value is written to this file, the system triggers the clearing of various cache components in the system. There are different options for clearing the cache using this method. We can use the free -h command to get the status of the memory in Linux.
Clearing Pagecache Only
In context with How to clear cache in Linux, we should also concern about Pagecache which holds pages of files that are read from or written to disk. Clearing the page cache frees up memory and ensures that subsequent file operations will fetch data directly from the disk. The value 1, written to the drop_caches file corresponds to clearing the page cache. To clear the page cache, you can use the following command,
- The sudo allows you to execute the subsequent command as a root user.
- The sh command is a shell interpreter that allows the execution of commands. The -c option allows you to specify the command to be executed as the argument.
- The echo command is used to display text or values to the terminal. In this case, it is used to output the value 1. The > operator redirects the output of the echo command and writes it to the /proc/sys/vm/drop_caches file.
The Linux kernel gets the value of 1 and initiates the clearing process. After clearing the cache, the output shows a difference in buff/cache.
Clearing Inodes and Dentries
To clear both inodes and dentries, we can write the value 2 to the drop_caches file. The following command shows this,
Clearing Inodes, Dentries, and Pagecaches
We can write the value 3 to the drop_caches file for clearing all three caches. Execute the command to clear all three caches,
You may ask the question why we need the use of sh -c and why not put the echo command directly. This is because the sudo command only grants elevated privileges to the immediate command. In our case only echo gets the sudo privileges, but not to the output redirection (>). Therefore it is essential to use the sh -c to execute the command.
How to Clear Buffer Cache in Linux?
In context to How to clear cache in Linux, Buffer cache stores disk blocks that have been recently accessed or modified. It acts as an intermediate layer between the filesystem and the disk, optimizing read and write operations. When data is read from or written to the disk, the buffer cache stores a copy of the data in memory, allowing subsequent operations to be performed more quickly. The buffer cache also helps in maintaining data integrity.
To clear the buffer cache in Linux, you can perform the following steps:
-
Open a terminal or shell session.
-
Flush the disk buffer cache by executing the following command,
This command is used to synchronize the cache data with the disk. It forces the operating system to flush the contents of the memory cache to the disk, ensuring that all pending modifications are immediately written, but it doesn't clear the cache. It helps to prevent data loss or corruption by synchronizing the data in memory with the physical storage devices.
-
Clear the buffer cache by dropping all clean entries from the cache using the command,
This command initiates the clearing process to clean the cache.
How to Clear Swap Space in Linux?
In context with How to clear cache in Linux, Swap space is a dedicated portion of the hard disk used to serve as an extension of physical memory when it becomes insufficient. When the RAM is fully utilized, the operating system starts moving inactive or less frequently used pages of memory to the swap space. This process is known as swapping or paging out. We can also swap out the pages of memory back to RAM.
Clearing the swap space in Linux involves disabling and re-enabling the swap partition. Follow the steps below to clear the swap space in Linux,
-
Disable swap by executing the command,
- The swapoff command is used to deactivate or disable swap space.
- The -a option is used to disable all swap partitions/files currently active on the system.
After running this command, the swap space is disabled, and the system will no longer utilize it for swapping memory pages.
-
Clear the swap file by using the command,
- The dd command is called a data duplicator and is commonly used for low-level copying and conversion of data. In this command, it is used to write a data file.
- The if flag specifies the input file for the dd command. The /dev/zero is a special file that provides an infinite sequence of null bytes (zeros) when read from.
- The of flag specifies the output file for the dd command. Here, /swapfile is the file path where the swap file will be created.
- The bs flag sets the block size for data transfer. In this command, 1M denotes a block size of 1 megabyte.
- The count flag determines the number of blocks to be copied. In this case, 4096 indicates that 4096 blocks, each with a size of 1 megabyte, will be written to the output file.
After this command, a swap file named swapfile will be created at the location, /swapfile with a size of 4096 megabytes or 4 gigabytes.
-
Recreate the swap file by entering the following commands,
The mkswap command is used to create swap space on a file or a device. In the above command, we create a swap file in /swapfile. After running the command, the /swapfile location is formatted as a swap area and is prepared to be used as virtual memory by the operating system.
The swapon command is used to enable and activate a swap area or file. We are activating the swap file created at /swapfile. After running this command, the operating system starts using the swap file as additional virtual memory
The output shows a difference in the Swap row after running the commands.
FAQs
Q. Why is it necessary to clear cache memory, buffer cache, and swap space?
A. Clearing these resources periodically can help free up system resources, enhance performance, and ensure efficient memory utilization.
Q. Is clearing the cache memory and buffer cache safe?
A. Yes, it is safe to clear the cache memory and buffer cache. The data being cleared is non-essential and can be reconstructed if required.
Q. Why do we need sudo permission to clear the cache in Linux?
A. The cache memory and buffer cache are important components of the operating system's memory management and may also contain important data. Therefore we need proper authorization to clear them.
Conclusion
- Optimizing system performance on Linux systems involves managing cache memory, buffer cache, and swap space.
- The main answer to How to clear cache in Linux is the /proc/sys/vm/drop_caches file which acts as a trigger for clearing cache Memory in Linux and it clears cache based on the number written to the file.
- The sync command is used for synchronization of the cache data with the disk before clearing the buffer Cache in Linux.
- Clearing the swap space in Linux involves disabling, clearing, and re-enabling the swap partition.
- Regularly clearing cache memory, buffer cache, and swap space will help maintain the overall health and efficiency of your Linux system.