67

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?

kolypto
  • 10,738
  • 12
  • 51
  • 66

10 Answers10

66

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

vvvvv
  • 175
  • 8
Douglas Leeder
  • 2,725
  • 18
  • 15
  • 4
    It works, very slowly though :) Thanks! But still I believe there's a more elegant solution :) – kolypto Feb 07 '10 at 18:57
  • 8
    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 07 '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 08 '10 at 09:34
  • 3
    I'm not sure killing swapoff will actually stop the operation. Depends how it's implemented. – Douglas Leeder Feb 08 '10 at 14:05
  • 1
    @Juliano - Hence my last sentence - If you don't actually have enough memory then the OOM killer will get you. – Douglas Leeder Feb 08 '10 at 14:05
  • Luckily, the second command (`swapon`) is fast ;) It took about a minute to kick 0.6 GiB out of the swap... – Tomasz Gandor Apr 08 '16 at 09:07
  • 4
    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
  • Well, that fixed the swapped out processes but it didn't help my X-Windows session. It timed out too much that some processes much have died and that was it. After a reboot, that was fixed too. – Alexis Wilke Jun 15 '20 at 04:35
14

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.

Cristian Ciupitu
  • 6,226
  • 2
  • 41
  • 55
sntg
  • 1,424
  • 11
  • 15
  • 15
    Do not use /proc/sys directly, use the `sysctl` command instead. In this case, it is `sysctl vm.swappiness=x`. – Juliano Feb 07 '10 at 23:53
  • 8
    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 08 '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 08 '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
  • 2
    @Juliano that is not true. procfs is part of public kernel interface. The official doc calls out that it is an interface to the kernel internals: https://www.kernel.org/doc/Documentation/filesystems/proc.txt Also sysctl merely forwards the values to procfs path. See: https://github.com/thlorenz/procps/blob/master/deps/procps/sysctl.c#L354 – wbkang Dec 17 '18 at 22:08
4

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.

CarpeNoctem
  • 2,397
  • 4
  • 23
  • 32
  • 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 08 '10 at 09: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 08 '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 04 '18 at 17:34
  • This kind of approach is only actually useful if OP is planning to launch the same thing again. "My machine is in a bad state where things are slow, and I want to fix this" is a reasonable question and shouldn't always be met with "Why did you get it in a bad state? Try not to do that." – Alex Meiburg Jun 30 '22 at 20:43
2

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.

3dinfluence
  • 12,409
  • 2
  • 27
  • 41
  • 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 08 '10 at 14:04
2

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.

Gruic
  • 29
  • 1
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).

Maximus Minimus
  • 8,937
  • 1
  • 22
  • 36
1

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.

Michael Kohne
  • 2,284
  • 1
  • 16
  • 29
  • 12
    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 07 '14 at 02:50
  • 1
    Well...I want the "unswap" to happen during my coffee break, not every time when I try to switch applications. Is it "out-thinking the VM subsystem" when I want to inform it of an impeding coffee break? – Klaws Oct 31 '19 at 15:12
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. =)

jdoss
  • 21
  • 2
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.

ZarathustrA
  • 101
  • 1
-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'
slm
  • 7,355
  • 16
  • 54
  • 72