关于页缓存(Page Cache)与dirty_background_bytes的认知误区及疑问
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_centisecsvalue. 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:
- Temporary change (reverts on reboot):
This sets the expiration to 3600 seconds (1 hour)—tweak the number based on how long you want dirty data to stay in memory.echo 360000 > /proc/sys/vm/dirty_expire_centisecs - Permanent change (survives reboots):
Add this line to/etc/sysctl.conf:
Then apply the change immediately with:vm.dirty_expire_centisecs = 360000sysctl -p
How To Verify
To confirm the behavior matches your expectations:
- Use
vmstat 1to monitor disk write activity (bi/bocolumns) and memory usage. - Check real-time dirty page stats with:
Keep an eye onwatch -n 1 'cat /proc/vmstat | grep -E "dirty|writeback"'nr_dirty—this counts current dirty pages. You should see this grow until it hits your 9GB threshold beforenr_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

