59

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?

10 Answers 10

59

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

sudo swapoff -a
sudo swapon -a

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

  • 4
    It works, very slowly though :) Thanks! But still I believe there's a more elegant solution :) – kolypto Feb 7 '10 at 18:57
  • 7
    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
  • 2
    It seems that the swapoff command does something like "do not allow new writes to swap" while any other running process (which was/is using swap) can still "release swap". This may be why it seems to go so slow. It does not seem to cause OOM's as another person said (I am using Ubuntu Bionic) - likely that would only be the case if "additional (or existing) processes start using new memory" (i.e. real OOM's). So all in all the swapoff/swapon is a great solution it seems. Gentle/steady. This assumes that you do not initiate more memory using processes (or increase consumption in running ones). – Roel Van de Paar Aug 16 '18 at 20:37
12

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.

  • 13
    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
  • 7
    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
  • @Juliano, why shouldn't /proc/sys be used? – James Dec 29 '15 at 10:10
  • @James Because /proc/sys is the lower level interface used to change and query the kernel configuration (it is "implementation detail"). Linux provides the higher-level sysctl command for user interaction. The end result is the same, but usually higher-level interfaces are preferred for the user to interact directly. Kinda like starting the engine by short-circuiting the ignition wires (lower level) instead of just turning the key (higher level). – Juliano Dec 31 '15 at 23:00
5

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.

  • 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
  • 1
    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
  • convert has the -limit argument to control its memory vs. disk usage, and you should read up on them. Setting -limit memory 512MB or similar would be good. It would probably also be good to specify an explicit MAGICK_TEMPORARY_PATH and clean that up after your command is done. – slacy Mar 4 '18 at 17:34
3

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.

  • I'm afraid this is not quite accurate. – Marc.2377 Dec 7 '18 at 0:31
2

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.

  • 9
    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 '14 at 2:50
1

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

1

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.

  • 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
0

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

0

I believe that there is no any really good way to force Linux unswap data from disk to memory. When swapoff/swapon is a working solution, but it is dirty and easily can make your system unstable. For the cases when you have more data in the swap than free memory it will be hard to imagine any efficient policy which Linux can employ to decide what pieces of data move into memory and what pieces keep on disk.

Summary: Just let Linux gradually restore its performance in the normal way. Its VM subsystem is organized in such a way that it strives and constantly moving to some ideal balanced state.

-2

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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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