1

Given a 4KB block size, when a 1KB file is read from disk and cached in main memory, is the entire 4KB block that contains the file cached?

Similarly, if opening a 100KB file, seeking 50KB in and then reading 1KB of data, is only the 4KB block that contains the 1KB of data read cached in main memory and not the entire file?

I am assuming ext4 filesystem, but not stuck to it.

  • 1
    Thanks for the feedback. I meant this as one question, as stated in the title. The body offers two different illustrations of the same question. I am not sure quite yet how I can rephrase it to make that clearer. – mjb2kmn Dec 20 '18 at 14:42
1

Yep.

The authoritative Linux Page Cache Basics states

File blocks are written to the Page Cache not just during writing, but also when reading files.

and

Automatically storing file blocks in the Page Cache is generally quite advantageous.

This applies to block-based filesystems, i.e., ccfs, ext?, gfs, ifs, xfs, as shown in his diagram. It's also confirmed by Page Cache, the Affair Between Memory and Files, which explicitly states:

If you read a single byte from a file, the whole 4KB chunk containing the byte you asked for is read from disk and placed into the page cache.

and the third paragraph of that web page confirms only the 4KB block that contains the 1KB of data read cached in main memory and not the entire file.

  • 1
    This page is very general and uses examples of large files, I am specifically looking for details on how small files or small reads of large files are cached. – mjb2kmn Dec 20 '18 at 14:59
  • Added another citation which directly addresses your second question. – K7AAY Dec 20 '18 at 19:25
  • 1
    The Page Cache, the Affair Between Memory and Files post by Gustavo Duarte explains it perfectly. – mjb2kmn Dec 20 '18 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.