up vote 13 down vote favorite
2
share [g+] share [fb]

I've launched something that took lots of memory and now everything lags a lot. I guess all applications' memory has gone to swap in order to free some space for the memory-intensive process, and now everything is slowly returning to RAM when accessed.

Is there a way to explicitly move everything possible from swap back to RAM? Or maybe not everything, but just some particular processes data?

link|improve this question

I'd recommend just allowing normal operations to swap in what is actually needed, rather than manually doing anything. – Douglas Leeder Feb 8 '10 at 14:07
feedback

8 Answers

up vote 13 down vote accepted

I'd recommend allowing the normal Linux memory control swap in the things that are actually used, as they are used.

The only thing I can think off is to turn swap off, then on again

swapoff -a
swapon -a

That assumes you have enough spare physical memory to contain everything in swap...

link|improve this answer
1  
It works, very slowly though :) Thanks! But still I believe there's a more elegant solution :) – o_O Tync Feb 7 '10 at 18:57
3  
Careful with this solution, it will take everything out of swap forcibly... if it doesn't fit physical memory, the kernel will start the deadly OOM killer. I don't know of any nice "try to move everything to RAM, but fail gracefully if not possible" command. – Juliano Feb 7 '10 at 23:52
1  
I've tried it and found out that it works very slowly. So it's possible to monitor the free RAM and kill swapoff if memory goes low: in this case, swap space will remain functional :) – o_O Tync Feb 8 '10 at 9:34
2  
I'm not sure killing swapoff will actually stop the operation. Depends how it's implemented. – Douglas Leeder Feb 8 '10 at 14:05
@Juliano - Hence my last sentence - If you don't actually have enough memory then the OOM killer will get you. – Douglas Leeder Feb 8 '10 at 14:05
feedback

You can tune it echoing some number between 0 to 100 into /proc/sys/vm/swappiness, higher number, more the system will use swap.

link|improve this answer
3  
Do not use /proc/sys directly, use the sysctl command instead. In this case, it is sysctl vm.swappiness=x. – Juliano Feb 7 '10 at 23:53
3  
I don't think even a swappiness of zero will cause pages that are already swapped to be explicitly brought into main memory? – Douglas Leeder Feb 8 '10 at 14:03
@Douglas is right. vm.swappiness will mainly control the decision of whether move things to swap or reduce the amount of caches and buffers when memory is demanded. – Juliano Feb 8 '10 at 15:40
feedback

Linux does a fine job managing memory and you shouldn't stand in its way. The vm.swappiness setting (mentioned previously) doesn't get in its way. You're more likely to experience odd issues doing things any other way.

What did you launch that was so memory hungry? Can it be tuned? If it doesn't have it's own memory limit directives you can look at ulimit as well.

link|improve this answer
In my case it was convert -density 200 file.pdf jpegs/file.jpg. For some reason it uses lots of memory, but you're right: it can be tuned. Anyway, the situation is possible with any application :) – o_O Tync Feb 8 '10 at 9:20
I agree with this answer - you should probably leave normal operations on the machine to swap in what is actually needed. – Douglas Leeder Feb 8 '10 at 14:06
feedback

To copy some of my answer from this question.

So that you know how the swappiness tunable works. This works by telling the VM subsystem to look for pages to swap when the % of memory mapped to process page tables + swappiness value is > 100. So a setting of 60 will cause the system to start paging out stale pages from the process page table when it is using more than 40% of your system's memory. If you want to allow your programs to use more memory at the expense of cache you'll want to lower the swappiness value.

link|improve this answer
I don't think program memory vs. cache is the issue here - I think it's application memory vs. unused memory. And I don't think swappiness will affect that. – Douglas Leeder Feb 8 '10 at 14:04
feedback

I would advise against trying to out-think the VM subsystem in the kernel. It is EXTREMELY unlikely that you actually have enough information to make better decisions than it will. And if you force it somehow to do the wrong thing, then you'll just end up making things even slower.

link|improve this answer
feedback

If you have the memory available for all your applications, it is ok to set the swappiness to 0 so things won't swap out. For example, qemu-kvm is a big target the VMM to get swapped out, because it "appears" to be idle most of the time. I've see up to 80% of the memory of a qemu-kvm memory get written to swap. The VMs running in qemu-kvm will become near-unresponsive because they are running out of swap (although the guest has no idea this is happening). The guest VM will think it's performing most excellently, even though in truth it is dragging along terribly. When I bunch of VMs "wake up" and start doing things, it can spike the load average up to over 30, even on enterprise grade hardware with ample fast memory and disk. I guess this is a failing in the out-of-the-box qemu-kvm design.

Hope this helps someone.

link|improve this answer
feedback

If you're able to reboot the system that should do it (and might take far less time than trying any other solution out).

link|improve this answer
feedback

Is the process still running? Open a terminal and see if you can spot the process(s) that were launched. (ps aux |grep processname might make it a bit easier) Use kill -9 PID to kill them off if they are still running. Be careful of what you kill off. If you don't know what the process is, don't kill it! Also, post the output of free -m so we can see if you are actually still using a lot of swap.

If things are still running slow, you might still have whatever you launched still running. I would never turn the swap off on unless you really know what you are doing or you like living on the edge. =)

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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