Arch linux vm swappiness

Arch Linux

Reducing the swappiness setting is widely recommended across the Internet as a catch-all means to improve system performance. Many users set it to low values like 5 and 10, as I did myself for many years, in the hope that it will make their system faster.

This simply isn’t true. In my experience on a laptop with 4GB of RAM, setting swappiness to 100 results in noticeably increased system performance, at least under my typical workload.

Let’s take a look at why this might be the case. Suppose that a user has a very large and memory intensive program (let’s call it Android Studio) running in the background, but they’re not actually using it right now. Instead, they have switched to another desktop, and are doing some light web browsing. At swappiness=0, they would find that their browser is extremely sluggish, and moreover, remains sluggish no matter how long they continue using it. They are baffled by this. «After all,» they say, «the browser only requires a small amount of data to be in RAM for it to run fast — just the browser executable, its runtime data, and a few databases. Sure it was slow when I first first opened it, because that data was on disk. But now that data is in RAM, and it should stay there because I’m still actively using it? Right?». Well, no. What the user has overlooked is that at swappiness=0, the system prioritises keeping Android Studio’s runtime data in memory over the browser’s databases and other working files. So their browser databases are put out to disk, loaded into RAM each time the browser reads from them, which is all the time, then immediately evicted from RAM again. This results in disk thrashing, and system performance grinds to a halt.

The moral of the story is that if you have a lot of background processes, it doesn’t make sense to set swappiness=0, because that’s saying you would rather keep them in memory over what you’re currently working on. By contrast, swappiness=100 sets an equal balance between runtime data and disk-backed files, meaning the data necessary to keep your browser working fast will stay in RAM, like it should. In general, the default of 60 should also work well.

For some reason, there is no mention of these facts on the Arch Wiki. Are they correct?

Last edited by PBS (2021-11-12 21:01:10)

#2 2021-11-12 08:18:28

Re: Myths about swappiness and performance

And yes: suggestions to use low vm.swappiness are usually retarded bullshit by people who genuinly believe that otherwise the kernel will simply prefer swap over ram…
https://wiki.archlinux.org/title/Swap#Performance is kinda misleading w/ an unreferenced anecdotal «low value on sufficient memory is known to improve responsiveness on many systems» («many people say…»)
https://rudd-o.com/linux-and-free-softw … o-fix-that presents an artificial test that turns processes inactive and charges the file cache and lo and behold, the file cache gets the lion share of the RAM because it needs it… *headshakingemoji*

Читайте также:  Переместить файл командная строка линукс

#3 2021-11-12 08:46:28

ngoonee Forum Fellow From: Between Thailand and Singapore Registered: 2009-03-17 Posts: 7,345

Re: Myths about swappiness and performance

This is interesting, though I have no direct experience as I’ve never bothered with swappiness (current machine has 32 GB of RAM and it just has not been a problem for me so far). I do think the wiki page needs updating though.

Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

#4 2021-11-12 09:10:50

Re: Myths about swappiness and performance

@seth I already know about those two pages. The last one is linked from the Arch Wiki, despite containing several misconceptions. It does however raise one good point, which is that it would be nice to have a utility for downloading a big file without displacing anything out of cache.

Last edited by PBS (2021-11-12 09:14:17)

#5 2021-11-12 09:46:04

Re: Myths about swappiness and performance

It’s possible to avoid the file cache, https://unix.stackexchange.com/question … ific-files and dd actually has a flag for that.
https://unix.stackexchange.com/question … stem-cache links an effort to wrap that using LD_PRELOAD

I haven’t (yet) read https://lwn.net/Articles/789623/ but it certainkly looks related

#6 2021-11-12 11:00:12

Re: Myths about swappiness and performance

Reducing the swappiness setting is widely recommended [. ]
on the Arch Wiki. Are they correct?

A lot has changed since then including (but not-limited to) zswap by default and better cost-estimation by the LRU:
https://lwn.net/Articles/821105/
https://lwn.net/Articles/851184/ (mm)

In general, the default of 60 should also work well.

Источник

Swap

This page provides an introduction to swap space and paging on GNU/Linux. It covers creation and activation of swap partitions and swap files.

Linux divides its physical RAM (random access memory) into chunks of memory called pages. Swapping is the process whereby a page of memory is copied to the preconfigured space on the hard disk, called swap space, to free up that page of memory. The combined sizes of the physical memory and the swap space is the amount of virtual memory available.

Support for swap is provided by the Linux kernel and user-space utilities from the util-linux package.

Swap space

Swap space can take the form of a disk partition or a file. Users may create a swap space during installation or at any later time as desired. Swap space can be used for two purposes, to extend the virtual memory beyond the installed physical memory (RAM), and also for suspend-to-disk support.

If it is beneficial to extend the virtual memory with swap depends on the amount of installed physical memory. If the amount of physical memory is less than the amount of memory required to run all the desired programs, then it may be beneficial to enable swap. This avoids out of memory conditions, where the Linux kernel OOM killer mechanism will automatically attempt to free up memory by killing processes. To increase the amount of virtual memory to the required amount, add the necessary difference (or more) as swap space.

Читайте также:  Kali linux wifi все приложения

The biggest drawback of enabling swap is its lower performance, see section #Performance. Hence, enabling swap is a matter of personal preference: some prefer programs to be killed over enabling swap and others prefer enabling swap and slower system when the physical memory is exhausted.

The factual accuracy of this article or section is disputed.

Reason: Linus Torvalds says that swap partitions are preferable: «all the normal distributions set things up with swap partitions, not files, because honestly, swapfiles tend to be slower and have various other complexity issues.» (Discuss in Talk:Swap)

Or to show physical memory as well as swap usage:

Swap partition

A swap partition can be created with most GNU/Linux partitioning tools. Swap partitions are designated as type 82 on MBR and 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F on GPT.

To set up a partition as Linux swap area, the mkswap(8) command is used. For example:

To enable the device for paging:

To enable this swap partition on boot, add an entry to /etc/fstab :

UUID=device_UUID none swap defaults 0 0

where the device_UUID is the UUID of the swap space.

Note: The fstab-entry is optional if the swap partition is located on a device using GPT. See #Activation by systemd.

Warning: Enabling discard on RAID setups using mdadm will cause system lockup on boot and during runtime, if using swapon.

Activation by systemd

systemd activates swap partitions based on two different mechanisms. Both are executables in /usr/lib/systemd/system-generators . The generators are run on start-up and create native systemd units for mounts. The first, systemd-fstab-generator , reads the fstab to generate units, including a unit for swap. The second, systemd-gpt-auto-generator inspects the root disk to generate units. It operates on GPT disks only, and can identify swap partitions by their type GUID, see systemd#GPT partition automounting for more information.

Disabling swap

To deactivate specific swap space:

Alternatively use the -a switch to deactivate all swap space.

Since swap is managed by systemd, it will be activated again on the next system startup. To disable the automatic activation of detected swap space permanently, run systemctl —type swap to find the responsible .swap unit and mask it.

Swap file

As an alternative to creating an entire partition, a swap file offers the ability to vary its size on-the-fly, and is more easily removed altogether. This may be especially desirable if disk space is at a premium (e.g. a modestly-sized SSD).

Swap file creation

Use dd to create a swap file the size of your choosing. For example, creating an 8 GiB swap file:

# dd if=/dev/zero of=/swapfile bs=1M count=8k status=progress

Note: Using dd to allocate a swap file is the most portable solution, see swapon(8) § Files with holes for details.

Set the right permissions (a world-readable swap file is a huge local vulnerability):

After creating the correctly sized file, format it to swap:

Finally, edit the fstab configuration to add an entry for the swap file:

/swapfile none swap defaults 0 0

For additional information, see fstab#Usage.

Note: The swap file must be specified by its location on the file system, not by its UUID or LABEL.

Читайте также:  Linux list all connection

Swap file removal

To remove a swap file, it must be turned off first and then can be removed:

# swapoff /swapfile # rm -f /swapfile

Finally, remove the relevant entry from /etc/fstab .

Compressed block device in RAM

zswap is available by default if you use a swap file or partition, but you can avoid swap files or partitions altogether by using a compressed block device in RAM, using zram. See Improving performance#zram or zswap for more information on the differences between using zram or zswap.

Swap encryption

Performance

Swap operations are usually significantly slower than directly accessing data in RAM. Disabling swap entirely to improve performance can sometimes lead to a degradation, since it decreases the memory available for virtual file system (VFS) caches, causing more frequent and costly disk usage.

Swap values can be adjusted to help performance:

Swappiness

When memory usage reaches a certain threshold, the kernel starts looking at active memory and seeing what it can free up. File data can be written out to the file system (if changed), unloaded and re-loaded later; other data must be written to swap before it can be unloaded.

Note: There is a common misconception that swappiness affects the memory threshold or prevents using swap space, but it only affects the preference for freeing up file pages over swap. See: this article for a more detailed explanation or the the kernel source code where it is used.

To check the current swappiness value:

Alternatively, the files /sys/fs/cgroup/memory/memory.swappiness (cgroup v1-specific) or /proc/sys/vm/swappiness can be read in order to obtain the raw integer value.

To temporarily set the swappiness value:

To set the swappiness value permanently, create a sysctl.d(5) configuration file. For example:

/etc/sysctl.d/99-swappiness.conf

To have the boot loader set swappiness when loading the kernel, add a kernel parameter, e.g. sysctl.vm.swappiness=10 .

To test and more on why this may work, take a look at this article. For a more recent counter-argument, see this article.

VFS cache pressure

Another sysctl parameter that affects swap performance is vm.vfs_cache_pressure , which controls the tendency of the kernel to reclaim the memory which is used for caching of VFS caches, versus pagecache and swap. Increasing this value increases the rate at which VFS caches are reclaimed[1]. For more information, see the Linux kernel documentation.

Priority

If you have more than one swap file or swap partition you should consider assigning a priority value (0 to 32767) for each swap area. The system will use swap areas of higher priority before using swap areas of lower priority. For example, if you have a faster disk ( /dev/sda ) and a slower disk ( /dev/sdb ), assign a higher priority to the swap area located on the fastest device. Priorities can be assigned in fstab via the pri parameter:

/dev/sda1 none swap defaults,pri=100 0 0 /dev/sdb2 none swap defaults,pri=10 0 0

Or via the —priority parameter of swapon:

# swapon --priority 100 /dev/sda1

If two or more areas have the same priority, and it is the highest priority available, pages are allocated on a round-robin basis between them.

Striping

There is no necessity to use RAID for swap performance reasons. The kernel itself can stripe swapping on several devices, if you just give them the same priority in the /etc/fstab file. Refer to The Software-RAID HOWTO for details.

See also

Источник

Оцените статью
Adblock
detector