Ubuntu – Why does the system use so much cache

cacheperformanceramswap

Previously, on my desktop computer running Ubuntu 14.04, I had 4GB RAM, which I thought should be plenty. However, after being on for a while, my computer would seem to get slow. I have a system resource monitor app in my Gnome panel, which I assume represents the available RAM (?). It shows a dark green area as being "Memory", and a light green area as "Cache". The "Cache" would slowly grow until it filled the whole graph, and then programs would get slow to load, or it would take a while to switch programs.

I could alleviate the problem somewhat with this command, but eventually the computer cache fills up again, so it's only a bandaid:

sudo sh -c "sync; echo 3 > /proc/sys/vm/drop_caches"

So, I figured I'd get more RAM, so I replaced one 2GB stick with an 8GB stick, and now I have 10 GB ram.

And my "cache" still slowly maxes out and my computer slows as a result. Also, sometimes the computer starts out with "cache" maxed when I first boot and log in. Not always though, I don't know if there's a pattern that determines why it happens.

Why is Ubuntu using up so much cache? Is 10GB not enough for Ubuntu?

Here's what my system monitor looks like in my Gnome panel. The middle square shows RAM usage. The light green area is the "cache":

Cache near max

This is my memory and swap history, which doesn't seem to include any information about "cache". I realize at this point I'm not totally clear on the difference between "cache" and "swap":

memory and swap

Best Answer

You probably need to reduce your swappiness.

The kernel considers it better to use the free ram as cache rather then evict cache pages and have the ram marked as truly free. Growing the cache is generally a good thing.

Now the question arises what to do when all the memory is allocated to cache and your application needs more memory. Often the right thing to do is to evict a page from the cache and hand it over to your app but occasionally it is beneficial to swap out a rarely used page, give that to your app and keep the cache intact.

This process is loosely controlled by /proc/sys/vm/swappiness. For more information please check the excellent answers to Why most people recommend to reduce swappiness to 10-20? and How do I configure swappiness?.

Related Question