How To Clean Swap Memory In Linux



Technologists often talk about “cache” when discussing CPUs, disk drives, and computer systems. But what is it and why should you care?

A disk cache is RAM built into your hard disk. It evens out the flow of data between the relatively slow disk drive and the rest of the computer by accepting data faster than it can be written to or read from the disk. To help the cache do its job, the disk normally includes logic to analyze disk usage and pre-fetch data, that is likely to be needed soon.

We’ll use the mkswap command to make our file swap-consumable for the Linux kernel. Again as root, carefully type: mkswap /extraswap To turn on our swap file, we run swapon /extraswap. Now when we run swapon -s we should see our existing swap partition and our new swapfile. Also, the free command should show an increase in total swap space. Like any other operating system, GNU/Linux has implemented a memory management efficiently and even more than that. But if any process is eating away your memory and you want to clear it, Linux provides a way to flush or clear ram cache.

As long as the cache isn’t full, the disk drive’s data transfer rate is a good deal higher than its actual read/write speed.

Don’t confuse cache with the size of the disk drive or your system RAM. Cache is a separate entity; in fact, it’s normally memory chips that are installed in the casing that houses your disk drive. Often, when you read the specifications for a disk drive, it will include the amount of on-board cache.

This built-in cache is also different from setting aside a portion of the system RAM to use as a disk buffer, even though this is occasionally referred to as “disk cache” as well.

How To Clean Swap Memory In Linux Windows 10

The disk cache is important because it has a major impact on how well your system performs on>The benefit of cache only lasts until the cache is full. If the system trying to send or receive data faster than the disk can read or write it, the cache will fill up eventually and the disk’s performance will fall off significantly. But since disk activity on desktop systems is usually bursty, that is, it comes in bursts rather than as a steady stream of data, it is possible to put enough cache memory on a disk to stay ahead of the write requests most of the time.

The exact meaning of “most of the time” depends in large part on how much cache the disk has. Ideally the disk cache would be so large it would never fill up. Since RAM is relatively expensive compared to other components in a disk drive (prices vary based on the type of RAM used) it isn’t very practical.

Disks differ widely in the size of the cache. The standard today is a minimum of 2 Mb cache, although many”high-performance” commodity drives available at retail include 8 Mb of cache. A drive with 16 Mb will give significant performance improvements, but these are unusual and result in a more expensive disk drive.

All other things being equal, the disk with the most cache will give you the best performance. You’ll pay more, but your system will perform better.

How

Like any other operating system, GNU/Linux has implemented a memory management efficiently and even more than that. But if any process is eating away your memory and you want to clear it, Linux provides a way to flush or clear ram cache.

How to Clear Cache in Linux?

Add Swap In Linux

Every Linux System has three options to clear cache without interrupting any processes or services.

1. Clear PageCache only.

2. Clear dentries and inodes.

3. Clear PageCache, dentries and inodes.

Explanation of above command.

sync will flush the file system buffer. Command Separated by “;” run sequentially. The shell wait for each command to terminate before executing the next command in the sequence. As mentioned in kernel documentation, writing to drop_cache will clean cache without killing any application/service, command echo is doing the job of writing to file.

How To Clear Swap Memory In Linux Without Reboot

If you have to clear the disk cache, the first command is safest in enterprise and production as“...echo 1 > ….” will clear the PageCache only. It is not recommended to use third option above“...echo 3 >” in production until you know what you are doing, as it will clear PageCache, dentries andinodes.

Is it a good idea to free Buffer and Cache in Linux that might be used by Linux Kernel?

When you are applying various settings and want to check, if it is actually implemented specially on I/O-extensive benchmark, then you may need to clear buffer cache. You can drop cache as explained above without rebooting the System i.e., no downtime required.

Linux is designed in such a way that it looks into disk cache before looking onto the disk. If it finds the resource in the cache, then the request doesn’t reach the disk. If we clean the cache, the disk cache will be less useful as the OS will look for the resource on the disk.

Moreover it will also slow the system for a few seconds while the cache is cleaned and every resource required by OS is loaded again in the disk-cache.

Now we will be creating a shell script to auto clear RAM cache daily at 2PM via a cron scheduler task. Create a shell script clearcache.sh and add the following lines.

Set execute permission on the clearcache.sh file.

Now you may call the script whenever you required to clear ram cache.

Now set a cron to clear RAM cache everyday at 2PM. Open crontab for editing.

Append the below line, save and exit to run it at 2PM daily.

For more details on how to cron a job you may like to check our article on 11 Cron Scheduling Jobs.

Is it good idea to auto clear RAM cache on production server?

No! it is not. Think of a situation when you have scheduled the script to clear ram cache everyday at 2PM. Everyday at 2PM the script is executed and it flushes your RAM cache. One day for whatsoever reason, may be more than expected users are online on your website and seeking resource from your server.

At the same time scheduled script run and clears everything in cache. Now all the user are fetching data from disk. It will result in server crash and corrupt the database. So clear ram-cache only when required,and known your foot steps, else you are a Cargo Cult System Administrator.

How to Clear Swap Space in Linux?

If you want to clear Swap space, you may like to run the below command.

Also you may add above command to a cron script above, after understanding all the associated risk.

Now we will be combining both above commands into one single command to make a proper script to clear RAM Cache and Swap Space.

After testing both above command, we will run command “free -h” before and after running the script and will check cache.

How To Clean Swap Memory In Linux
Got swappiness? Learn how to set appropriate swap settings in Linux, and clear your swap's contents.

How To Clean Swap Memory In Linux Operating System

More Linux resources

Swap memory is usually a 'set it and forget it' type of affair. Most enterprise environments have swap built into the systems, and these memory caches are not manipulated unless there is an apparent lack of memory available or if a server crashes due to the OOM killer (out of memory) error. However, there is a niche situation that can cause an administrator to need to clear the system swap manually. If that is the situation that you find yourself in, you’ve come to the right place. This article is a discussion about this situation and the solution required.

Feeling Swappy?

Occasionally, a system uses a high percentage of swap memory even when there is RAM available for use. The culprit here is the ‘swappiness’ of the system. Yep, you read that right...swappiness. So now that you know the lingo, you're ready to explore what it means. Swappiness refers to the kernel parameter responsible for how much and how often that the system moves data from RAM to swap memory.

Swap

The default value for swappiness is 60; however, you can manually set it anywhere between 0-100. Small values cause little swapping to occur, whereas high values can cause very aggressive swapping. A value of zero causes no swapping at all to occur, so if you want to minimize swapping to its lowest possible value without turning it off, you should set it to at least one.

[ Free download: Advanced Linux commands cheat sheet. ]

If you wanted to change up the swappiness of your system, the procedure is very straight-forward. You can check your current swappiness setting by running the following command:

It should look something like this: