Cache Memory Clear In Linux



Got swappiness? Learn how to set appropriate swap settings in Linux, and clear your swap's contents.

The cache / buffer memory in Linux is nothing but a set of cache stored on the server which contains frequently visited pages. The cache configuration is done in a such a way that whenever any data is requested by RAM it will check the cache memory and serve the data from cache memory if available.

More Linux resources

  • Clear Linux / UNIX BASH Shell Command Line Cache / History Author: Vivek Gite Last updated: January 30, 2018 14 comments I just noticed tutorial about clearing DNS cache, and there are lots of commands stored in my bash shell cache.
  • At the gross level, on Xeon E5 v3 systems, reading an array that is 4x larger than the L3 cache size will clear nearly 100% of the prior data from the L1, L2, and L3 caches. This only requires process binding (e.g., 'taskset' or 'numactl -physcpubind' on Linux systems).

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?

Where is my cache on my computer

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.

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:

Cache Clear In Linux

There was a alert showing that the swap free space is low in a RHEL 5.6 box. I logged in and checked, the box has 13G RAM, but only 1G swap. First, this is not a good practice, as RedHat recommends 4G swap for RHEL5 with 4-16G RAM.

free -m
total used free shared buffers cached
Mem: 13788 13134 654 0 609 4948
-/+ buffers/cache: 7576 6212
Swap: 1023 898 125

Cache Memory Clear In Linux Operating System

Secondly, to fix it I need to increase the curret swap space, or add another swap file or partition. The process is pretty straight forward, here is a example:

1) Create a 512 M file: dd if=/dev/zero of=/home/swapfile bs=1024 count=524888
2) Setup the swap file: mkswap /home/swapfile
3) Add a new entry in the /etc/fstab: /home/swapfile swap swap defaults 0 0
4) Mount all swap defined in /etc/fstab: swapon -av or only mount the new one: swapon -v /home/swapfile

But I noticed that the cached RAM is high, I should be able to release that cache to increase the free RAM. Here is how to:

1) Ensure flush the cache back to disk first: sync
2) Tell kernel to drop or clean pagecache, dentries and inodes: echo 3 > /proc/sys/vm/drop_caches (this is just one time action. cat /proc/sys/vm/drop_caches tells you what the last action was taken).
3) Turn off swap to force it flush back to RAM: swapoff -av
4) Turn the swap back on: swapon -av

References:

Writing to file /proc/sys/vm/drop_caches instructs the kernel to drop or clean the page caches (pagecache), dentries (implemented through slab cache), and inodes (implemented through slab cache) and thus causing the memory to become reclaimed and available. Clean data cache pages are not freed by design. They can be easily reclaimed by the kernel if or when extra memory is needed, but in the meantime they contain useful data from disks that if needed again saves disk io.

Enabling drop_caches can cause deadlock if the system is under heavy memory and I/O load. It is not recommended to use this feature on system experiencing heavy load.

Red Hat Enterprise Linux 4.6 and 5.0 introduced /proc/sys/vm/drop_caches. There were issues in 4.6 through 5.2 releases that could cause deadlocks between kjournald and drop_pagecache if the system was under heavy io load. This issue persists within RHEL 4 releases but was fixed in RHEL 5.3 and later.

Before running the commands use sync to make sure dirty cache pages are flushed to disk first to maximize the number of data cache pages that can be reclaimed.

To free the pagecache only:
# echo 1 > /proc/sys/vm/drop_caches
To free dentries and inodes only:
# echo 2 > /proc/sys/vm/drop_caches
To free pagecache, dentries and inodes:
# echo 3 > /proc/sys/vm/drop_caches
Please note that there is no way to tune how the cache is dropped, how much is dropped, or when it is dropped.

Red Hat Enterprise Linux 5

How Do You Clear The Cache In Linux

Amount of installed RAMRecomended swap space
4GB or less2GB swap space
4GB – 16GB4GB swap space
16GB – 64GB8GB swap space
64GB – 256GB16GB swap space

Note: A swap space of at least 100GB is recommended for systems with over 140 logical processors.

Red Hat Enterprise Linux 6, Red Hat Enterprise Linux 7, Red Hat Enterprise Open Stack Platform 3 and Red Hat Enterprise Open Stack Platform 4

Amount of installed RAMReccomended swap spaceRecommended swap space if allowing for hibernation
2GB or lessTwice the installed RAM3 times the amount of RAM
> 2GB – 8GBThe same amount of RAM2 times the amount of RAM
> 8GB – 64GBAt least 4GB1.5 times the amount of RAM
> 64GB or moreAt least 4GBHibernation not recommended