Linux – How to configure Linux to use less memory for caching

linuxmemcachememorymemory usage

I have a follow up question to 377448, but not enough rep to comment there.

Before I get dinged for asking the same dumb question, let me say I've been reading a lot of similar posts and answers regarding caching, and I understand what's going on there, but I haven't found an answer that fits my context. Which is this;

VirtualBox, contrary to what its documentation currently says, doesn't allocate the full base memory from the host when the guest VM launches, but allocates it as the guest demands. Rather than tuning and bench marking and re-tuning the base memory to fit the ever changing VM, I allocated it more than enough – 4 Gig.

What I've noticed is that shortly after boot, the VM consumes about 2 Gig, but that over time, even if the apps running in the VM are largely idle, the linux memory caching slowly balloons until the 4 Gig limit is reached.

Since the RAM on the laptop is precious and my processing (performance) demands are low, I added a cron @reboot command like so;

@reboot root while true; do sleep 1; /sbin/sysctl -w vm.drop_caches=3; done

which eliminates the continual growth in a way that doesn't noticeably degrade performance. 1 second intervals might be too aggressive, but I'll experiment with that later.

I'm pretty happy with the result, but this solution seems like a brutish hack.

Is there an existing method for configuriong the memory caching as 'lazy'/'aggressive' or 'high performance'/'low performance' or by providing some cache size limit, or cache purge interval?

Best Answer

You seem to have two conflicting requirements. "I want to allocate 4G to this host" and "I dont want the host to use 4G".

Aside from that fact, what makes you think dropping those caches makes the hypervisor drop the memory in the map?

On the hypervisor (virtualbox host), there may be demand paging happening -- thats fine, but what you think is happening probably is not.

There are two stages when virtual memory is request.

  • What is commited
  • What is allocated

When the VM starts it commits to 4G of memory. This is how much it can have, but not necessarily how much it will actually use.

As the VM starts to consume memory, it will allocate pages up as needed up to what it has been committed. Allocation actually places the memory into the ram/swap.

In your scenario, indeed the VM may (I dont know the virtualbox model but I'll take your word for it) commit to 4G of memory. As the VM fills up the pagecache it will allocate all of that memory, again as you mentioned. What wont happen however is virtualbox freeing the memory that has been allocated when you drop caches on the VM.

So by you flushing the page cache out, whilst it appears that the memory is free on the VM, the memory is still allocated and used to the hypervisor.

As such, you have not saved yourself any memory whatsoever on the host laptop, in fact you've just wasted it because what memory could have been used for preventing I/O lookups in the VM are no longer available.