Page Cache not Fully Released
I tried to release all page cache pages. First, I checked its current size. cat /proc/meminfo|head
shows:
MemTotal: 11703912 kB
MemFree: 5637568 kB
MemAvailable: 7114944 kB
Buffers: 136304 kB
Cached: 3184944 kB
SwapCached: 0 kB
Active: 2760572 kB
Inactive: 1424344 kB
Active(anon): 2271112 kB
Inactive(anon): 251312 kB
It is 3,184,944 KB
. Then, I tried to clear page cache using the following command:
sync; echo 3 > /proc/sys/vm/drop_caches
cat /proc/meminfo|head
changed to this:
MemTotal: 11703912 kB
MemFree: 6722340 kB
MemAvailable: 7062480 kB
Buffers: 3408 kB
Cached: 2271332 kB
SwapCached: 0 kB
Active: 2418432 kB
Inactive: 1938232 kB
Active(anon): 2277092 kB
Inactive(anon): 1535468 kB
Almost 1 GB
is released. Why is around 2 GB
still locked in memory?
I also processed sudo lsof
output and the total size was 1,586,536,501 B
. Note that, this is the total size and RSS
should be smaller. This is still about 1 GB
smaller than the size of page cache cotents. Am I missing something? Any guesses/suggestions is appreciated!
UPDATE:
Let's assume that all 2 GB
is needed by live processes. Reading 2 GB
from disk (at least in my slow hard disk) should take some time. But page cache size is refreshed to 2 GB
, almost, immediately.
If we assume that open files in live processes will be immediately reclaimed (i.e., will not be released and re-read from disk), then the following should not occur:
/usr/lib/x86_64-linux-gnu/libcairo.so.2.11510.0
is in page cache and applying vmtouch -v
on the file results in:
/usr/lib/x86_64-linux-gnu/libcairo.so.2.11510.0
[OOOOOOOOOOOOOOOOOOOOOOOOOOOoOOOOOoOOOOOoOoOOOOOOoo oOOO] 257/284
Files: 1
Directories: 0
Resident Pages: 257/284 1M/1M 90.5%
Elapsed: 9.1e-05 seconds
I opened the libcairo
and, as a consequence, vmtouch
reported 100%
residency. Then I released page cache. 240
out of 284
pages were resident (instead of 284
out of 284
). This means that the pages were actually accessed from disk. Otherwise (i.e., if the release operation was ignored), residency would remain 284
out of 284
.
Perhaps, some pages are locked in memory?!
You can't release all the pages in the page cache without killing all the processes running on the system. The page cache at least in part holds pages backing the portions running processes that come from the disk, like executable pages and libraries.
Or, to put it another way, you can release all the page cache, but some of it will be instantly picked back up, as the pages are still needed.
When you request the pages to be released, the release action doesn't erase the pages or immediately mark them as free. It marks them as immediately available. And then they are immediately marked as used, as what is in memory still needs them.
So when you read the file in, all the pages are now in memory. Then you tell the system to release the pages, and all the pages are released, but the ones actually being used are reclaimed immediately, likely before the actual flush occurs. The ones that you forced to be read in remain released.