自学内容网 自学内容网

GNU/Linux - /proc/sys/vm/drop_caches

/proc/sys/vm/drop_caches 是 Linux 中的一个特殊文件,允许用户释放系统内存中的各种缓存。让我们深入了解一下这项功能的细节:
The /proc/sys/vm/drop_caches is a special file in Linux that allows users to free up various caches in the system's memory. Let's dive into the details of this feature:
    Overview
    /proc/sys/vm/drop_caches 是 Linux 内核提供的一种机制,用于手动释放内存中的页面缓存、目录项缓存和 inodes。这在测试或需要快速释放内存的情况下非常有用。
    /proc/sys/vm/drop_caches is a mechanism provided by the Linux kernel to manually free up pagecache, dentries, and inodes from memory. This can be useful for testing purposes or in situations where you need to free up memory quickly.
     Functionality
    drop_caches 参数允许用户指示内核释放不同类型的缓存:
    The drop_caches parameter allows users to instruct the kernel to free different types of caches:
1. 页面缓存: 这是文件内容的缓存。
2. Dentries: 这是目录条目缓存。
3. Inodes: 这些是包含文件元数据的数据结构。
1. Pagecache: This is the cache for file contents.
2. Dentries: These are directory entry caches.
3. Inodes: These are data structures containing metadata about files.
     Usage
    要使用 drop_caches,需要在文件中写入一个特定值:
    To use drop_caches, you write a specific value to the file:
1. Echo 1:仅释放页面缓存。
2. Echo 2:释放目录项缓存和 inodes。
3. Echo 3: 释放页面缓存、目录项缓存和 inodes。
1. Echo 1: Frees pagecache only.
2. Echo 2: Frees dentries and inodes.
3. Echo 3: Frees pagecache, dentries, and inodes.
    例如,要释放所有缓存,可以使用
    For example, to free all caches, you would use:
echo 3 > /proc/sys/vm/drop_caches
    Important Considerations  重要考虑因素
    1. 非破坏性操作: 此操作为非破坏性操作,不会释放任何脏对象(已修改但尚未写入磁盘的对象)。
    1. Non-destructive Operation: This operation is non-destructive and will not free any dirty objects (those that have been modified but not yet written to disk).
    2. 丢弃前同步: 建议在删除缓存前运行 “同步”,以确保所有脏对象都写入磁盘。
    2. Sync Before Dropping: It's recommended to run 'sync' before dropping caches to ensure all dirty objects are written to disk.
    3. 性能影响: 使用 drop_caches 可能会导致性能问题,因为它会丢弃可能需要重新创建的缓存对象,从而可能导致大量的 I/O 和 CPU 占用。
    3. Performance Impact: Using drop_caches can cause performance problems as it discards cached objects that may need to be recreated, potentially causing significant I/O and CPU usage.
    4. 不适合常规使用: 由于可能影响性能,不建议在测试或调试环境之外使用 drop_caches。
    4. Not for Regular Use: Due to potential performance impacts, using drop_caches is not recommended outside of testing or debugging environments.
    5. 自动回收: 当系统其他地方需要内存时,内核会自动回收这些对象,因此通常不需要人工干预。
    5. Automatic Reclamation: The kernel automatically reclaims these objects when memory is needed elsewhere in the system, so manual intervention is usually unnecessary.
    6. 信息消息: 使用该文件时,您可能会在内核日志中看到信息消息。这些信息并不表示系统有任何问题。
    6. Informational Messages: You may see informational messages in your kernel log when this file is used. These messages do not indicate any problem with your system.
    Behavior After Dropping Caches
    删除缓存后
1. 作为正常运行的一部分,系统会立即重新开始缓存。
2. /proc/sys/vm/drop_caches 中的值会自动重置为 0。
    After dropping caches:
1. The system immediately starts caching again as part of its normal operation.
2. The value in /proc/sys/vm/drop_caches automatically resets to 0.
    Use Cases
    虽然不建议经常使用,但 drop_caches 在特定情况下还是很有用的:
1. 测试: 需要使用低缓存进行基准测试或性能测试时。
2. 调试: 隔离与缓存相关的问题。
3. 内存管理: 在极少数情况下,需要快速释放内存,但应谨慎处理。
    While not recommended for regular use, drop_caches can be useful in specific scenarios:
1. Testing: When you need to start with a "cold" cache for benchmarking or performance testing.
2. Debugging: To isolate issues related to caching.
3. Memory Management: In rare cases where you need to quickly free up memory, though this should be approached with caution.
    Alternative Approaches
    对于大多数系统来说,最好让内核自动管理缓存。如果内存一直不足,可以考虑:
1. 为系统添加更多内存。
2. 调整其他内存管理参数。
3. 优化系统上运行的应用程序。
    For most systems, it's better to let the kernel manage caches automatically. If you're consistently running low on memory, consider:
1. Adding more RAM to the system.
2. Adjusting other memory management parameters.
3. Optimizing the applications running on the system.
    总之,虽然 /proc/sys/vm/drop_caches 提供了在 Linux 中手动释放各种缓存的方法,但应谨慎使用,并主要用于测试或调试目的。对于正常的系统操作,最好还是依靠内核内置的内存管理功能。
    In conclusion, while /proc/sys/vm/drop_caches provides a way to manually free up various caches in Linux, it should be used judiciously and primarily for testing or debugging purposes. For normal system operation, it's generally best to rely on the kernel's built-in memory management capabilities.
资料来源:
[1] Setting /proc/sys/vm/drop_caches to clear cache, https://unix.stackexchange.com/questions/17936/setting-proc-sys-vm-drop-caches-to-clear-cache
[2] Drop_Caches - linux-mm.org Wiki, Drop_Caches - linux-mm.org Wiki
[3] Freeing page cache using echo 3 > /proc/sys/vm/drop_caches ..., https://askubuntu.com/questions/609226/freeing-page-cache-using-echo-3-proc-sys-vm-drop-caches-doesnt-work
[4] Why does drop_caches increase "available" memory - Server Fault, https://serverfault.com/questions/1099933/why-does-drop-caches-increase-available-memory

原文地址:https://blog.csdn.net/guoqx/article/details/143652996

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!