My question is an extension of this one How to manipulate page cache in Linux?
I was trying to do a small project that aims to limit the size of page cache used on a per file basis. The approach I used was as follows -
I expect this code to free the pages and release the memory but that doesn't seem to happen. For example, if I read a file of size 25MB and I've restricted the size of page cache for this file to be 512 pages (2MB), then I expect to see a change of only 2MB in the free memory (free -m). What I see instead is that the full 25MB is eaten up and shows up in the free command.
What more should I do to ensure that my requirements are fulfilled? I've not thought about dirty pages yet as I couldn't even make it work for reads (cat the file). Any pointers would be helpful.
P.S. - I'm using linux 4.0 for this project.
Under Linux, the Page Cache accelerates many accesses to files on non volatile storage. This happens because, when it first reads from or writes to data media like hard drives, Linux also stores data in unused areas of memory, which acts as a cache.
The page cache is the main disk cache used by the Linux kernel. In most cases, the kernel refers to the page cache when reading from or writing to disk. New pages are added to the page cache to satisfy User Mode processes's read requests.
Risks from clearing page cacheClearing cache will free RAM, but it causes the kernel to look for files on the disk, rather than in the cache. This can cause performance issues. Normally, the kernel will clear the cache when the available RAM is depleted. It frequently writes dirtied content to the disk using pdflush.
A page does not necessarily contain physically adjacent disk blocks, and it cannot thus be identified by a device number and a block number. Instead, a page in the page cache is identified by a file's inode and by the offset within the file.
You might have to do way more than just delete_from_page_cache() + try_to_unmap() + put_page()...
See how shrink_page_list() behaves, including the checks for page_check_references():
More here: How to unmap struct page from all PTEs mapping it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With