I need to serve big files very frequently. After starting the serving process for the first time and users start to request these files, all my drives are heavily working. But disk utilization reduces to a minimum after a while, even though the traffic doesn’t decline.
Why? Clearly this is the work of Linux’ caching mechanism. But I never really believed it (that’s just how my brain works). So I tried and proved it myself, as fast and easy as possible…
First I created a big file on my ThinkPad x230 from 2012 with the biggest i7 there was, a Samsung PRO SSD and merely 8 GB of RAM. The test file I’m going to work with is 1GB of size. The test program I will write is just reading the whole file, 1 kilobyte (which isn’t even an optimal size to read from a hard drive) at a time. Then I measure the execution time and check the used cache and the used memory.
For comparison, this is what the cache consisted of:
Compiled with clang, the program takes more than 2 seconds to read the entire file and my disk operation LED light was lit up constantly until the program ended.
That’s not an optimal execution time when you have to distribute the same big files again and again. But that was just the first start. Let’s see if Linux learned from that situation:
That doesn’t look like it cached the whole file or even part of it. The cache / buffer only grew by like 800 MB. Regardless, let’s see if something has improved:
Well, it worked. Speed increase of roughly 800% and hard disk lifespan saved. The whole file was cached, the disk operation LED didn’t even turn on. Maybe the cache / buffer limit was 2 GB and some other cached / buffered data had to be uncached for our big file. I won’t bother looking this up for now.
Last try. Force the kernel to uncache everything and try again:
It works! And did I install or configure something to make this work? No! Linux is amazing! And don’t worry if your RAM seems full. It’s probably just a large cache that eats all the RAM. But if any application needs some of this precious RAM, Linux will uncache something immediately to make space.
Just to show how big this “my RAM is full” issue is: there is an entire website about this: https://linuxatemyram.com/