My Ubuntu laptop swap cache to disk freeze my system

Posted on

Problem :

My laptop is Thinkpad x220, 4G ram, harddisk is 7200rpm, installed with Ubuntu 12.04, using Unity desktop.

When I open eclipse and chrome, they eat about 2G memory. Here is the free -m result:

             total       used       free     shared    buffers     cached
Mem:          3841       3301        540          0         10        956
-/+ buffers/cache:       2334       1507
Swap:         8187        306       7881

And the top result:

 Mem:   3934188k total,  3540508k used,   393680k free,    45680k buffers
 Swap:  8384508k total,   313512k used,  8070996k free,  1078888k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                       
 5552 martin    20   0 4249m 388m 6560 S    0 10.1   2:46.28 java                                                                       
 3369 martin    20   0 2548m 182m  32m R    1  4.7  29:19.64 chrome                                                                     
 3406 martin    20   0 1000m 155m  16m S    0  4.1   4:18.97 chrome                                                                     
 2736 martin    20   0 1582m 106m  29m S    1  2.8  13:10.27 compiz                                                                     
 3704 martin    20   0  945m  85m 8396 S    0  2.2   0:15.60 chrome                                                                     
11047 martin    20   0  926m  82m  23m S    0  2.2   0:06.52 chrome                                                                     
11443 martin    20   0  915m  72m  21m S    0  1.9   0:46.08 chrome                                                                     
 4280 martin    20   0  822m  66m  12m S    4  1.7  14:59.41 chrome

After some periods about 10 minutes, I have only 100M+ memory available, then the system starts to swap memory to disk, that causes my computer freeze totally, it would recover after the swap finished.

I already have vm.swappiness = 10

I’ve searched google, and some article said that when memory is not enough, the system will
free the cache, and use it as memory. But it seems to me that the cache is never freed. When the system lacks of memory (But actually I have enough memory, some memory is just used for cache), it will not use cache, instead, it swap the memory directly to disk, that causes freeze.

How can I avoid this?

Decreasing the cached memory?

Solution :

If you want more memory free, you can get it by setting vm.min_free_kbytes. For example, if you want the system to waste 256MB of memory, use this command:

sysctl -w vm.min_free_kbytes=262144

The system will now keep 256MB free and have 256MB less memory to work with. That will make performance worse, but it will make the cache smaller, as you requested.

But I think you are missing an important point — free memory is no better than memory that is not installed in the computer at all. Neither of them help the computer to perform better.

You should restore all settings to defaults and work on your actual problem — that your computer is freezing.

Leave a Reply

Your email address will not be published. Required fields are marked *