Linux Clear Cache清除缓存

Linxu clear cache

文章目录

  • Linxu clear cache
    • 1.Intro
    • 2.Explain
    • 3.Questions
      • 3.1 what are pagecache, dentries, inodes?
      • 3.2 Is it good idea to auto clear RAM cache on production server?


1.Intro

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.

Every Linux System has three options to clear cache without interrupting any processes or services. You can drop cache as explained above without rebooting the System.

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.

1.Clear PageCache only.

$ sync
$ echo 1 > /proc/sys/vm/drop_caches

2.Clear dentries[目录项] and inodes[索引节点].

$ sync
$ echo 2 > /proc/sys/vm/drop_caches

3.Clear PageCache, dentries and inodes.

$ sync
$ echo 3 > /proc/sys/vm/drop_caches

The sync command will force the data in the cache to be written to the disk.



2.Explain

Command “sync” will flush the file system buffer.
As mentioned in kernel documentation, writing to drop_cache will clean cache without killing any application/service.
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 and inodes.



3.Questions

3.1 what are pagecache, dentries, inodes?

It appears you are working with memory caching of directory structures. An inode in your context is a data structure that represents a file. A dentries is a data structure that represents a directory. These structures could be used to build a memory cache that represents the file structure on a disk. To get a directly listing, the OS could go to the dentries–if the directory is there–list its contents (a series of inodes). If not there, go to the disk and read it into memory so that it can be used again.

The page cache could contain any memory mappings to blocks on disk. That could conceivably be buffered I/O, memory mapped files, paged areas of executables–anything that the OS could hold in memory from a file.

Your commands flush these buffers.

3.2 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 2am. Everyday at 2am 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.

你可能感兴趣的:(Linux系统使用,linux,缓存,后端,cache,清空缓存)