为了加速操作和减少磁盘I/O,内核通常会尽可能多地缓存内存,这部分内存就是Cache Memory(缓存内存)。根据设计,包含缓存数据的页面可以按需重新用于其他用途(例如,应用程序)。
程序运行后,高速缓冲存储器不会自动释放。这可能会导致程序在频繁读写文件后可用的物理内存较少,需要在必要时主动释放缓存内存(例如,确实没有足够的内存)。
[En]
The cache memory is not automatically released after the program is run. This may result in less physical memory available to the program after frequently reading and writing files, requiring active release of cache memory when necessary (for example, there is really not enough memory).
注意:通常不建议主动释放缓存,除非您有非常特殊的要求,例如使用测试程序缓存,因为缓存可用于相同的应用程序。
[En]
Note: in general, proactive release of cache memory is not recommended unless you have very specific requirements, such as the use of test program cache memory, because cache memory is available for the same application.
释放缓存内容
可以通过修改系统控制文件 /proc/sys/vm/drop_caches
释放缓存内存。
1、释放 pagecache
(页面缓存)
echo 1 > /proc/sys/vm/drop_caches
2、释放reclaimable slab objects (includes dentries and inodes)(可回收slab对象(包括dentry,inode))
echo 2 > /proc/sys/vm/drop_caches
3、释放 slab
对象和 pagecache
echo 3 > /proc/sys/vm/drop_caches
这是一种非破坏性操作(non-destructive operation
),不会释放任何脏对象。如果要增加此操作释放的对象数量,可以在写入 /proc/sys/vm/drop_cache
之前运行 sync
。这将最大限度地减少系统上脏对象的数量,并创建更多待删除的候选对象。
需要注意的是 /proc/sys/vm/drop_caches
不是用于控制,各种内核缓存(inodes,dentries,pagecache等等)增长的方法。当系统其它地方需要内存时,Linux内核会自动回收这些对象。
使用该文件可能引发性能问题。因为它会删除缓存对象,可能需要消耗大量的I/O和CPU才能重新创建被删除的对象,尤其是这些对象被大量使用的情况下。因此,不建议在测试或调试环境之外使用。
实践测试
free -m
total used free shared buff/cache available
Mem: 7794 2373 1478 431 3942 4691
Swap: 3967 0 3967
echo 1 > /proc/sys/vm/drop_caches
free -m
total used free shared buff/cache available
Mem: 7794 2373 2600 431 2821 4705
Swap: 3967 0 3967
echo 2 > /proc/sys/vm/drop_caches
free -m
total used free shared buff/cache available
Mem: 7794 1635 5527 431 631 5495
Swap: 3967 0 3967
echo 3 > /proc/sys/vm/drop_caches
free -m
total used free shared buff/cache available
Mem: 7794 1635 5529 431 630 5496
Swap: 3967 0 3967
参考连接
Original: https://www.cnblogs.com/shouke/p/16156335.html
Author: 授客
Title: Linux 手工释放Linux Cache Memory
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/523828/
转载文章受原作者版权保护。转载请注明原作者出处!