Write operations that continuously leave 4,000 bytes (4 KB) in the buffer do so because of a deliberate design in many storage and filesystem layers: data is held back to be coalesced into a full block or sector before being flushed to disk. This is not a bug or a leak—it is a performance optimization that reduces the number of small, inefficient writes. The 4 KB figure specifically matches the typical sector size of modern hard drives and SSDs, as well as the page size of many operating systems.
Background: Why 4 KB Matters
The 4 KB size is not arbitrary. It is the standard logical sector size for most hard drives (since the Advanced Format transition) and the native block size for many SSDs. It also matches the memory page size used by Linux and Windows on common x86 architectures. When a filesystem writes data, it groups bytes into blocks that are multiples of this size. If a program writes less than 4 KB, the filesystem may hold it in a buffer until it can fill a full block, or until a flush is explicitly requested. This behavior is visible in tools like strace on Linux, where a write() call might return immediately but the data sits in the page cache until the kernel decides to write it out.
How Buffering Works in the Operating System
When you call write() in a program, the kernel copies the data into a memory buffer (the page cache) and then returns. The actual disk I/O happens later, asynchronously. The kernel uses a set of heuristics to decide when to flush:
- If the buffer reaches a full block (e.g., 4 KB), it is immediately scheduled for write-back.
- If the buffer is dirty for longer than a threshold (e.g., 30 seconds in Linux), it is written even if not full.
- If the filesystem is unmounted or the system shuts down, all buffers are flushed.
- If the application calls
fsync()orfdatasync(), the data is forced to disk immediately.
This design means that a program doing many small writes (e.g., 100 bytes each) will see the kernel accumulate them until they form a 4 KB block, then write that block. The leftover bytes—those that do not fill a complete block—remain in the buffer, which is why you might observe a constant 4 KB residue after a series of writes that total, say, 4,100 bytes.
Why Programs See a Persistent 4 KB Buffer
If you monitor a process's memory or I/O statistics, you might notice that after a write of, say, 2 KB, the buffer shows 2 KB. After another 2 KB write, the buffer shows 4 KB, and then it flushes to zero. But if you write 3 KB and then stop, the buffer will show 3 KB—not 4 KB. So why would it always show exactly 4 KB? This happens when the application writes a pattern that never quite fills a block. For example, if a program writes 1 KB four times, the first three writes might fill the buffer to 3 KB, and the fourth write fills it to 4 KB, triggering a flush. But if the program writes 1 KB five times, the fifth write leaves 1 KB in the buffer, not 4 KB. The persistent 4 KB is more likely due to a fixed-size buffer in the application itself, or a filesystem that uses a 4 KB allocation unit and always reserves one full block for the next write. In some network filesystems, the client may buffer up to a full 4 KB before sending, so you always see that much pending.
Common Misconception: It’s a Memory Leak
Many developers mistakenly think a constant 4 KB buffer indicates a memory leak or a stuck write. In reality, it is usually the kernel’s page cache holding data that has not yet been flushed. The buffer is not lost—it is just waiting. You can verify this by calling fsync() and observing the buffer drop to zero. If it does not drop, then there might be a real issue, such as a driver that fails to complete the write. But in normal operation, the 4 KB residue is harmless and expected.
How to Control or Eliminate the Buffer
If you need to ensure data is written immediately, you have several options:
- Use
fsync()orfdatasync()after each write—this forces a flush but hurts performance. - Open the file with the
O_SYNCorO_DSYNCflag, which makes every write synchronous. - Use
posix_fadvise()withPOSIX_FADV_DONTNEEDto drop the cached data after writing. - For databases, use the
direct I/Omode (e.g.,O_DIRECTon Linux) to bypass the page cache entirely.
Note that these options can significantly reduce throughput because they eliminate the benefits of buffering. For most applications, the default behavior is optimal.
Comparison: Buffered vs. Direct I/O
Buffered I/O (the default) is fast for small writes because it batches them into full blocks. Direct I/O bypasses the buffer, so each write goes straight to disk, which is slower for small writes but gives predictable behavior. For example, a database that needs to guarantee durability might use direct I/O to avoid the risk of losing data in the page cache during a power failure. However, direct I/O requires that the buffer size be aligned to the sector size (usually 4 KB), which is why you often see 4 KB alignment requirements in documentation.
Frequently Asked Questions
Is the 4 KB buffer a sign of a problem?
No, it is normal. The kernel holds back data to fill a full block, and 4 KB is the standard block size. If the buffer stays at 4 KB indefinitely, it simply means the last write did not fill the next block, and the kernel has not yet decided to flush it. It will be written later, usually within a few seconds.
How do I force the buffer to flush immediately?
Call fsync() on the file descriptor, or use sync() to flush all files. In the shell, you can use the sync command. This ensures all pending writes are written to disk.
Why is the buffer size exactly 4 KB, not 8 KB or 1 MB?
Because 4 KB is the typical sector size of hard drives and SSDs, and also the memory page size on most systems. The kernel uses this as the unit for write-back. Some filesystems use larger block sizes (e.g., 64 KB), but the default is 4 KB.
Can I change the buffer size?
Yes, for filesystems you can set the block size when formatting (e.g., mkfs.ext4 -b 4096). For the page cache, you can tune the kernel’s writeback behavior via sysctl parameters like vm.dirty_writeback_centisecs and vm.dirty_expire_centisecs, but you cannot change the fundamental 4 KB page size on x86.
Does this happen on SSDs too?
Yes, SSDs also use 4 KB as the typical logical block size. The buffering behavior is identical, though the underlying flash translation layer may have larger erase blocks (e.g., 128 KB), but that is hidden from the OS.
Why does my program see 4 KB in the buffer even after calling fsync()?
If fsync() returns successfully, the data is on disk. If you still see 4 KB in the buffer, it might be a different buffer (e.g., the application’s own user-space buffer) or a kernel buffer for metadata. Use strace to confirm the write and fsync calls.