Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

My situation is pretty simple: I backup-ed some of my documents and put them on a NAS and since I rather transfer one big file instead of million small ones, I created a tar

tar cf backup/2013-07-12-pictures.tar Pictures

My Pictures directory contains about 12GB of data and what happens is, that my cache fills up steadily. If I tar some more files (e.g. my projects, whatever) it doesn't take long and all of my 32GB RAM are used:

sheldor:~$ free -g
             total       used       free     shared    buffers     cached
Mem:            31         31          0          0          0         27
-/+ buffers/cache:          2         28
Swap:           29          0         29

At this point, although the mouse and the system is in general responsible, starting programs (which reads from disk) takes ages. Literally ages: I have an SSD and starting Chrome which usually takes a second needs now about 2 minutes.

Now you find all those posts on the internet about that it is bad to clear the cache and so on. Here, on Ask Ubuntu you find such a post too which saved my life by providing the command

sudo sync && sudo sysctl -w vm.drop_caches=3

If you read through the comments, you find for instance this here:

This is not a problem, this is how the page cache works. It will keep things cached as long as possible, but release items automatically when memory pressure from other applications increases. Clearing it up manually is unnecessary. – Caesium Nov 22 '11 at 16:08

What? My system (Intel Core i7 3.50GHz × 8, 32GB RAM, NVidia Gtx 590) was frozen after a simple tar. I mean, how much pressure do I have to make before my cache is freed automatically?

Question: Is there a way to tweak settings so my cache gets cleared more often? Or is this just something I have to live with?

share|improve this question
    
I think you may be interested in learning about the 'low-latency' kernel. –  Andrea Corbellini Oct 11 '13 at 18:46

3 Answers 3

up vote 2 down vote accepted
+100

I agree that cache handling in Linux is far from optimal. In fact, in many regards Linux still feels more optimized for server/workstation use than desktop use. See this Q&A for a similar problem with CPU/IO-scheduling.

With that said, there are solutions out there that can alleviate the problem. You can manually clear the cache or set up a cronjob, like user197590 pointed out. Another method would be to disable cache generation for specific applications. You can do so with the nocache utility. At the moment it's only available in the 13.10 repos but I was able to install the Saucy package on 12.04 without any problems.

nocache is fairly straightforward to use. You prepend it to any command you want to execute without cache, e.g.:

nocache tar cf backup/2013-07-12-pictures.tar Pictures

It's not a causal solution but it can most definitely cure the symptoms of overzealous caching.

share|improve this answer
    
I would upvote your answer but it seems I'm out of rep. I'm not used to have only 11 points. Honestly, I really thought there must be a simple (configuration) solution, because I'm sure a new Ubuntu user would go nuts when he hears about cron or freeing the cache manually. On the other hand, new users maybe don't recognize that there is a caching problem. Thanks for this answer. –  halirutan Oct 9 '13 at 1:14
    
I tried your solution and, although it doesn't solve the problem completely, it seems to slow down the increase in cache memory. –  halirutan Oct 11 '13 at 12:28
    
@halirutan. That's good to hear! You might be able to improve your results a bit by following the instructions in the 'Limitations' section of the github page. Please also note that nocache comes with some diagnostic utilities that could help you ascertain what files are causing the cache fill. –  Glutanimate Oct 11 '13 at 17:37

The use of memory prefetching deamon such as preload is the best possible solution I've found. It does the same as MS Windows prefetch mechanism.

From preload website:

preload is an adaptive readahead daemon. It monitors applications that users run, and by analyzing this data, predicts what applications users might run, and fetches those binaries and their dependencies into memory for faster startup times.

Preload is for sure in 12.04 default repo, so

sudo apt-get install preload

And that's it. Preload will learn what apps are you using and take care of filling the cache with right files.

I've used it on HDD and SSD PCs with a great effect.

share|improve this answer
    
Even with preload installed large file operations still have a significant impact on system responsiveness and interactivity. It's definitely an improvement but not a complete solution (unfortunately none of the methods currently outlined in the answers are). –  Glutanimate Oct 11 '13 at 5:26

You can manually free up the memory cache with the following simple command:

sk@sk:~$ sudo sh -c "sync; echo 3 > /proc/sys/vm/drop_caches"

However, if you want to force the Linux OS to clear the memory cache on a particular interval, just add the command to cron job. Here, I show you how.

Open up your terminal and enter the following command to create a file called cacheclear.sh. Say for example, I create cacheclear.sh file in my /home directory:

sk@sk:~$ sudo vi /home/cacheclear.sh

Add the following lines to cacheclear.sh file:

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

Save and exit the file. Now add this file to the root crontab:

sk@sk:~$ sudo crontab -e

This command opens the root crontab.

Add the following line to the end:

0 * * * * /home/cacheclear.sh

Save and exit the file. Once you’re done, the cron job will run this command every hour and will clear the system memory caches.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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