views:

2634

answers:

3

I want to force the Linux kernel to allocate more memory to applications after the cache starts taking up too much memory (as can be seen by the output of 'free').

I've run

sudo sync; sudo sysctrl -w vm.drop_caches=3; free

(to free both disc dentry/inode cache and page cache) and I see that only about half of the used cache was freed - the rest remains. How can I tell what is taking up the rest of the cache and force it to be freed?

+6  A: 

The contents of /proc/meminfo tell you what the kernel uses RAM for.

You can use /proc/sys/vm/vfs_cache_pressure to force the kernel to reclaim memory that is used for filesystem-related caches more lazily or eagerly.

Note that your application may only benefit from tuning this parameter if it does little or no disk I/O.

hillu
My app does very little IO - most data is from a remote DB. I'm a little hazy on the /proc/meminfo: I see about Cached: 1.9GB, SwapCached: 8MB. IIRC, that means that Cached is composed of 8MB swap caching, 1.892GB other stuff (disk cache, and what else?). Where do memory mapped files are counted?
Guss
Memory mapped files (if the app is at all good) should have their own advisories and pressure, preferably by the application calling posix_madvise(). You might also want to read up on your 'swappiness' setting.
Tim Post
Indeed a good answer, just not an answer to my question :-). Do I understand correctly that memory mapped files are counted against their application's RES memory footprint?
Guss
Against their applications VIRT which may or may not be resident depending on if all the pages are accessed and paged in.
stsquad
+1  A: 

You might find John Nilsson's answer to my Question useful for purging the cache in order to test whether that is related to your problem:

sync && echo 1 > /proc/sys/vm/drop_caches

Though I'm guessing the only real difference is 1 vs 3

Stephen Denne
1 only clears the disk cache - it was relevant to your question. I want to also clear the page cache as I think that one of my apps is leaking memory and thrashing badly.
Guss
+2  A: 

You will want to increase vfs_cache_pressure as well as set swappiness to 0. Doing that will make the kernel reclaim cache faster, while giving processes equal or more favor when deciding what gets paged out.

With the default swappiness setting, the kernel is almost always going to favor keeping FS related cache in real memory. So if you increase the cache pressure, be sure to also adjust swappiness.

Tim Post
Thanks for the answer. Though its not what I was hoping for, I'll accept it as it includes information I didn't know and gives some guidelines to achieve a target. I would still like to know better how to figure out what the cache is used for, especially when it is not being cleaned by drop_caches.
Guss