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?


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.

  • You are correct in that what VBox has allocated it never returns. However, dropping the cache each second seems to free that memory to be reused for subsequent caching - as opposed to the caching demanding more fresh memory from the hypervisor. In the end it's net less memory demanded from the hypervisor. – CAB Oct 2 '13 at 3:15
  • That is probably true -- so why give it 4G in the first place if you are not prepared for it to allocate 4G? You said it uses 2G at boot, so why not give it that? – Matthew Ife Oct 2 '13 at 6:49
  • I frequently install packages/services, try them out and then remove and rebuild the VM to try other packages/services. I got tired of the 'how much RAM will it need today?' curly shuffle, so I prefer to allow it 4G and just let it take what it needs. At the same time, I'm often working 2 or 3 different projects, and it's nice to have them running simultaneously. Bench marking combinations to calculate optimal RAM sizes is time prohibitive, and ultimately non-productive. If a configuration goes to production it runs on a large VM, and I don't care what caching does there. – CAB Oct 2 '13 at 17:12

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.