Tag Archives: Linux Servers

Flush Memory Cache and Buffer Cache on Linux

Many times systems faced low memory issues of Linux systems running a while. The reason is that Linux uses so much memory for disk cache is because the RAM is wasted if it isn’t used. Cache is used to keep data to use frequently by the operating system. Reading data from cache if 1000’s time faster than reading data from hard drive.
It’s good for the os to get data from the cache in memory. But if any data not found in the cache, it reads from hard disk. So it’s no problem to flush cache memory. This article has details about how to Flush Memory Cache on Linux Server.

Clear Linux Memory Buffer Cache

There are three options available to flush the cache of Linux memory. Use one of below as per your requirements.

  • Free pagecache, dentries and inodes in cache memory
    • sync; echo 3 > /proc/sys/vm/drop_caches
  • Free dentries and inodes use following command
    • sync; echo 2 > /proc/sys/vm/drop_caches
  • Free pagecache only use following command
    • sync; echo 1 > /proc/sys/vm/drop_caches

Schedule Cron to Flush Cache Regularly

It’s a good idea to schedule following in crontab to automatically flush cache on the regular interval. Use ‘crontab -e’ command to edit cron on your system.

crontab -l

0 * * *  * sync; echo 3 > /proc/sys/vm/drop_caches

The above cron will execute on every hour and flushes the memory cache on your system.

Find Cache Memory uses in Linux

Use free command to find out cache memory uses by Linux system. Output of free command is like below

free -m

Sample Output

             total       used       free     shared    buffers     cached
Mem:         16050      15908        142          0        120      14953
-/+ buffers/cache:        834      15216
Swap:            0          0          0

Last column is showing cached memory ( 14953 MB) by system. -m option is used for showing memory details in MB’s.

Further Explanations

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.

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 PageCachedentries and inodes.

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.

Convert squid log file timestamps

When you work with a squid access log file you sometimes want to know when a site or resource was accessed. Squid does not store the date and time information for that in a human readable format.

It is stored as <unix timestamp>.<centisecond> so you can use a command like that to post-process to make it more readable for you:

cat access.log | perl -p -e 's/^([0-9]*)/"[".localtime($1)."]"/e'

Squid Proxy Server Error: TCP_MISS_ABORTED/000 0 GET HIER_DIRECT/2404: ipV6

Error Message:

TCP_MISS_ABORTED/000 0 GET http://www.google.com/ – HIER_DIRECT/2404:6800:4003:803::1011
7433 x.x.x.x TCP_MISS_ABORTED/000 0 GET http://www.facebook.com/ user HIER_DIRECT/2a03:2880:20:3f07:face:b00c:0:1
TCP_DENIED/407 4471 GET http://www.google.com/ – HIER_NONE/- text/html

Reason of the error:

Latest version of Squid support both ipv4 and ipv6 and by default dns queries will prefer ipv6.
If your system is not configured for ipv6 network, DNS queries will be pointed to ipv6 network and your proxy server cannot reach that network.

Solution:

Configure ipv6 properly and try pinging google.com
#ping6 ivp6.google.com

Otherwise you have to add the following line in the squid config to give first preference to ipv4.

#vim /etc/squid/squid.conf
dns_v4_first on
#service squid restart