Swap red hat linux

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.

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” provides 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 may 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

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.

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 with the Rescue Mode in the Red Hat Enterprise Linux 6 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.

Читайте также:  Открыть 8000 порт linux

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 6 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. For instructions on how to do so, refer to Section 14.2.2, “Adding Unallocated Volumes to a Volume Group”.

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

# swapoff -v /dev/VolGroup00/LogVol01
# lvresize /dev/VolGroup00/LogVol01 -L +2G
# mkswap /dev/VolGroup00/LogVol01
# swapon -v /dev/VolGroup00/LogVol01

To test if the logical volume was successfully extended, use cat /proc/swaps or free to inspect the swap space.

15.1.2. Creating an LVM2 Logical Volume for Swap

# lvcreate VolGroup00 -n LogVol02 -L 2G
# mkswap /dev/VolGroup00/LogVol02
# /dev/VolGroup00/LogVol02 swap swap defaults 0 0
# swapon -v /dev/VolGroup00/LogVol02

To test if the logical volume was successfully created, use cat /proc/swaps or free to inspect the swap space.

15.1.3. Creating a Swap File

Procedure 15.2. Add a swap file

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.

# dd if=/dev/zero of=/swapfile bs=1024 count=65536

To test if the new swap file was successfully created, use cat /proc/swaps or free to inspect the swap space.

Источник

Managing swap in the modern Linux system

computer RAM

Swap space is an integral part of the modern Linux system, especially in the Enterprise environments of today. Many of you may be familiar with its concepts and how to manage it to work in your favor. I would bet that there are a lot of people who, like myself until recently, haven’t interfaced with swap or the manipulation of it in a live environment and may not entirely understand its purpose. I want to go through the basic concepts and explain how to create and manage a swap space.

What is swap?

Great Linux resources

Have you ever seen the movie Inception? The premise is centered around «a dream within a dream.» Well, swap is similar in concept because it is virtual memory built on physical memory. Confusion may set in without a common definition of «memory.» In computing, there are two main types of memory — RAM and hard disk.

Random-access memory, also known as RAM, is the memory used to store and access programs being actively used by the computer. This memory is very different from the hard drive storage that you have on your computer. Whether it’s disk-based or solid-state, your Hard drive memory is used to store data not actively being used on the computer. It also allows for the data to be retained through power cycles. With RAM, it is considered volatile memory, meaning that the RAM is cleared when the computer is cycled off.

Читайте также:  Linux commands on windows command prompt

Now that I’ve defined those types of memory, you need to understand why swap is necessary. RAM is a finite resource, and when you’re multitasking or accessing demanding files or software, you can exceed the amount of RAM onboard the system. When this happens, it can cause programs to crash or freeze until the needed RAM becomes available. This is where swap comes in.

Swap allows you to create virtual RAM using your existing physical hard drive space when your physical RAM is being pushed to capacity. Imagine that you are working on a server and that when you initialize specific processes, you experience a crash. Many times, the Out of Memory error (OOM Killer as it’s known) will occur when you try to start multiple core system processes simultaneously. This is especially common in lightly provisioned systems (virtual editions servers and the like). An easy fix for this is to allocate several Gb of swap memory. It’s time to take a look at how to create swap memory.

[NOTE: If you’re still not convinced about swap, be sure to check out this great piece from Redhat.com]

There are two options when it comes to creating a swap space. You can create a swap partition or a swap file. Most Linux installations come preallocated with a swap partition. This is a dedicated block of memory on the hard disk utilized when the physical RAM is full. On the flip side, you can also create a swap file. This is a file that is designed to be a specific size and is best used only in particular circumstances. It is recommended to use a swap partition unless the situation dictates otherwise.

I am going to demonstrate the procedure for extending swap on an LVM logical volume. You can follow along here, or see the customer portal for the full documentation, including a method for a non-LVM swap.

[NOTE: To see the full documentation, please see the RHEL customer portal. ]

Before you create or increase swap, it is helpful to determine if your machine already has a swap available. You can quickly check this by running the free -m command. You can see here that a fresh installation of RHEL8 in Virtual Box comes preallocated with a 3Gb swap, which is not currently being utilized.

output of free-m

Now, say that system utilization dictates that you need to increase the swap space available. You begin by disabling the swap for the associated volume.

Disable: # swapoff -v /dev/rhel/swap Resize: # lvresize /dev/rhel/swap -L +2G (to increase swap by 2G) Format: # mkswap /dev/rhel/swap Enable: # swapon -v /dev/rhel/swap

If you need to reduce the swap space for any reason, you simply change step 2 to look something like this:

Reduce: # lvreduce /dev/rhel/swap -L -1G (to reduce the swap by 1Gb)

There may be other things that you need to manipulate when it comes to swap. The creation of a new volume or complete removal of swap is sometimes necessary. There is also the swap file that I mentioned earlier. For more information on how to accomplish these tasks, be sure to check the link to the customer portal from earlier in the article.

[NOTE: For a great deep-dive on this topic, be sure to check out David Both’s article over at Opensource.com]

Источник

How to add swap space on Red Hat 7 Operating System

The swap space is a unique space on the disk that is used by the system when Physical RAM is full. When a Linux machine runout the RAM. It use swap space to move inactive pages from RAM.

Читайте также:  Запуск zabbix агента linux

Swap space can be created into Linux system in two ways, one we can create a dedicated partition for swap or another one is created a swap file and use it as swap space.

Usually, when we are creating VPS with any provider like Digital Ocean, Linode, etc., we don’t have the swap partition into the server, in this case, we need to create swap file to use as swap space in server.

In this tutorial, we have explained all necessary steps to create and add swap space on Red Hat 7 machine.

If the output of the above command is empty, it means your system doesn’t have swap space.

It is also possible to configure more than one swap space.

Create and add swap space on Red Hat 7

In this tutorial, we will add 1GB of swap space, if you want to create bigger swap space you can create by replacing 1G with any other size what you want.

To create a swap file, follow the below steps:

If in your system fallocate is not installed or you get a message like “fallocate failed: Operation not supported,” you can use below command to create swap file:

$ sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576

Step 2 – Change Permission

Step 3 – Create swap space

To create swap space, use the “mkswap” command, as shown below:

Step 4 – Activate swap space

To activate swap space permanent by configuring file “/etc/fstab”:

Add following line into the file “/etc/fstab”:

/swapfile swap swap defaults 0 0

Step 5 – Verify Swap space

Configure Swappiness Value

Swappiness is the essential swap properties of Linux kernel, which is defined when the system will use swap space. Swappiness can have the value between 0 to 100, which is represent the percentage of RAM utilization, for example, if you set the value 20 for swappiness, it means the Linux kernel will use swap space when RAM is only 20 % available.

So, we can say the lower value of swappiness is to try to avoid the use of swap space.

The default value of swappiness is 30, which you can check by using the following command:

The default value (30) of swappiness is ok for the desktop system, but when you run the production system, try to set the lower value for production servers.

You can set the swappiness value by using “sysctl” command, for example, if you want to set the value of swappiness is 20 you should execute below command:

$ sudo sysctl vm.swappiness=20

To change the value of swappiness permanently, you should change the parameter “vm.swappiness” into the “/etc/sysctl.conf” file and reboot the system.

Remove Swap File

To deactivate and remove the active swap space, follow the below steps:

Step 2 – Remove from /etc/fstab

Edit the “/etc/fstab” file and remove the line “/swapfile swap swap defaults 0 0” from the file and save

Conclusion

Now you have learned how to create a swap space into the Red Hat Enterprise Linux (RHEL) 7 machine if it not configured or you want to create another swap space.

Are you open to learn Linux?

Get weekly Linux news, tutoials, tips & tricks, and other useful information related to Linux and Open source in your INBOX.

Источник

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