Sometimes I see my swap file is a bit bigger then usual. I tried sync && echo 3 > /proc/sys/vm/drop_caches to no avail. But when I tried swapoff -a it takes a bit longer but the swap file is smaller after. How can I clear my swap file without using swapoff?
- 3Unless you are doing benchmarks, you never want to do this.jordanm– jordanm2012-11-05 19:31:09 +00:00Commented Nov 5, 2012 at 19:31
- 1But my OS is working better when I drop caches with swapoff? Can you explain?Cybercartel– Cybercartel2012-11-05 19:35:50 +00:00Commented Nov 5, 2012 at 19:35
- 4You seem to be confusing swap and cache. They are almost opposite concepts.Stéphane Chazelas– Stéphane Chazelas2012-11-05 19:42:59 +00:00Commented Nov 5, 2012 at 19:42
- Ok. It's trial and error? But how can I clean my memory? What is drop-caches? Why when I drop swap and remount swap swap file is smaller?Cybercartel– Cybercartel2012-11-05 19:50:03 +00:00Commented Nov 5, 2012 at 19:50
1 Answer
3 > drop_caches instructs the kernel to discard all the cached data (that are not needed any more).
swapoff will try to pull as much of the data, that are currently on swap, as it can back into memory. It may also trigger dropping some cached pages to make room for what is coming into memory from the swap file.
You really only seldom need to do this, the kernel does a rather good job on deciding what to do with what data (e.g. if you are working with a big file it might cache it and swap out the memory allocated to some other running process that isn't very active).
I'm not sure whether you are really using a swap file as such (i.e. a file on a regular filesystem) or just misinterpreting the output of free, but if you do, what could actually help is a dedicated swap partition, as you won't encounter any possible filesystem overhead (but the space would be reserved for kernel to use of course).
By the way, if you use tmpfs (usually mounted at /dev/shm and these days often at /run and some other places as well), any files held there will show up as "cached" in free
- I don't use free and I have 2 swap partitions. I use cat /proc/swaps and I noticed a little increase. My box has only 256 mb ram and I use ramzswap to help but I always run out of memory.Cybercartel– Cybercartel2012-11-05 22:20:04 +00:00Commented Nov 5, 2012 at 22:20
- Is it normal? Maybe I've memory leaks?Cybercartel– Cybercartel2012-11-05 22:56:29 +00:00Commented Nov 5, 2012 at 22:56
- @Chiyou not necessarily - as the processes access disk, data get cached and other data swapped out. You might also wan to check swappiness in
/proc/sys/vm/swappiness(also see question on swappines).peterph– peterph2012-11-05 23:46:45 +00:00Commented Nov 5, 2012 at 23:46 - My swappiness is 30. I don't want to swap much because my hard disk is very slow. What would help is more RAM but thats impossible. My box is a handset.Cybercartel– Cybercartel2012-11-05 23:51:41 +00:00Commented Nov 5, 2012 at 23:51
- How about cutting down the number of processes? Isn't there anything running that you could go without?peterph– peterph2012-11-06 00:09:01 +00:00Commented Nov 6, 2012 at 0:09