When I run time find ~
the first time, meaning the first time I run find
in a session, it takes longer than than when run a second time. Why is that?
First run:
real 0m12.410s
user 0m0.424s
sys 0m1.287s
Second run:
real 0m0.988s
user 0m0.214s
sys 0m0.405s
Best Answer
The link provided by Rinzwind titled Why is [find] running incredible fast, if run twice? and What is “cached” in the top command? cover the same issue.
Essentially, if free RAM is available, the kernel can use part of it to cache information. The memory used in this way can be made available to applications on demand.
Linux ate my RAM explains terms such as available, buffer, free, and used. It also provides the following command to clear "most of the disk cache":
To confirm that find is quicker the second time around, I ran
time find ~
for the first time:And a second time to see the beneficial effect of memory caching:
Then I cleared cache with
echo 3 | sudo tee /proc/sys/vm/drop_caches
and rantime find ~
again. Sure enough, the time was high again:But, because of fresh caching, a run of find subsequently and expectedly, showed an improved time:
While reading about cache, I came across Caching/preloading files on Linux into RAM and a couple of answers suggested vmtouch - the Virtual Memory Toucher: it is described as a "Portable file system cache diagnostics and control" and is in the repos for Bionic and later.