Are you facing a performance issue and you suspect it might be related to cache usage? High cache usage should not normally cause performance issues, but it might be the root cause in some rare cases.

 

What is Memory Cache?

In order to speed operations and reduce disk I/O, the kernel usually does as much caching as it has memory By design, pages containing cached data can be repurposed on-demand for other uses (e.g., apps) Repurposing memory for use in this way is no slower than claiming pristine untouched pages.

 

What is the purpose of /proc/sys/vm/drop_caches?

Writing to /proc/sys/vm/drop_caches allows one to request the kernel immediately drop as much clean cached data as possible. This will usually result in some memory becoming more obviously available; however, under normal circumstances, this should not be necessary.

 

How to clear the Memory Cache using /proc/sys/vm/drop_caches?

Writing the appropriate value to the file /proc/sys/vm/drop_caches causes the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free.

 

1. In order to clear PageCache only run:

# sync; echo 1 > /proc/sys/vm/drop_caches

 

2. In order to clear dentries (Also called as Directory Cache) and inodes run:

# sync; echo 2 > /proc/sys/vm/drop_caches

 

3. In order to clear PageCache, dentries and inodes run:

# sync; echo 3 > /proc/sys/vm/drop_caches

 

Running sync writes out dirty pages to disks. Normally dirty pages are the memory in use, so they are not available for freeing. So, running sync can help the ensuing drop operations to free more memory.

 

Page cache is memory held after reading files. Linux kernel prefers to keep unused page cache assuming files being read once will most likely to be read again in the near future, hence avoiding the performance impact on disk IO.

 

dentry and inode_cache are memory held after reading directory/file attributes, such as open() and stat(). dentry is common across all file systems, but inode_cache is on a per-file-system basis. Linux kernel prefers to keep this information assuming it will be needed again in the near future, hence avoiding disk IO.

 

Note: Starting with the sync command as shown in the above 3 commands is optional. The sync command allows the kernel write as many dirty cache pages to disk as it can (to maximize the number of data cache pages that can be dropped)

 

How to clear the Memory Cache using sysctl

 

You can also Trigger cache-dropping by using sysctl -w vm.drop_caches=[number] command.

 

1. To free pagecache, dentries and inodes, use the below command.

sysctl -w vm.drop_caches=3

 

2. To free dentries and inodes only, use the below command.

sysctl -w vm.drop_caches=2 

 

3. To free the pagecache only, use the below command.

sysctl -w vm.drop_caches=1

 

Note: Using vm.drop_caches can cause a deadlock if the system is under heavy memory and I/O load!!!

 

“Clean” cached data is eligible for dropping. “Dirty” cached data needs to be written somewhere. Using vm.drop_caches will never trigger the kernel to drop dirty cache.

Byla tato odpověď nápomocná? 0 Uživatelům pomohlo (0 Hlasů)