Linux – Will stat() cause an actual harddisk access or is the information cached


While optimising my PHP opcode caches, I tried alternating the stat setting ( = the opcode cache checks, whether a file was modified via stat(), before using the cached results). But I did not see any difference in my application benchmarks. The filesystem in use is ext4, mounted with noatime option.

Is it correct to assume, that the metadata used by stat(), is kept in memory, or is there always some kind of harddisk interaction when stat() is used?

Best Answer

The data returned by stat (the file's metadata) is cached like any other filesystem data. If you accessed it recently enough for it to be still in the cache, then subsequent accesses are faster, until something else replaces it in RAM.

Accessing a file's content does not load its metadata into memory (or vice versa).

The stat check costs a little extra (very little if the metadata is in cache, but still a little). Whether this compensates the potential extra processing depends on how much processing you'd be doing and on your IO/CPU saturation ratio.