Este contenido no está disponible en el idioma seleccionado.

Chapter 15. Swap Space


Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory. Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files. Note that Btrfs does not support swap space.
In years past, the recommended amount of swap space increased linearly with the amount of RAM in the system. However, modern systems often include hundreds of gigabytes of RAM. As a consequence, recommended swap space is considered a function of system memory workload, not system memory.
Table 15.1, “Recommended System Swap Space” illustrates the recommended size of a swap partition depending on the amount of RAM in your system and whether you want sufficient memory for your system to hibernate. The recommended swap partition size is established automatically during installation. To allow for hibernation, however, you need to edit the swap space in the custom partitioning stage.
Recommendations in Table 15.1, “Recommended System Swap Space” are especially important on systems with low memory (1 GB and less). Failure to allocate sufficient swap space on these systems can cause issues such as instability or even render the installed system unbootable.
Table 15.1. Recommended System Swap Space
Amount of RAM in the system Recommended swap space Recommended swap space if allowing for hibernation
⩽ 2 GB 2 times the amount of RAM 3 times the amount of RAM
> 2 GB – 8 GB Equal to the amount of RAM 2 times the amount of RAM
> 8 GB – 64 GB At least 4 GB 1.5 times the amount of RAM
> 64 GB At least 4 GB Hibernation not recommended

Note

There are two reasons why hibernation is not recommended with systems with more than 64 GB of RAM. Firstly, hibernation requires extra space for an inflated (and perhaps infrequently utilized) swap area. Secondly, the process of moving resident data from RAM to disk and back on can take a lot of time to complete.
At the border between each range listed in Table 15.1, “Recommended System Swap Space”, for example a system with 2 GB, 8 GB, or 64 GB of system RAM, discretion can be exercised with regard to chosen swap space and hibernation support. If your system resources allow for it, increasing the swap space may lead to better performance.
Note that distributing swap space over multiple storage devices also improves swap space performance, particularly on systems with fast drives, controllers, and interfaces.

Important

File systems and LVM2 volumes assigned as swap space should not be in use when being modified. Any attempts to modify swap fail if a system process or the kernel is using swap space. Use the free and cat /proc/swaps commands to verify how much and where swap is in use.
You should modify swap space while the system is booted in rescue mode, see Booting Your Computer in Rescue Mode in the Red Hat Enterprise Linux 7 Installation Guide. When prompted to mount the file system, select Skip.

15.1. Adding Swap Space

Sometimes it is necessary to add more swap space after installation. For example, you may upgrade the amount of RAM in your system from 1 GB to 2 GB, but there is only 2 GB of swap space. It might be advantageous to increase the amount of swap space to 4 GB if you perform memory-intense operations or run applications that require a large amount of memory.
You have three options: create a new swap partition, create a new swap file, or extend swap on an existing LVM2 logical volume. It is recommended that you extend an existing logical volume.

15.1.1. Extending Swap on an LVM2 Logical Volume

By default, Red Hat Enterprise Linux 7 uses all available space during installation. If this is the case with your system, then you must first add a new physical volume to the volume group used by the swap space.
After adding additional storage to the swap space's volume group, it is now possible to extend it. To do so, perform the following procedure (assuming /dev/VolGroup00/LogVol01 is the volume you want to extend by 2 GB):

Procedure 15.1. Extending Swap on an LVM2 Logical Volume

  1. Disable swapping for the associated logical volume:
    # swapoff -v /dev/VolGroup00/LogVol01
  2. Resize the LVM2 logical volume by 2 GB:
    # lvresize /dev/VolGroup00/LogVol01 -L +2G
  3. Format the new swap space:
    # mkswap /dev/VolGroup00/LogVol01
  4. Enable the extended logical volume:
    # swapon -v /dev/VolGroup00/LogVol01
  5. To test if the swap logical volume was successfully extended and activated, inspect active swap space:
    $ cat /proc/swaps
    $ free -h

15.1.2. Creating an LVM2 Logical Volume for Swap

To add a swap volume group 2 GB in size, assuming /dev/VolGroup00/LogVol02 is the swap volume you want to add:
  1. Create the LVM2 logical volume of size 2 GB:
    # lvcreate VolGroup00 -n LogVol02 -L 2G
  2. Format the new swap space:
    # mkswap /dev/VolGroup00/LogVol02
  3. Add the following entry to the /etc/fstab file:
    /dev/VolGroup00/LogVol02   swap     swap    defaults     0 0
  4. Regenerate mount units so that your system registers the new configuration:
    # systemctl daemon-reload
  5. Activate swap on the logical volume:
    # swapon -v /dev/VolGroup00/LogVol02
  6. To test if the swap logical volume was successfully created and activated, inspect active swap space:
    $ cat /proc/swaps
    $ free -h

15.1.3. Creating a Swap File

To add a swap file:

Procedure 15.2. Add a Swap File

  1. Determine the size of the new swap file in megabytes and multiply by 1024 to determine the number of blocks. For example, the block size of a 64 MB swap file is 65536.
  2. Create an empty file:
    # dd if=/dev/zero of=/swapfile bs=1024 count=65536
    Replace count with the value equal to the desired block size.
  3. Set up the swap file with the command:
    # mkswap /swapfile
  4. Change the security of the swap file so it is not world readable.
    # chmod 0600 /swapfile
  5. To enable the swap file at boot time, edit /etc/fstab as root to include the following entry:
    /swapfile          swap            swap    defaults        0 0
    The next time the system boots, it activates the new swap file.
  6. Regenerate mount units so that your system registers the new /etc/fstab configuration:
    # systemctl daemon-reload
  7. To activate the swap file immediately:
    # swapon /swapfile
  8. To test if the new swap file was successfully created and activated, inspect active swap space:
    $ cat /proc/swaps
    $ free -h
Red Hat logoGithubRedditYoutubeTwitter

Aprender

Pruebe, compre y venda

Comunidades

Acerca de la documentación de Red Hat

Ayudamos a los usuarios de Red Hat a innovar y alcanzar sus objetivos con nuestros productos y servicios con contenido en el que pueden confiar.

Hacer que el código abierto sea más inclusivo

Red Hat se compromete a reemplazar el lenguaje problemático en nuestro código, documentación y propiedades web. Para más detalles, consulte el Blog de Red Hat.

Acerca de Red Hat

Ofrecemos soluciones reforzadas que facilitan a las empresas trabajar en plataformas y entornos, desde el centro de datos central hasta el perímetro de la red.

© 2024 Red Hat, Inc.