Linux Clear Cache清除缓存

Linxu clear cache


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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值