Linux下清理内存和Cache方法 /proc/sys/vm/drop_caches

本文详细介绍在Linux系统中如何通过使用echo3>/proc/sys/vm/drop_caches命令来有效释放缓存,解决因频繁文件访问导致的Cache占用过高问题。文中提供了清理内存和Cache的具体方法,包括如何运行sync命令确保缓存数据同步到硬盘,以及如何通过修改drop_caches参数来清理不同类型的数据缓存。同时,文章还分享了调整sysctl.conf配置文件的策略,以避免内存持续增长,帮助读者掌握Linux系统资源管理的实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通常在嵌入式系统开发中会使用这样一条命令来释放缓存:

"echo 3 > /proc/sys/vm/drop_caches"

Linux下清理内存和Cache方法 /proc/sys/vm/drop_caches

频繁的文件访问会导致系统的Cache使用量大增

$ free -m
total used free shared buffers cached
Mem: 3955 3926 28 0 55 3459
-/+ buffers/cache: 411 3544
Swap: 5726 0 5726

free内存减少到几十兆,系统运行缓慢

运行sync将dirty的内容写回硬盘
$sync

通过修改proc系统的drop_caches清理free的cache
$echo 3 > /proc/sys/vm/drop_caches

drop_caches的详细文档如下:
Writing to this will cause the kernel to drop clean caches, dentries and inodes from memory, causing that memory to become free.
To free pagecache:

  • echo 1 > /proc/sys/vm/drop_caches
    To free dentries and inodes:
  • echo 2 > /proc/sys/vm/drop_caches
    To free pagecache, dentries and inodes:
  • echo 3 > /proc/sys/vm/drop_caches
    As this is a non-destructive operation, and dirty objects are notfreeable, the user should run “sync” first in order to make sure allcached objects are freed.
    This tunable was added in 2.6.16.

修改/etc/sysctl.conf 添加如下选项后就不会内存持续增加
vm.dirty_ratio = 1
vm.dirty_background_ratio=1
vm.dirty_writeback_centisecs=2
vm.dirty_expire_centisecs=3
vm.drop_caches=3
vm.swappiness =100
vm.vfs_cache_pressure=163
vm.overcommit_memory=2
vm.lowmem_reserve_ratio=32 32 8
kern.maxvnodes=3

上面的设置比较粗暴,使cache的作用基本无法发挥。需要根据机器的状况进行适当的调节寻找最佳的折衷。

### 关于 `drop_caches` 的使用方法及效果 在 Linux 系统中,`drop_caches` 是一种用于手动清除内存缓存的方法。它允许用户通过写入特定数值到 `/proc/sys/vm/drop_caches` 文件来释放页面缓存、目录项缓存以及 inode 缓存。这种方法对于测试性能调优非常实用。 以下是具体的使用方式及其对应的效果: #### 使用方法 可以通过以下命令向 `/proc/sys/vm/drop_caches` 写入不同的值来实现不同类型的缓存清理: - **清理 Page Cache**: ```bash echo 1 > /proc/sys/vm/drop_caches ``` 这一操作会删除干净的页缓存 (Clean PageCache),而不影响其他类型的缓存[^3]。 - **清理 Reclaimable Slab 对象**: ```bash echo 2 > /proc/sys/vm/drop_caches ``` 此命令专门针对可回收的 slab 对象进行清理,这些对象通常由内核动态分配并管理。 - **同时清理 Page Cache Reclaimable Slab 对象**: ```bash echo 3 > /proc/sys/vm/drop_caches ``` 执行此命令后,既会清理干净的页缓存也会清理 reclaimable slab 对象,从而达到更全面的缓存释放目的。 为了确保所有未同步的数据被写入磁盘,在执行上述任何一条命令前建议先运行 `sync` 命令: ```bash sync ``` 这一步骤能够减少因脏数据尚未落盘而导致的信息丢失风险,并提高缓存清理的安全性有效性。 #### 效果说明 当成功完成以上任一项操作之后,相应的缓存会被立即从内存中移除,进而使得这部分内存变为可用状态。需要注意的是,这种做法是非破坏性的——也就是说,只有那些已经标记为“清洁”的资源才会受到影响;而仍然处于修改状态(Dirty Objects)的内容则不会受到波及[^4]。 此外值得注意的一点是,尽管该功能可以帮助我们更好地控制系统的内存占用情况,但在实际生产环境中频繁或者不当使用可能会带来一定的负面影响,比如短暂增加硬盘I/O负载等问题。 ```python def clear_cache(option=3): import os try: # Ensure all pending writes are completed before clearing cache. os.system('sync') # Clear specified type of cache based on option value. command = f'echo {option} > /proc/sys/vm/drop_caches' result = os.popen(command).read() return True if not result else False except Exception as e: print(f"Error occurred while trying to clear cache: {e}") return False ``` 上面展示了一个简单的 Python 函数示例,它可以用来自动化这一过程,并提供默认选项以一次性清除多种类型的缓存。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值