1

I'm asking specifically about Linux, but an answer that applies to Unix in general (i.e. POSIX or similar) would be even better, obviously.

Linux uses free memory (i.e. that memory which is not yet allocated to processes) for caching filesystem metadata (and maybe other things). When processes request additional memory, these caches are shrunk to make room.

My question: Is there a method by which an application can allocate memory that serves as a cache only? That is, the allocation is made knowing that the kernel is allowed to seize control of this memory area in some way when available memory runs low and other processes' memory allocations could otherwise not be served.

1

You could perhaps use madvise(2)’s MADV_FREE for this — it marks pages as available for reclaim, but doesn’t necessarily drop them immediately, and the data can be read back. You’ll know the pages are gone if you get all zeroes back (per page).

  • Of course "madvise is advice. It's advice." and one of the lessons to learn from that talk is that looking for something with general application here (as the questioner would like) has to be done quite carefully in order to be sure that one kernel's semantics match those of other kernels. – JdeBP Nov 22 '17 at 20:47
  • Well, yes; I was answering specifically for Linux (kernel 4.5 and later in fact, which I should mention in the answer). Advising the kernel that it may reclaim memory if necessary seems like a pretty close match to the requirements, in that context. It would no doubt be better to build something on top of the page cache, but that’s rather more involved... – Stephen Kitt Nov 22 '17 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.