Difference between revisions of "Swap/de"
Views
Actions
Namespaces
Variants
Tools
(Updating to match new version of source page) |
(Updating to match new version of source page) |
||
Line 16: | Line 16: | ||
Dieser Artikel versucht, so umfassend wie möglich zu sein, aber noch mehr Informationen finden Sie in den [[ | <span class="mw-translate-fuzzy">Dieser Artikel versucht, so umfassend wie möglich zu sein, aber noch mehr Informationen finden Sie in den [[Swap#See_Also|unten verlinkten Artikel]]</span> | ||
=Brauche ich eine Auslagerung?= | =Brauche ich eine Auslagerung?= | ||
Line 34: | Line 34: | ||
* Der einzige Nachteil, wenn Sie mehr Auslagerungsspeicher haben, als Sie tatsächlich benötigen, ist, dass der dafür reservierte Speicherplatz nicht für Anwendungs- oder Systemdaten verwendet werden kann. | * Der einzige Nachteil, wenn Sie mehr Auslagerungsspeicher haben, als Sie tatsächlich benötigen, ist, dass der dafür reservierte Speicherplatz nicht für Anwendungs- oder Systemdaten verwendet werden kann. | ||
Der "abnehmende Nutzen" bedeutet, dass Sie, wenn Sie mehr Auslagerungsspeicher als das Doppelte Ihrer RAM-Größe benötigen, besser mehr RAM hinzufügen sollten, da der Zugriff auf die Festplatte (HDD) etwa 10³ langsamer ist als der Zugriff auf den RAM, so dass etwas, das 1 Sekunde dauern würde, plötzlich mehr als 15 Minuten dauert! Und auf einem Halbleiterlaufwerk (Solid State Drive, SSD) dauert derselbe Vorgang, der im RAM 1 Sekunde dauerte, auf dem SSD immer noch etwa 1 Minute! | <span class="mw-translate-fuzzy">Der "abnehmende Nutzen" bedeutet, dass Sie, wenn Sie mehr Auslagerungsspeicher als das Doppelte Ihrer RAM-Größe benötigen, besser mehr RAM hinzufügen sollten, da der Zugriff auf die Festplatte (HDD) etwa 10³ langsamer ist als der Zugriff auf den RAM, so dass etwas, das 1 Sekunde dauern würde, plötzlich mehr als 15 Minuten dauert! Und auf einem Halbleiterlaufwerk (Solid State Drive, SSD) dauert derselbe Vorgang, der im RAM 1 Sekunde dauerte, auf dem SSD immer noch etwa 1 Minute!</span> | ||
Unter Berücksichtigung aller oben genannten Punkte ergibt sich die folgende Tabelle: | Unter Berücksichtigung aller oben genannten Punkte ergibt sich die folgende Tabelle: | ||
Line 78: | Line 78: | ||
if [ "$(whoami)" = 'root' ]; then | if [ "$(whoami)" = 'root' ]; then | ||
dmesg | grep "zswap:" | grep --silent "load" | dmesg | grep "zswap:" | grep --silent "load" | ||
if | if [ $? -eq 0 ]; then | ||
# zswap is active | # zswap is active | ||
echo "zswap information:" | echo "zswap information:" | ||
Line 97: | Line 97: | ||
==Erstellen und Aktivieren einer Auslagerungspartition== | ==Erstellen und Aktivieren einer Auslagerungspartition== | ||
Um eine Auslagerungspartition zu erstellen, benötigen Sie genügend nicht zugewiesenen Speicherplatz, um eine zusätzliche Partition zu erstellen. Wenn Sie nicht genügend Platz haben, springen Sie zum nächsten Abschnitt [[ | <div class="mw-translate-fuzzy"> | ||
Um eine Auslagerungspartition zu erstellen, benötigen Sie genügend nicht zugewiesenen Speicherplatz, um eine zusätzliche Partition zu erstellen. Wenn Sie nicht genügend Platz haben, springen Sie zum nächsten Abschnitt [[#Using_a_Swapfile|#Verwenden_einer_Auslagerungsdatei]]. Eine Auslagerungspartition kann in jeder Datenträgerverwaltung / Partitionsverwaltung erstellt werden und sollte als Typ {{ic|linuxswap}} eingerichtet werden.#Using_a_Swapfile | |||
</div> | |||
Sobald Sie eine Auslagerungspartition haben, müssen Sie die Auslagerungspartition mit {{ic|mkswap}} initialisieren. Wenn Ihre Auslagerungspartition zum Beispiel {{ic|/dev/sda3}} ist, könnten Sie diesen Befehl verwenden: | Sobald Sie eine Auslagerungspartition haben, müssen Sie die Auslagerungspartition mit {{ic|mkswap}} initialisieren. Wenn Ihre Auslagerungspartition zum Beispiel {{ic|/dev/sda3}} ist, könnten Sie diesen Befehl verwenden: | ||
Line 108: | Line 110: | ||
sudo bash -c "echo UUID=$(lsblk -no UUID /dev/sda3) none swap defaults 0 0 >> /etc/fstab" | sudo bash -c "echo UUID=$(lsblk -no UUID /dev/sda3) none swap defaults 0 0 >> /etc/fstab" | ||
If you would like to have more than one swap partition, simply repeat the steps above for any additional partitions. | <span lang="en" dir="ltr" class="mw-content-ltr">If you would like to have more than one swap partition, simply repeat the steps above for any additional partitions.</span> | ||
{{note|<span lang="en" dir="ltr" class="mw-content-ltr">Be sure to replace /dev/sda3 in the above commands with your actual swap partition.</span>}} | |||
{{tip|<span lang="en" dir="ltr" class="mw-content-ltr">''If you're using a HDD'' (spinning rust) put the swap partition *at the beginning of the disk* as the speed of the disk is higher on the inside tracks. On an SSD this doesn't matter.</span>}} | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
=Using a Swapfile= | =Using a Swapfile= | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
Using a swap partition has one major disadvantage: Changing the size of swap or adding swap requires repartitioning the disk. In current Linux kernels, it is possible to use a swap file instead of a dedicated partition and as from kernel 2.6 onwards there is no performance difference any more between the two. | Using a swap partition has one major disadvantage: Changing the size of swap or adding swap requires repartitioning the disk. In current Linux kernels, it is possible to use a swap file instead of a dedicated partition and as from kernel 2.6 onwards there is no performance difference any more between the two. | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
==Creating and Enabling a Static Swapfile== | ==Creating and Enabling a Static Swapfile== | ||
{{tip|There is no reason you can't have both a swap partition and a swapfile. This is an easy way to add more swap without repartitioning.}} | </div> | ||
{{tip|<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
There is no reason you can't have both a swap partition and a swapfile. This is an easy way to add more swap without repartitioning. | |||
</div>}} | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
First create and intialize the file to hold the swap. For example, to create a 4GB swapfile, you could use the command: | First create and intialize the file to hold the swap. For example, to create a 4GB swapfile, you could use the command: | ||
</div> | |||
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress | sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
Set the appropriate permissions on the file. It should be readable and writable only by {{ic|root}}. This can be done with the command: | Set the appropriate permissions on the file. It should be readable and writable only by {{ic|root}}. This can be done with the command: | ||
</div> | |||
sudo chmod 600 /swapfile | sudo chmod 600 /swapfile | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
Next we need to format and enable the swapfile: | Next we need to format and enable the swapfile: | ||
</div> | |||
sudo mkswap /swapfile | sudo mkswap /swapfile | ||
Line 137: | Line 154: | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
In order to ensure that the swap is enabled at boot we can add an entry to {{ic|/etc/fstab}}. You can add the line to fstab manually or using the command: | In order to ensure that the swap is enabled at boot we can add an entry to {{ic|/etc/fstab}}. You can add the line to fstab manually or using the command: | ||
</div> | |||
sudo bash -c "echo /swapfile none swap defaults 0 0 >> /etc/fstab" | sudo bash -c "echo /swapfile none swap defaults 0 0 >> /etc/fstab" | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
==Swapfiles on BTRFS== | ==Swapfiles on BTRFS== | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
As of kernel 5.0 and higher, swapfiles are supported on btfrs. They still require some special handling in addition to the above steps. | As of kernel 5.0 and higher, swapfiles are supported on btfrs. They still require some special handling in addition to the above steps. | ||
</div> | |||
Prior to running the {{ic| | <span lang="en" dir="ltr" class="mw-content-ltr">Prior to running the {{ic|dd}} step above, you should run these commands:</span> | ||
sudo truncate -s 0 /swapfile | sudo truncate -s 0 /swapfile | ||
sudo chattr +C /swapfile | sudo chattr +C /swapfile | ||
Line 152: | Line 175: | ||
These commands create an empty swapfile, disable COW for that file and ensure that compression is disabled. | <span lang="en" dir="ltr" class="mw-content-ltr">These commands create an empty swapfile, disable COW for that file and ensure that compression is disabled.</span> | ||
{{tip|To stay compatible with Timeshift, it is necessary to create the swapfile outside the root subvolume "@". | {{tip|<div lang="en" dir="ltr" class="mw-content-ltr"> | ||
To stay compatible with Timeshift, it is necessary to create the swapfile outside the root subvolume "@". | |||
{{RootCmd|command=btrfs subvolume create /@swapfile}} | {{RootCmd|command=btrfs subvolume create /@swapfile}} | ||
Just replace /swapfile with /@swapfile/swapfile.}} | Just replace /swapfile with /@swapfile/swapfile. | ||
</div>}} | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
==Swapfiles on ZFS== | ==Swapfiles on ZFS== | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
zfs doesn't support swapfiles, however you can achieve a similar benefit using a zvol as a swap volume. | zfs doesn't support swapfiles, however you can achieve a similar benefit using a zvol as a swap volume. | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
Detailed instructions on how to accomplish this can be found in this [https://github.com/zfsonlinux/pkg-zfs/wiki/HOWTO-use-a-zvol-as-a-swap-device ZoL guide]. | Detailed instructions on how to accomplish this can be found in this [https://github.com/zfsonlinux/pkg-zfs/wiki/HOWTO-use-a-zvol-as-a-swap-device ZoL guide]. | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
=Automated Swap Management with systemd-swap= | =Automated Swap Management with systemd-swap= | ||
{{note|Using systemd-swap with on btrfs/zfs or with hibernation support requires special handling beyond the scope of this article}} | </div> | ||
{{note|<span lang="en" dir="ltr" class="mw-content-ltr">Using systemd-swap with on btrfs/zfs or with hibernation support requires special handling beyond the scope of this article</span>}} | |||
It is possible to automatically manage the size of your swapfile using {{ic|systemd-swap}}. It can create swap files dynamically based on memory needs. It is also an easy way to enable/manage [[#Using_zswap_with_systemd-swap|zswap]] | <span lang="en" dir="ltr" class="mw-content-ltr">It is possible to automatically manage the size of your swapfile using {{ic|systemd-swap}}. It can create swap files dynamically based on memory needs. It is also an easy way to enable/manage [[#Using_zswap_with_systemd-swap|zswap]]</span> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
==Installing and Configuring systemd-swap== | ==Installing and Configuring systemd-swap== | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
First, install and enable {{ic|systemd-swap}}: | First, install and enable {{ic|systemd-swap}}: | ||
</div> | |||
pamac install systemd-swap | pamac install systemd-swap | ||
sudo systemctl enable systemd-swap.service | sudo systemctl enable systemd-swap.service | ||
Next enable dynamic swap management by creating a file {{ic|/etc/systemd/swap.conf.d/myswap.conf}} with the following parameters: | <span lang="en" dir="ltr" class="mw-content-ltr">Next enable dynamic swap management by creating a file {{ic|/etc/systemd/swap.conf.d/myswap.conf}} with the following parameters:</span> | ||
zswap_enabled=0 | zswap_enabled=0 | ||
zram_enabled=0 | zram_enabled=0 | ||
Line 183: | Line 220: | ||
If you would prefer a single command to create this file you could use: | <span lang="en" dir="ltr" class="mw-content-ltr">If you would prefer a single command to create this file you could use:</span> | ||
sudo bash -c 'echo -e "zswap_enabled=0\nzram_enabled=0\nswapfc_enabled=1" > /etc/systemd/swap.conf.d/myswap.conf' | sudo bash -c 'echo -e "zswap_enabled=0\nzram_enabled=0\nswapfc_enabled=1" > /etc/systemd/swap.conf.d/myswap.conf' | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
==Remove Other Swap== | ==Remove Other Swap== | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
In order for systemd-swap to be effective you need to remove any traditional swap devices you have. | In order for systemd-swap to be effective you need to remove any traditional swap devices you have. | ||
</div> | |||
To display your current swap devices you can use the command {{ic|swapon}}. For example, my test machine displays the following: | <span lang="en" dir="ltr" class="mw-content-ltr">To display your current swap devices you can use the command {{ic|swapon}}. For example, my test machine displays the following:</span> | ||
swapon | swapon | ||
NAME TYPE SIZE USED PRIO | NAME TYPE SIZE USED PRIO | ||
Line 198: | Line 239: | ||
In this example there are two swap devices. A swapfile and a swap partition. To turn them off we can use the command {{ic|swapoff}}. | <span lang="en" dir="ltr" class="mw-content-ltr">In this example there are two swap devices. A swapfile and a swap partition. To turn them off we can use the command {{ic|swapoff}}.</span> | ||
sudo swapoff /swapfile | sudo swapoff /swapfile | ||
sudo swapoff /dev/sda3 | sudo swapoff /dev/sda3 | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
Next we need to remove them. | Next we need to remove them. | ||
The swap partition will need to be deleted using your favorite partitioning tool. You can remove the swapfile by simply deleting it. | The swap partition will need to be deleted using your favorite partitioning tool. You can remove the swapfile by simply deleting it. | ||
</div> | |||
sudo rm /swapfile | sudo rm /swapfile | ||
We need to also remove any lines related to swap from {{ic|/etc/fstab}}. Edit the file with an [[Special:MyLanguage/Viewing_and_editing_configuration_files|appropriate text editor]] and remove or comment out the lines for the swap space. In the example above, we would remove these lines: | <span lang="en" dir="ltr" class="mw-content-ltr">We need to also remove any lines related to swap from {{ic|/etc/fstab}}. Edit the file with an [[Special:MyLanguage/Viewing_and_editing_configuration_files|appropriate text editor]] and remove or comment out the lines for the swap space. In the example above, we would remove these lines:</span> | ||
UUID=c2430cf1-8ea9-4422-a5c5-5a38779194c3 swap swap defaults,noatime 0 2 | UUID=c2430cf1-8ea9-4422-a5c5-5a38779194c3 swap swap defaults,noatime 0 2 | ||
/swapfile none swap defaults 0 0 | /swapfile none swap defaults 0 0 | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
With hibernation configured it will also be necessary to edit {{ic|/etc/default/grub}}. Find and remove option {{ic|<nowiki>resume=UUID=...</nowiki>}} or replace the UUID with a new proper value. In the example above, we would remove this text: | With hibernation configured it will also be necessary to edit {{ic|/etc/default/grub}}. Find and remove option {{ic|<nowiki>resume=UUID=...</nowiki>}} or replace the UUID with a new proper value. In the example above, we would remove this text: | ||
resume=UUID=c2430cf1-8ea9-4422-a5c5-5a38779194c3 | resume=UUID=c2430cf1-8ea9-4422-a5c5-5a38779194c3 | ||
Then run: | Then run: | ||
sudo update-grub | sudo update-grub | ||
If removing hibernation entirely then also delete {{ic|resume}} from the {{ic|HOOKS}} line in {{ic|/etc/ | If removing hibernation entirely then also delete {{ic|resume}} from the {{ic|HOOKS}} line in {{ic|/etc/mkinitcpio.conf}} and run: | ||
sudo | sudo mkinitcpio -P | ||
</div> | |||
Next reboot. After the reboot, when running {{ic|swapon}} you should see something similiar to the following: | <span lang="en" dir="ltr" class="mw-content-ltr">Next reboot. After the reboot, when running {{ic|swapon}} you should see something similiar to the following:</span> | ||
NAME TYPE SIZE USED PRIO | NAME TYPE SIZE USED PRIO | ||
/var/lib/systemd-swap/swapfc/1 file 512M 0B -2 | /var/lib/systemd-swap/swapfc/1 file 512M 0B -2 | ||
systemd-swap will add and remove swapfiles as your memory usage dictates. | <span lang="en" dir="ltr" class="mw-content-ltr">systemd-swap will add and remove swapfiles as your memory usage dictates.</span> | ||
{{note|If you don't delete your existing swap partition it will be automatically activated by systemd even though you have removed it from fstab}} | {{note|<span lang="en" dir="ltr" class="mw-content-ltr">If you don't delete your existing swap partition it will be automatically activated by systemd even though you have removed it from fstab</span>}} | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
==Using zswap with systemd-swap== | ==Using zswap with systemd-swap== | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
zswap is compressed swap kept in RAM. zswap keeps the most frequently used pages in RAM and writes less frequently used pages to the swap space on disk. In many workloads, this will result in increased swap performance and less wear and tear on SSDs. | zswap is compressed swap kept in RAM. zswap keeps the most frequently used pages in RAM and writes less frequently used pages to the swap space on disk. In many workloads, this will result in increased swap performance and less wear and tear on SSDs. | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
To enable zswap with systemd-swap, simply alter the instructions above and set Linux Kernel parameter zswap_enabled=1 | To enable zswap with systemd-swap, simply alter the instructions above and set Linux Kernel parameter zswap_enabled=1 | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
=Tuning & Performance Considerations= | =Tuning & Performance Considerations= | ||
</div> | |||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
Although swap seems like a great way to expand memory, excessive swap use will cause severe performance degradation, as mentioned before. | Although swap seems like a great way to expand memory, excessive swap use will cause severe performance degradation, as mentioned before. | ||
</div> | |||
There are couple of parameters that can be used to tune swap utilization. These are swappiness and vfs_cache_pressure. To see your current settings for these you can use the following commands: | <span lang="en" dir="ltr" class="mw-content-ltr">There are couple of parameters that can be used to tune swap utilization. These are swappiness and vfs_cache_pressure. To see your current settings for these you can use the following commands:</span> | ||
cat /proc/sys/vm/swappiness | cat /proc/sys/vm/swappiness | ||
cat /proc/sys/vm/vfs_cache_pressure | cat /proc/sys/vm/vfs_cache_pressure | ||
{{ic|swappiness}} controls how likely a page is to be transferred to swap. This value represents the percentage of the free memory before activating swap. The lower the value, the less swapping is used and the more memory pages are kept in physical memory where: | <span lang="en" dir="ltr" class="mw-content-ltr">{{ic|swappiness}} controls how likely a page is to be transferred to swap. This value represents the percentage of the free memory before activating swap. The lower the value, the less swapping is used and the more memory pages are kept in physical memory where:</span> | ||
* 0 disables swap | * <span lang="en" dir="ltr" class="mw-content-ltr">0 disables swap</span> | ||
* 60 is the default value which is ideal for a server that is memory-starved and running a lot of services | * <span lang="en" dir="ltr" class="mw-content-ltr">60 is the default value which is ideal for a server that is memory-starved and running a lot of services</span> | ||
* 100 is very aggressive swapping. | * <span lang="en" dir="ltr" class="mw-content-ltr">100 is very aggressive swapping.</span> | ||
For ''most'' desktop computers the recommended value is 10: Theoretically, this means to only start swapping when RAM usage reaches around 90 percent. | <span lang="en" dir="ltr" class="mw-content-ltr">For ''most'' desktop computers the recommended value is 10: Theoretically, this means to only start swapping when RAM usage reaches around 90 percent.</span> | ||
{{ic|vfs_cache_pressure}} used to be a percentage value that controls the tendency of the kernel to ''reclaim'' the memory which is used for caching of directory and inode objects with a default value of 100 on Kernels <5.4. On Kernels >=5.4 can be increased beyond 100. Increasing this value will increase the rate in which these objects are removed from the RAM cache. Decreasing it will allow these objects to be cached in memory longer, consuming additional RAM over time. Depending on your specific workload, increasing or decreasing this value too far can have significant negative impacts on system performance. Experimentation is needed to find the appropriate balance and the default value is reasonable. In general, it is more common to optimize swappiness before experimenting with vfs_cache_pressure. | <span lang="en" dir="ltr" class="mw-content-ltr">{{ic|vfs_cache_pressure}} used to be a percentage value that controls the tendency of the kernel to ''reclaim'' the memory which is used for caching of directory and inode objects with a default value of 100 on Kernels <5.4. On Kernels >=5.4 can be increased beyond 100.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Increasing this value will increase the rate in which these objects are removed from the RAM cache. Decreasing it will allow these objects to be cached in memory longer, consuming additional RAM over time.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Depending on your specific workload, increasing or decreasing this value too far can have significant negative impacts on system performance.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Experimentation is needed to find the appropriate balance and the default value is reasonable. In general, it is more common to optimize swappiness before experimenting with vfs_cache_pressure.</span> | ||
To set these values you can use the command {{ic|sysctl}}. For example, to set the swappiness value to 10 you could use: | <span lang="en" dir="ltr" class="mw-content-ltr">To set these values you can use the command {{ic|sysctl}}. For example, to set the swappiness value to 10 you could use:</span> | ||
sudo sysctl vm.swappiness=10 | sudo sysctl vm.swappiness=10 | ||
'''There is no preset defined answer on the optimal values for these parameters.''' Experimentation is needed to find the optimum configuration for your specific hardware and workload. | '''<span lang="en" dir="ltr" class="mw-content-ltr">There is no preset defined answer on the optimal values for these parameters.</span>''' <span lang="en" dir="ltr" class="mw-content-ltr">Experimentation is needed to find the optimum configuration for your specific hardware and workload.</span> | ||
<div lang="en" dir="ltr" class="mw-content-ltr"> | |||
=See Also= | =See Also= | ||
* The Arch Wiki page on [https://wiki.archlinux.org/index.php/Swap swap] | </div> | ||
* The Arch Wiki page on [https://wiki.archlinux.org/index.php/Zswap zswap] | * <span lang="en" dir="ltr" class="mw-content-ltr">The Arch Wiki page on [https://wiki.archlinux.org/index.php/Swap swap]</span> | ||
* The Linux kernel documentation on [https://www.kernel.org/doc/Documentation/vm/zswap.txt zswap] | * <span lang="en" dir="ltr" class="mw-content-ltr">The Arch Wiki page on [https://wiki.archlinux.org/index.php/Zswap zswap]</span> | ||
* The Linux kernel documentation on [https://www.kernel.org/doc/Documentation/sysctl/vm.txt swappiness and vfs_cache_pressure] | * <span lang="en" dir="ltr" class="mw-content-ltr">The Linux kernel documentation on [https://www.kernel.org/doc/Documentation/vm/zswap.txt zswap]</span> | ||
* The openSUSE guide to [https://doc.opensuse.org/documentation/leap/tuning/html/book.sle.tuning/cha.tuning.memory.html tuning memory] | * <span lang="en" dir="ltr" class="mw-content-ltr">The Linux kernel documentation on [https://www.kernel.org/doc/Documentation/sysctl/vm.txt swappiness and vfs_cache_pressure]</span> | ||
* The [https://github.com/Nefelim4ag/systemd-swap systemd-swap page] | * <span lang="en" dir="ltr" class="mw-content-ltr">The openSUSE guide to [https://doc.opensuse.org/documentation/leap/tuning/html/book.sle.tuning/cha.tuning.memory.html tuning memory]</span> | ||
* The ZFS on Linux guide on [https://github.com/zfsonlinux/pkg-zfs/wiki/HOWTO-use-a-zvol-as-a-swap-device Swap Volumes] | * <span lang="en" dir="ltr" class="mw-content-ltr">The [https://github.com/Nefelim4ag/systemd-swap systemd-swap page]</span> | ||
* <span lang="en" dir="ltr" class="mw-content-ltr">The ZFS on Linux guide on [https://github.com/zfsonlinux/pkg-zfs/wiki/HOWTO-use-a-zvol-as-a-swap-device Swap Volumes]</span> | |||
[[Category:Contents Page{{#translation:}}]] | [[Category:Contents Page{{#translation:}}]] |
Revision as of 16:26, 29 December 2022
Übersicht
Der Auslagerungsspeicher wird zur Erweiterung des für die Ausführung von Programmen verfügbaren Speichers (RAM) verwendet.
- Ohne Auslagerungsspeicher werden bei Speichermangel die Anwendungen beendet, was zu einem Absturz des gesamten Systems führen kann.
- Wenn dem System Auslagerungsspeicher zur Verfügung steht, kann der Kernel weniger häufig genutzte Speicherseiten von inaktiven Anwendungen / Diensten auf die Festplatte schreiben ("auslagern"), wodurch mehr echter Arbeitsspeicher für aktive Anwendungen zur Verfügung steht.
Es gibt drei verschiedene Möglichkeiten, wie der Swap in Manjaro verwaltet werden kann:
- Eine Auslagerungs-Partition
- Eine Auslagerungs-Datei
- Komprimierte Auslagerung (zswap)
Dieser Artikel versucht, so umfassend wie möglich zu sein, aber noch mehr Informationen finden Sie in den unten verlinkten Artikel
Brauche ich eine Auslagerung?
Das ist eine Frage, die nicht beantwortet werden kann, ohne einen Blick auf Ihre Konfiguration zu werfen. Selbst bei reichlich verfügbarem Speicher wird dieser oft als Auffangnetz oder sogar manchmal aufgrund spezifischer Anwendungsanforderungen verwendet, also werfen Sie einen Blick auf die folgende nicht abschließende Liste:
- Wenn Sie den Ruhezustand verwenden: ja, Sie müssen auslagern!
- Wenn Sie Dienste haben, die nicht immer aktiv sind, aber trotzdem ständig laufen: ja, Sie müssen auslagern!
- Wenn Sie eine Anwendung haben, die anstelle von RAM direkt virtuellen Speicher für temporäre Speicherung zuweist: ja, Sie müssen auslagern!
- Wenn Sie eine Anwendung haben, die ein Speicherleck hat: ja, Sie müssen auslagern!
- Wenn Sie einen Server mit 1 TB RAM haben, den Sie als Desktop verwenden, ohne dass Anwendungen virtuellen Speicher zuweisen oder Speicherlecks haben: Nein, Sie müssen nicht auslagern!
Wie viel Auslagerungspeicher brauche ich?
Die benötigte Swap-Größe ist sehr variabel und hängt von Ihren spezifischen Anwendungen und der Arbeitslast ab. Es gibt keine allgemeingültige Formel für die Größe des Auslagerungsspeichers, ohne die Nutzung über einen bestimmten Zeitraum hinweg zu überwachen. Ein vernünftiger Ansatzpunkt wäre:
- Bei weniger als 4 GB physischem Speicher (RAM) wird dringend empfohlen, dass der Swap-Speicher mindestens so groß sein sollte wie der RAM-Speicher. Außerdem wird empfohlen, dass der Swap-Speicher maximal doppelt so groß ist wie der RAM-Speicher, abhängig von der Größe des für das System verfügbaren Festplattenplatzes, wegen des abnehmenden Nutzens.
- Bei moderneren Systemen (>4 GB) sollte Ihr Auslagerungsplatz mindestens der Größe Ihres physischen Speichers (RAM) entsprechen, wenn Sie den Ruhezustand verwenden, andernfalls benötigen Sie mindestens ROUNDUP(SQRT(RAM)), d.h. die Quadratwurzel Ihrer Arbeitsspeichergröße aufgerundet auf das nächste GB. Das Maximum ist auch hier das Doppelte der Arbeitsspeichergröße, ebenfalls wegen des abnehmenden Nutzens.
- Der einzige Nachteil, wenn Sie mehr Auslagerungsspeicher haben, als Sie tatsächlich benötigen, ist, dass der dafür reservierte Speicherplatz nicht für Anwendungs- oder Systemdaten verwendet werden kann.
Der "abnehmende Nutzen" bedeutet, dass Sie, wenn Sie mehr Auslagerungsspeicher als das Doppelte Ihrer RAM-Größe benötigen, besser mehr RAM hinzufügen sollten, da der Zugriff auf die Festplatte (HDD) etwa 10³ langsamer ist als der Zugriff auf den RAM, so dass etwas, das 1 Sekunde dauern würde, plötzlich mehr als 15 Minuten dauert! Und auf einem Halbleiterlaufwerk (Solid State Drive, SSD) dauert derselbe Vorgang, der im RAM 1 Sekunde dauerte, auf dem SSD immer noch etwa 1 Minute!
Unter Berücksichtigung aller oben genannten Punkte ergibt sich die folgende Tabelle:
(die letzten 3 Spalten bezeichnen den Auslagerungsbereich)
Arbeitsspeicher Kein Ruhezustand Mit Ruhezustand Maximum 1GB 1GB 2GB 2GB 2GB 2GB 3GB 4GB 3GB 3GB 5GB 6GB 4GB 4GB 6GB 8GB
Arbeitsspeicher Kein Ruhezustand Mit Ruhezustand Maximum 5GB 2GB 7GB 10GB 6GB 2GB 8GB 12GB 8GB 3GB 11GB 16GB 12GB 3GB 15GB 24GB 16GB 4GB 20GB 32GB 24GB 5GB 29GB 48GB 32GB 6GB 38GB 64GB 64GB 8GB 72GB 128GB 128GB 11GB 139GB 256GB 256GB 16GB 272GB 512GB 512GB 23GB 535GB 1TB 1TB 32GB 1056GB 2TB 2TB 46GB 2094GB 4TB 4TB 64GB 4160GB 8TB 8TB 91GB 8283GB 16TB
Auslagerungsinformationen anzeigen
Der Befehl swapon
zeigt Ihre aktuellen Swap-Informationen an. Zum Beispiel:
swapon NAME TYPE SIZE USED PRIO /dev/sda7 partition 20G 44.3M -2
Das folgende Skript wird:
- zeigt an, ob zswap aktiv ist oder nicht, und wenn aktiv, gibt zswap Parameter an, wenn es mit dem Befehl Template:$ic ausgeführt wird
- eine Liste aller Anwendungen / Dienste anzeigen, die Auslagerung beanspruchen und wie viel sie beanspruchen, in absteigender Reihenfolge
#!/bin/bash #Check whether running as root if [ "$(whoami)" = 'root' ]; then dmesg | grep "zswap:" | grep --silent "load" if [ $? -eq 0 ]; then # zswap is active echo "zswap information:" grep --recursive --color=none . /sys/kernel/debug/zswap/ read -n 1 -s -r -p "Press any key to continue" else echo "[warning] zwap not active. Continuing" fi else echo "[warning] Not running as root: skipping zswap info" fi for szFile in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 "\t" $3}END{ print "" }' "$szFile" done | sort --key 2 --numeric --reverse | more
Eine Auslagerungspartition benutzen
Eine Auslagerungspartition ist die traditionelle Art, den Auslagerungsbereich zu verwalten. In diesem Szenario wird eine dedizierte Partition (oder Partitionen) für die Unterbringung der Auslagerungsdaten erstellt.
Erstellen und Aktivieren einer Auslagerungspartition
Um eine Auslagerungspartition zu erstellen, benötigen Sie genügend nicht zugewiesenen Speicherplatz, um eine zusätzliche Partition zu erstellen. Wenn Sie nicht genügend Platz haben, springen Sie zum nächsten Abschnitt #Verwenden_einer_Auslagerungsdatei. Eine Auslagerungspartition kann in jeder Datenträgerverwaltung / Partitionsverwaltung erstellt werden und sollte als Typ linuxswap
eingerichtet werden.#Using_a_Swapfile
Sobald Sie eine Auslagerungspartition haben, müssen Sie die Auslagerungspartition mit mkswap
initialisieren. Wenn Ihre Auslagerungspartition zum Beispiel /dev/sda3
ist, könnten Sie diesen Befehl verwenden:
sudo mkswap /dev/sda3
Als nächstes müssen wir die Auslagerungspartition mit dem Befehl swapon
aktivieren. In Anlehnung an unser obiges Beispiel könnte dies erfolgen mit:
sudo swapon /dev/sda3
Um sicherzustellen, dass die Auslagerung beim Booten aktiviert ist, können wir einen Eintrag in /etc/fstab
hinzufügen. Am besten verwenden Sie dazu die UUID anstelle des Gerätenamens. Sie können die Zeile zu fstab manuell oder mit dem Befehl hinzufügen:
sudo bash -c "echo UUID=$(lsblk -no UUID /dev/sda3) none swap defaults 0 0 >> /etc/fstab"
If you would like to have more than one swap partition, simply repeat the steps above for any additional partitions.
Using a Swapfile
Using a swap partition has one major disadvantage: Changing the size of swap or adding swap requires repartitioning the disk. In current Linux kernels, it is possible to use a swap file instead of a dedicated partition and as from kernel 2.6 onwards there is no performance difference any more between the two.
Creating and Enabling a Static Swapfile
First create and intialize the file to hold the swap. For example, to create a 4GB swapfile, you could use the command:
sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress
Set the appropriate permissions on the file. It should be readable and writable only by root
. This can be done with the command:
sudo chmod 600 /swapfile
Next we need to format and enable the swapfile:
sudo mkswap /swapfile
sudo swapon /swapfile
In order to ensure that the swap is enabled at boot we can add an entry to /etc/fstab
. You can add the line to fstab manually or using the command:
sudo bash -c "echo /swapfile none swap defaults 0 0 >> /etc/fstab"
Swapfiles on BTRFS
As of kernel 5.0 and higher, swapfiles are supported on btfrs. They still require some special handling in addition to the above steps.
Prior to running the dd
step above, you should run these commands:
sudo truncate -s 0 /swapfile sudo chattr +C /swapfile sudo btrfs property set /swapfile compression none
These commands create an empty swapfile, disable COW for that file and ensure that compression is disabled.
Swapfiles on ZFS
zfs doesn't support swapfiles, however you can achieve a similar benefit using a zvol as a swap volume.
Detailed instructions on how to accomplish this can be found in this ZoL guide.
Automated Swap Management with systemd-swap
It is possible to automatically manage the size of your swapfile using systemd-swap
. It can create swap files dynamically based on memory needs. It is also an easy way to enable/manage zswap
Installing and Configuring systemd-swap
First, install and enable systemd-swap
:
pamac install systemd-swap sudo systemctl enable systemd-swap.service
Next enable dynamic swap management by creating a file /etc/systemd/swap.conf.d/myswap.conf
with the following parameters:
zswap_enabled=0 zram_enabled=0 swapfc_enabled=1
If you would prefer a single command to create this file you could use:
sudo bash -c 'echo -e "zswap_enabled=0\nzram_enabled=0\nswapfc_enabled=1" > /etc/systemd/swap.conf.d/myswap.conf'
Remove Other Swap
In order for systemd-swap to be effective you need to remove any traditional swap devices you have.
To display your current swap devices you can use the command swapon
. For example, my test machine displays the following:
swapon NAME TYPE SIZE USED PRIO /swapfile file 4G 0B -2 /dev/sda3 partition 8.4G 0B -3
In this example there are two swap devices. A swapfile and a swap partition. To turn them off we can use the command swapoff
.
sudo swapoff /swapfile sudo swapoff /dev/sda3
Next we need to remove them. The swap partition will need to be deleted using your favorite partitioning tool. You can remove the swapfile by simply deleting it.
sudo rm /swapfile
We need to also remove any lines related to swap from /etc/fstab
. Edit the file with an appropriate text editor and remove or comment out the lines for the swap space. In the example above, we would remove these lines:
UUID=c2430cf1-8ea9-4422-a5c5-5a38779194c3 swap swap defaults,noatime 0 2 /swapfile none swap defaults 0 0
With hibernation configured it will also be necessary to edit /etc/default/grub
. Find and remove option resume=UUID=...
or replace the UUID with a new proper value. In the example above, we would remove this text:
resume=UUID=c2430cf1-8ea9-4422-a5c5-5a38779194c3
Then run:
sudo update-grub
If removing hibernation entirely then also delete resume
from the HOOKS
line in /etc/mkinitcpio.conf
and run:
sudo mkinitcpio -P
Next reboot. After the reboot, when running swapon
you should see something similiar to the following:
NAME TYPE SIZE USED PRIO /var/lib/systemd-swap/swapfc/1 file 512M 0B -2
systemd-swap will add and remove swapfiles as your memory usage dictates.
Using zswap with systemd-swap
zswap is compressed swap kept in RAM. zswap keeps the most frequently used pages in RAM and writes less frequently used pages to the swap space on disk. In many workloads, this will result in increased swap performance and less wear and tear on SSDs.
To enable zswap with systemd-swap, simply alter the instructions above and set Linux Kernel parameter zswap_enabled=1
Tuning & Performance Considerations
Although swap seems like a great way to expand memory, excessive swap use will cause severe performance degradation, as mentioned before.
There are couple of parameters that can be used to tune swap utilization. These are swappiness and vfs_cache_pressure. To see your current settings for these you can use the following commands:
cat /proc/sys/vm/swappiness cat /proc/sys/vm/vfs_cache_pressure
swappiness
controls how likely a page is to be transferred to swap. This value represents the percentage of the free memory before activating swap. The lower the value, the less swapping is used and the more memory pages are kept in physical memory where:
- 0 disables swap
- 60 is the default value which is ideal for a server that is memory-starved and running a lot of services
- 100 is very aggressive swapping.
For most desktop computers the recommended value is 10: Theoretically, this means to only start swapping when RAM usage reaches around 90 percent.
vfs_cache_pressure
used to be a percentage value that controls the tendency of the kernel to reclaim the memory which is used for caching of directory and inode objects with a default value of 100 on Kernels <5.4. On Kernels >=5.4 can be increased beyond 100. Increasing this value will increase the rate in which these objects are removed from the RAM cache. Decreasing it will allow these objects to be cached in memory longer, consuming additional RAM over time. Depending on your specific workload, increasing or decreasing this value too far can have significant negative impacts on system performance. Experimentation is needed to find the appropriate balance and the default value is reasonable. In general, it is more common to optimize swappiness before experimenting with vfs_cache_pressure.
To set these values you can use the command sysctl
. For example, to set the swappiness value to 10 you could use:
sudo sysctl vm.swappiness=10
There is no preset defined answer on the optimal values for these parameters. Experimentation is needed to find the optimum configuration for your specific hardware and workload.
See Also
- The Arch Wiki page on swap
- The Arch Wiki page on zswap
- The Linux kernel documentation on zswap
- The Linux kernel documentation on swappiness and vfs_cache_pressure
- The openSUSE guide to tuning memory
- The systemd-swap page
- The ZFS on Linux guide on Swap Volumes