Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|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

9 Answers 9

up vote 36 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...

share|improve this answer
2  
It works, very slowly though :) Thanks! But still I believe there's a more elegant solution :) –  kolypto Feb 7 '10 at 18:57
5  
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 :) –  kolypto 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

You can tune it echoing some number between 0 to 100 into /proc/sys/vm/swappiness.

This control is used to define how aggressive the kernel will swap memory pages. Higher values will increase agressiveness, lower values decrease the amount of swap. A value of 0 instructs the kernel not to initiate swap until the amount of free and file-backed pages is less than the high water mark in a zone.

The default value is 60.

share|improve this answer
6  
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
4  
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
1  
@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

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.

share|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 :) –  kolypto 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

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.

share|improve this answer

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.

share|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

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.

share|improve this answer
1  
There are some cases where I want to do what the OP wants. If I accidentally let a process run riot and take up all my RAM+swap, then I can either wait 15 seconds every time I switch applications for it's memory to come out of swap or just force it and have everything run as fast as usual. –  Alex Jan 7 at 2:50
    
@Alex +1 for explicitly mentioning the valid use case we all have in our minds. –  Vortico Dec 13 at 3:22

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

share|improve this answer

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. =)

share|improve this answer

Emptying the buffers cache

If you ever want to empty them you can use this chain of commands.

$ free && sync && echo 3 > /proc/sys/vm/drop_caches && free

             total       used       free     shared    buffers     cached
Mem:       1018916     980832      38084          0      46924     355764
-/+ buffers/cache:     578144     440772
Swap:      2064376        128    2064248
             total       used       free     shared    buffers     cached
Mem:       1018916     685008     333908          0        224     108252
-/+ buffers/cache:     576532     442384
Swap:      2064376        128    2064248

You can signal the Linux Kernel to drop various aspects of cached items by changing the numeric argument to the above command.

NOTE: clean up memory of unnecessary things (Kernel 2.6.16 or newer). Always make sure to run sync first to flush useful things out to disk!!!

  • To free pagecache:

    $ echo 1 > /proc/sys/vm/drop_caches
    
  • To free dentries and inodes:

    $ echo 2 > /proc/sys/vm/drop_caches
    
  • To free pagecache, dentries and inodes:

    $ echo 3 > /proc/sys/vm/drop_caches
    

The above are meant to be run as root. If you're trying to do them using sudo then you'll need to change the syntax slightly to something like these:

$ sudo sh -c 'echo 1 >/proc/sys/vm/drop_caches'
$ sudo sh -c 'echo 2 >/proc/sys/vm/drop_caches'
$ sudo sh -c 'echo 3 >/proc/sys/vm/drop_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.