Manjaro Difference between revisions of "Swap/fr"

Difference between revisions of "Swap/fr"

From Manjaro
(Created page with "Les "rendements décroissants" signifie qu'au cas ou vous avez besoin de plus de mémoire que le double de votre RAM, il vaut mieux rajouter du RAM puisque l'accès à un disq...")
(Created page with "En distillant tout cela on arrive au tableau ci-dessous: (Les dernières 3 colonnes sont le swap dont vous avez besoin)")
Line 34: Line 34:
Les "rendements décroissants" signifie qu'au cas ou vous avez besoin de plus de mémoire que le double de votre RAM, il vaut mieux rajouter du RAM puisque l'accès à un disque dur (HDD) est environs 10³ fois plus lent que la vitesse d'accès au RAM, donc une tâche qui prend 1 seconde en RAM, prendra 15 minutes en swap!  Même sur un disque SSD la même opération d' une seconde prendra une minute entière avec un SSD.
Les "rendements décroissants" signifie qu'au cas ou vous avez besoin de plus de mémoire que le double de votre RAM, il vaut mieux rajouter du RAM puisque l'accès à un disque dur (HDD) est environs 10³ fois plus lent que la vitesse d'accès au RAM, donc une tâche qui prend 1 seconde en RAM, prendra 15 minutes en swap!  Même sur un disque SSD la même opération d' une seconde prendra une minute entière avec un SSD.


Taking into accound all of the above, this brings us to the following table:
En distillant tout cela on arrive au tableau ci-dessous:
(last 3 columns denote swap space)
(Les dernières 3 colonnes sont le swap dont vous avez besoin)


         RAM  No hibernation    With Hibernation  Maximum
         RAM  No hibernation    With Hibernation  Maximum

Revision as of 14:16, 1 April 2021

Other languages:
Deutsch • ‎English • ‎français • ‎русский


Aperçu

L'espace d'échange (« swap » à partir de maintenant) est utilisé pour étendre la capacité de la mémoire vive (RAM) disponible pour exécuter des logiciels .

  • Sans espace swap, vos applications se termineront jusqu'au point que le système d'exploitation complet se plante ou se gèle .
  • Contrairement, si votre système possède d'un espace swap, le noyau Linux peut prendre les pages de mémoire des applications / services non-actives et les écrire sur disque (« swapping ») pour pouvoir récupérer plus de mémoire vive ( « RAM ») pour les applications actives .


Il y a trois moyens selon Manjaro peur utiliser la mémoire swap:

  • Une partition swap
  • Un fichier swap
  • zswap


Cet article essaye d'être le plus complet que possible, mais si vous avez besoin de plus ample information, voyez articles liés en bas

Avez vous besoin de swap?

On ne peut pas répondre à cette question particulière sans les spécifications exactes de votre système . Même des systèmes avec beaucoup de mémoire vive de disponible, le swap est utilisé comme filet de sécurité et dû à des applications avec des besoins très particuliers . En bas une liste non-exhaustive qui éclairci cette définition vague en plus de détails :

  • Si vous utilisez de l'« hibernation » (mise en veille prolongée): oui, vous avez besoin de swap!
  • Si vous avez des services qui ne sont pas toujours actives, mais qui tournent quand-même : oui, vous avez besoin de swap!
  • Si vous avez une application qui alloue de la mémoire virtuelle directement comme stockuage et non du RAM : oui, vous avez besoin de swap!
  • Si vous avez une application qui contient une fuite de mémoire : oui, vous avez besoin de swap!
  • Si vous possédez d'un serveur contenant 1TB de RAM que vous utilisez sans applications qui allouent de la mémoire virtuelle directement ou ayant des duites de mémoire : non, vous n'avez pas besoin de swap!

Quelle est la taille de swap dont vous avez besoin?

La taille de swap est extrêmement variable et dépend de vos applications spécifiques et la façon dont vous les utilisez . Il n'y a pas de formule universelle sans surveillance de votre système surveillance sur une période de temps prolongée . Un bon point de départ sera :

  • Pour des systèmes contenant moins que 4GB de RAM, il est hautement recommandé que l'espace swap soit, au minimum, le double de la taille de votre RAM. Il es aussi recommandé que l'espace swap soit, au maximum, le double de votre RAM (dépendant de l'espace disque de disponible) en raison de rendements décroissants.
  • Pour des systèmes plus récentes (>4GB), votre swap devra être au minimum égal à votre mémoire vive (RAM) si vous utilisez de l'hibernation. Sinon, vous avez besoin d'un minimum de l'arrondi supérieur de la racine carrée de votre RAM en GB. Le maximum est, de nouveau, le double de votre RAM en raison de rendements décroissants.
  • Le seul et unique désavantage d'avoir trop de swap, sera que l'espace disque alloué ne pourra pas être utilisé ni par des applications, ni pour stocker des données.

Les "rendements décroissants" signifie qu'au cas ou vous avez besoin de plus de mémoire que le double de votre RAM, il vaut mieux rajouter du RAM puisque l'accès à un disque dur (HDD) est environs 10³ fois plus lent que la vitesse d'accès au RAM, donc une tâche qui prend 1 seconde en RAM, prendra 15 minutes en swap! Même sur un disque SSD la même opération d' une seconde prendra une minute entière avec un SSD.

En distillant tout cela on arrive au tableau ci-dessous: (Les dernières 3 colonnes sont le swap dont vous avez besoin)

       RAM   No hibernation    With Hibernation  Maximum
       1GB              1GB                 2GB      2GB
       2GB              2GB                 3GB      4GB
       3GB              3GB                 5GB      6GB
       4GB              4GB                 6GB      8GB
       RAM   No hibernation    With Hibernation  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
Note
The largest server one of the authors of this wiki has ever installed had, indeed, 8TB of RAM

Displaying Swap Information

The command swapon will display your current swap information. For example:

swapon
NAME      TYPE      SIZE   USED PRIO
/dev/sda7 partition  20G  44.3M   -2

The following script will:

  • show whether zswap is active or not and if active, give zswap parameters if run with the `sudo` command
  • display a list of all applications / services that take up swap and how much they take up in descending order
#!/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

Using a Swap Partition

A swap partition is the traditional way of managing swap. In this scenario, a dedicated partition (or partitions) are created for holding swap.

Creating and Enabling a Swap Partition

To create a swap partition, you need enough unallocated disk space to create an additional partition. If you do not have enough space, skip to the next section #Using_a_swap_file. A swap partition can be created in any disk management / partition management tool and should be set as type `linuxswap`.

Once you have a swap partition you will need to initialize the swap partition with mkswap. For example, if your swap partition is /dev/sda3, you could use the command:

sudo mkswap /dev/sda3

Next we need to enable the swap partition with the swapon command. Following our example above this could be done with:

sudo swapon /dev/sda3

In order to ensure that the swap is enabled at boot we can add an entry to /etc/fstab. It is best to use the UUID instead of the device name for this purpose. You can add the line to fstab manually or using the command:

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.


Note
Be sure to replace /dev/sda3 in the above commands with your actual swap partition.


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

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

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


First create and intialize the file to hold the swap. For example, to create a 4GB swapfile, you could use the command:

sudo fallocate -l 4G /swapfile
sudo mkswap /swapfile


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 u=rw,go= /swapfile


Next we need to enable the swapfile with the swapon command. Following our example above this could be done with:

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 ftab 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 fallocate 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

Note
Using systemd-swap with on btrfs/zfs or with hibernation support requires special handling beyond the scope of this article

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


Next reboot. After the reboot you should see something similiar to the following when running swapon

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.


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

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 zswap_enabled to 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 running a lot of services
  • 100 is very aggressive swapping.

For most desktops/laptops the recommended value is 10: Theoretically, this means to only start swapping when RAM usage reaches around 90 percent.

vfs_cache_pressure is a percentage value that controls the tendency of the kernel to *reclaim* the memory which is used for caching of directory and inode objects. The default value is 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

Cookies help us deliver our services. By using our services, you agree to our use of cookies.