You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于页缓存(Page Cache)与dirty_background_bytes的认知误区及疑问

Troubleshooting Unexpected Dirty Page Flushing Despite Configured Thresholds

Alright, let's break down what's going on here—your initial expectation makes total sense, but there's a critical kernel parameter that's interfering with your setup: dirty_expire_centisecs.

Why Your Current Setup Isn't Behaving As Expected

Your configured dirty_background_bytes=9663676416 (9GB) and dirty_bytes=19327352832 (18GB) are correct for threshold-based flushing, but the default dirty_expire_centisecs=3000 (that's 30 seconds, since 1 centisec = 0.01 seconds) is overriding this. Here's the breakdown:

  • The kernel doesn't only wait for dirty pages to hit the size threshold—it also flushes any dirty data that's been sitting in memory longer than the dirty_expire_centisecs value. So even if you haven't hit 9GB of dirty data, after 30 seconds, the kernel will write those pages to disk regardless.
  • There are also other edge cases that can trigger early flushing:
    • If the system starts running low on free memory, the kernel will prioritize reclaiming memory by flushing dirty pages, even before thresholds are met.
    • If your application explicitly calls fsync(), fdatasync(), or closes file handles, that forces an immediate flush of related dirty pages.

How To Fix It

If you want the size thresholds to take priority over time-based expiration, adjust the dirty_expire_centisecs parameter to a much larger value:

  1. Temporary change (reverts on reboot):
    echo 360000 > /proc/sys/vm/dirty_expire_centisecs
    
    This sets the expiration to 3600 seconds (1 hour)—tweak the number based on how long you want dirty data to stay in memory.
  2. Permanent change (survives reboots):
    Add this line to /etc/sysctl.conf:
    vm.dirty_expire_centisecs = 360000
    
    Then apply the change immediately with:
    sysctl -p
    

How To Verify

To confirm the behavior matches your expectations:

  • Use vmstat 1 to monitor disk write activity (bi/bo columns) and memory usage.
  • Check real-time dirty page stats with:
    watch -n 1 'cat /proc/vmstat | grep -E "dirty|writeback"'
    
    Keep an eye on nr_dirty—this counts current dirty pages. You should see this grow until it hits your 9GB threshold before nr_writeback (pages being written to disk) spikes.

Just a heads up: even with a longer expiration time, the kernel will still flush dirty pages if the system is under memory pressure. That's a normal part of Linux's memory management to avoid out-of-memory situations.

内容的提问来源于stack exchange,提问作者psycotica0

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:20:28