By Philip Couling

2019-02-08 14:28:24 8 Comments

I have a Debian (Buster) laptop with 8 GB RAM and 16GB swap. I'm running a very long running task. This means my laptop has been left on for the past six days while it churns through.

While doing this I periodically need to use my laptop as a laptop. This shouldn't be a problem; the long running task is I/O bound, working through stuff on a USB hard disk and doesn't take much RAM (<200 MB) or CPU (<4%).

The problem is when I come back to my laptop after a few hours, it will be very sluggish and can take 30 minutes to come back to normal. This is so bad that crash-monitors flag their respective applications as having frozen (especially browser windows) and things start incorrectly crashing out.

Looking on the system monitor, of the 2.5 GB used around half gets shifted into swap. I've confirmed this is the problem by removing the swap space (swapoff /dev/sda8). If I leave it without swap space it comes back to life almost instantly even after 24 hours. With swap, it's practically a brick for the first five minutes having been left for only six hours. I've confirmed that memory usage never exceeds 3 GB even while I'm away.

I have tried reducing the swappiness (see also: Wikipedia) to values of 10 and 0, but the problem still persists. It seems that after a day of inactivity the kernel believes the entire GUI is no longer needed and wipes it from RAM (swaps it to disk). The long running task is reading through a vast file tree and reading every file. So it might be the kernel is confused into thinking that caching would help. But on a single sweep of a 2 TB USB HD with ~1 billion file names, an extra GB RAM isn't going to help performance much. This is a cheap laptop with a sluggish hard drive. It simply can't load data back into RAM fast enough.

How can I tell Linux to only use swap space in an emergency? I don't want to run without swap. If something unexpected happens, and the OS suddenly needs an extra few GBs then I don't want tasks to get killed and would prefer start using swap. But at the moment, if I leave swap enabled, my laptop just can't be used when I need it.

The precise definition of an "emergency" might be a matter for debate. But to clarify what I mean: An emergency would be where the system is left without any other option than to swap or kill processes.

What is an emergency? - Do you really have to ask?... I hope you never find yourself in a burning building!

It's not possible for me to define everything that might constitute an emergency in this question. But for example, an emergency might be when the kernel is so pushed for memory that it has start killing processes with the OOM Killer. An emergency is NOT when the kernel thinks it can improve performance by using swap.

Final Edit: I've accepted an answer which does precisely what I've asked for at the operating system level. Future readers should also take note of the answers offering application level solutions.


@phuclv 2019-02-09 12:31:38

Having such a huge swap nowadays is often a bad idea. By the time the OS swapped just a few GB of memory to swap, your system had already crawled to death (like what you saw)

It's better to use zram with a small backup swap partition. Many OSes like ChromeOS, Android and various Linux distros have enabled zram by default for years, especially for systems with less RAM. It's much faster than swap on HDD and you can clearly feel the system responsiveness in this case. Less so on an SSD, but according to the benchmark results here it still seems faster even with the default lzo algorithm. You can change to lz4 for even better performance with a little bit less compression ratio. It's decoding speed is nearly 5 times faster than lzo based on official benchmark

There's also zswap although I've never used it. Probably worth a try and compare which one is better for your usecases

After that another suggestion is to reduce the priority of those IO-bound processes and possibly leave a terminal running on higher priority so that you can run commands on it right away even when the system is on a high load

Further reading

@Philip Couling 2019-02-09 13:52:06

Just so that I understand, you are saying that I can create a zram block device, use it as swap, with a lower priority swap as the HDD partition?

@phuclv 2019-02-09 13:55:52

@PhilipCouling if you're using HDD then yes, definitely you should use a zram or similar solutions. The priority of the swap should be lower than zram, so that Linux tries to use up the zram first, and then it'll consider the swap. If you use Ubuntu then the zram-config package already takes care of the priority settings for you

@Philip Couling 2019-02-15 13:02:53

I'm accepting this answer because it appears to do exactly what I've asked for. If I still have my 16GB swap enabled at a reduced priority, then the kernel will only use it when zswap has been exhausted. IE: "in an emergency". Note on debian-buster this is very easy to setup, simply by installing the zram-tools.

@Tyþë-Ø 2019-02-08 15:57:44

Remove swap or diminish it about 20% (may vary with systems) as recently OSs are not using swap anymore the same way as they did in the few years ago. It probably answers some of your question:

--> official

some of the Red Hat info below,

In the past, some application vendors recommended swap of a size equal to the RAM, or even twice the RAM. Now let us imagine the above-mentioned system with 2GB of RAM and 2GB of swap. A database on the system was by mistake configured for a system with 5GB of RAM. Once the physical memory is used up, swap gets used. As the swap disk is much slower than RAM, the performance goes down, and thrashing occurs. At this point, even logins into the system might become impossible. As more and more memory gets written to, eventually both physical- and swap memory are completely exhausted and the OOM killer kicks in, killing one or more processes. In our case, quite a lot of swap is available, so the time of poor performance is long.


portion of Debian link above,

Information and considerations related to the amount of swap to use:

"The recommended amount of swap space has traditionally been double the amount of system memory. This has changed over time to one and half times system memory, both answers are decent baselines but are becoming less and less useful answers to the question as time passes. There are many variables about your system and intended use that will determine the available system swap you will want to have."

You may try:

"Best way to disable swap in linux"

Personal note:

Since I've 6 GB RAM and in all my recently Linux OS. I've never seen any indication of use of Swap. I determined it I must turn it off either for space (few gigabytes more) and because it has slowed my system sometimes.

@Pryftan 2019-02-10 14:10:10

In the past, some application vendors recommended swap of a size equal to the RAM, or even twice the RAM. I feel much older seeing that, somehow... Even though I still have one of the HDDs at the ~528MB barrier and also a 2.5GB, somehow that quote - well it's something from so very long ago... Interesting quote though and it might explain why I saw similar problems a few years ago. I believe I used sysctl to fix it but I don't remember exactly what setting if that was it eve.

@jlliagre 2019-02-08 15:27:52

It seems that after a day of inactivity the kernel believes the entire GUI is no longer needed and wipes it from RAM (swaps it to disk).

The kernel is doing The Right Thing™ believing it. Why would it keep unused1 memory in RAM and so essentially waste it instead of using it as cache or something?

I don't think the Linux kernel is gratuitously or anticipatory swapping out pages, so if it does it that must be to store something else on RAM, thus improving performance of your long running task, or at least with this goal.

If you know when you'll need to reuse your laptop in advance, you might use the at command (or crontab) to schedule a swap cleanup (swapoff -a;swapon -a).

As cleaning the swap might be overkill, and even trigger the OOM killer if for some reason, not everything fit in RAM, you might just "unswap"2 everything related to the running applications you want to revive.

One way to do it would be to attach a debugger like gdb to each of the affected processes and trigger a core dump generation:

# gdb -p <pid>
generate-core-dump /dev/null

As you wrote, your long running application is not reusing the data it reads after the initial pass, so you are in a specific case where long term caching is not useful. Then bypassing the cache by using direct I/O like suggested by Will Crawford should be a good workaround.

Alternatively, you might just regularly flush the file cache by echoing 1 or 3 to the /proc/sys/vm/drop_caches pseudo-file before the OS thinks it's a good idea to swap out your GUI applications and environment.

See How do you empty the buffers and cache on a Linux system? for details.

1Unused in the sense: no more actively used since a significant period of time, the memory still being relevant to its owners.
2Put back in RAM pages stored on the swap area.

@Philip Couling 2019-02-08 15:49:15

Thanks for the thought on possible causes. I've added a little to the question since it might be relevant. I wonder if there's a way to lower the priority of caching against application's own memory.

@Jörg W Mittag 2019-02-09 04:56:55

"I don't think the Linux kernel is gratuitously or anticipatory swapping out pages so if it does it, that must be to store something else on RAM, thus improving performance." – I think this wording is a bit ambiguous. The kernel will definitely write pages to swap, whenever it has the chance (e.g. there is little disk I/O). It will, however, not remove them from RAM. That way, you have the best of both worlds: if you quickly need those pages again, they are already in RAM, and there is nothing to do. If an emergency (as the OP put it) arises, you simply need free those pages in RAM, because

@Jörg W Mittag 2019-02-09 04:58:18

… they are already in swap. And that is precisely why you do not want to use swap "only in emergencies", because during an emergency, the system is already under stress and the last thing you want is add large amounts of disk I/O to that.

@jpmc26 2019-02-09 05:00:17

The thing causing it to swap out is likely the long running process: it's accessing files on disk. Those files in memory will have been more recently used than the GUI's memory.

@jlliagre 2019-02-09 21:07:09

@JörgWMittag Do you have evidence the Linux kernel is, when the I/O usage is low, preemptively writing pages to the swap area "just in case", i.e. without freeing them from the RAM?

@Pryftan 2019-02-10 14:14:30

Why would it keep in RAM unused memory and so essentially waste it instead of using it as cache or something? Maybe because RAM is faster than swap space ? By a lot. In fact by heaps. Though rereading your comment I wonder if I misinterpreted your statement. Even so my point is true whether or no. I have seen serious performance hits from swap space though in recent years and I ended up (with some reservations) disabling swap on that box.

@jlliagre 2019-02-10 16:26:12

@Pryftan I fully agree that RAM is faster than hard disks or SSDs, but, unless you have an "unlimited" amount of RAM, it makes no sense to keep data in it if that data stays unused for a significant period of time. When this happens, RAM that could have been better used with storing "hot" data is wasted, so the overall performance is degraded. Just like it makes more sense to store non moving vehicles in a parking lot than to keep them randomly parked on the freeway

@Pryftan 2019-02-10 19:03:29

@jlliagre Yes.. If there are things that could be stored in RAM, at least. And perhaps that's what you were saying. In which case - well you know. Even so as noted I have seen performance hits though I wasn't sure why; it seemed rather odd to me but thinking on it it probably was that a lot of swap was being used as (I dimly recall) the box in question had not much RAM. Upgrading RAM helped tremendously but I believe I might have changed swap settings too.

@Will Crawford 2019-02-08 19:15:41

Is the process you're running something you've created yourself?

If so, it might be worth tweaking your code to open the files using the O_DIRECT flag, which to quote the manual page -

Try to minimize cache effects of the I/O to and from this file. In general this will degrade performance, but it is useful in special situations, such as when applications do their own caching. File I/O is done directly to/from user-space buffers. The O_DIRECT flag on its own makes an effort to transfer data synchronously, but does not give the guarantees of the O_SYNC flag that data and necessary metadata are transferred. To guarantee synchronous I/O, O_SYNC must be used in addition to O_DIRECT. See NOTES below for further discussion.

@derobert 2019-02-10 21:39:58

Another similar (but probably easier, as I'm pretty sure O_DIRECT has alignment restrictions and you'll kill performance if your reads aren't large) is fadvise to tell the kernel you won't need that data again, flushing it from the page cache. (on phone or would provide links, sorry)

@sourcejedi 2019-02-11 11:46:56

@derobert For one, the nocache command is a convenient hack to do this. (It uses LD_PRELOAD to hijack some libc calls).

@derobert 2019-02-08 16:07:48

One fix is to make sure the memory cgroup controller is enabled (I think it is by default in even half-recent kernels, otherwise you'll need to add cgroup_enable=memory to the kernel command line). Then you can run your I/O intensive task in a cgroup with a memory limit, which also limits the amount of cache it can consume.

If you're using systemd, you can set +MemoryAccounting=yes and either MemoryHigh/MemoryMax or MemoryLimit (depeneds on if you're using cgroup v1 or v2) in the unit, or a slice containing it. If its a slice, you can use systemd-run to run the program in the slice.

Full example from one of my systems for running Firefox with a memory limit. Note this uses cgroups v2 and is set up as my user, not root (one of the advantages of v2 over v1 is that delegating this to non-root is safe, so systemd does it).

$ systemctl --user cat mozilla.slice 
# /home/anthony/.config/systemd/user/mozilla.slice
Description=Slice for Mozilla apps


$ systemd-run --user --slice mozilla.slice --scope -- /usr/bin/firefox &
$ systemd-run --user --slice mozilla.slice --scope -- /usr/bin/thunderbird &

I found to get the user one working I had to use a slice. System one works just by putting the options in the service file (or using systemctl set-property on the service).

Here is an example service (using cgroup v1), note the last two lines. This is part of the system (pid=1) instance.

Description=mount S3QL filesystem


ExecStartPre=+/bin/sh -c 'printf "S3QL_CACHE_SIZE=%%i\n" $(stat -c "%%a*%%S*.90/1024" -f /srv/s3ql-cache/ | bc) > /run/local-s3ql-env'
ExecStartPre=/usr/bin/fsck.s3ql  --cachedir /srv/s3ql-cache/fs1 --authfile /etc/s3ql-authinfo  --log none «REDACTED»
ExecStart=/usr/bin/mount.s3ql --keep-cache --cachedir /srv/s3ql-cache/fs1 --authfile /etc/s3ql-authinfo --cachesize ${S3QL_CACHE_SIZE} --threads 4
ExecStop=/usr/bin/umount.s3ql /mnt/S3QL/

Documentation is in systemd.resource-control(5).

@Old Pro 2019-02-10 19:53:57

Can’t you do something comparable and portable by just using ulimit?

@derobert 2019-02-10 21:35:27

@OldPro not really. First, there isn't AFAIK a ulimit on total memory usage including page cache (which is the usage that is becoming excessive here). Second, ulimit for memory is per-process, cgroups work even if the long-running task forks.

@sourcejedi 2019-02-11 11:41:08

I thought the reason memory accounting is enabled by default on newer systems is due to a change in systemd version 238.

@derobert 2019-02-11 14:46:45

@sourcejedi that's relatively recent. When the memory controller was first introduced, just having it available (not even in use) had a large enough performance cost that some distros at least disabled it by default and you had to pass that kernel command line argument to enable it. The performance problems were fixed, so that changed, and more recently systemd activates it too by default.

@X Tian 2019-02-08 15:33:20

Here's an idea, which I haven't tried myself (and I'm sorry I haven't the time right now to experiment with this).

Suppose you create a small VM with only 512MB memory for your background process I'm not sure if you'd want this to have any swap, your call, and switch off swap on your host system.

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Ubuntu 16.04 swap space fills up, never releases

  • 2018-06-15 12:33:38
  • ghukill
  • 809 View
  • 0 Score
  • 1 Answer
  • Tags:   ubuntu memory swap

1 Answered Questions

[SOLVED] linux - OOM / swap not being used

1 Answered Questions

[SOLVED] When does the swap get emptied, after being used?

  • 2016-08-03 13:07:29
  • posdef
  • 105 View
  • 2 Score
  • 1 Answer
  • Tags:   memory swap

1 Answered Questions

Process Being Killed due to low swap

2 Answered Questions

[SOLVED] Spike in swap utilisation without high swap rate

1 Answered Questions

[SOLVED] Mysql / swap space full

  • 2011-08-03 11:17:49
  • bvishal4u
  • 1981 View
  • 2 Score
  • 1 Answer
  • Tags:   swap mysql

Sponsored Content