How to use and manage swap space in Linux

  
                

Configuring swap space for computers can make full use of the system. When physical memory is full, how do you use Linux swap space under Linux? How to manage Linux swap space? The following small series will introduce you to the use of swap space under Linux, let's learn together.

a, UNIX how to use the swap space

All UNIX operating systems support some kind of virtual memory (VM) system. With the VM system, you have two main areas to store information about active applications and the data they use —— physical random access memory (RAM) and allocated space on the hard drive.

On computers with VMs, the amount of applications and data that can be loaded into memory can be larger than the physical RAM because applications and information that are not actively used are swapped to disk (the term swap space is used by This is named).

The advantage of the VM system is that the physical RAM is more efficiently utilized —— this is because it only contains the application being executed and the data being used. Because RAM is faster than hard disk space, you get the performance you need from an actively loaded application, while also benefiting from the ability to run more applications than is usually supported.

The result is that you get a much larger addressable memory space (4GB on 32 for systems and larger on 64-bit systems), making application development easier while still Provide high performance.

Second, set the swap space value

The amount of swap space you configure on your computer depends mainly on the application and operating system. If the swap space is too small, you might not be able to run all the applications you want to run, and if the swap space is too large, you might be wasting disk space that you have never used. You may also indirectly overload the system, because too much swap space can cause poor performance compared to too small swap space, because it is swapped in compared to the time it takes the system to perform the actual processing. It takes more time to swap out the page.

Different UNIX variants use their swap space in different ways. Some variants use swap space only when physical memory has been used. Other variants use swap space for all inactive and unused pages. Some variants retain a copy of the data in all applications —— both the application in memory and swap space. By default, some variants avoid swap space and provide as much mechanism as possible to allow you to adjust how swap space is used. Many variants use a combination of these different arrangements to make the most of their swap space.

For example, in Linux®, when you use the 2.6 kernel, you can adjust the swappiness of the system by setting the vm.swappiness value in /etc/sysctl.conf. Higher values ​​result in more pages being swapped, and lower values ​​result in keeping more applications in memory even if they are idle.

In addition to these foundations, there are many ideas and rules about how the VM should be configured and how much disk space should be allocated. Some of these methods are summarized below:

1. Some administrators believe that your swap space should never be less than physical RAM. This is a requirement for older UNIX versions (especially SunOS) due to the way VMs are used. In theory, having more VMs than physical RAM allows the operating system to swap out all current processes to disk when the system becomes idle, and can improve system performance because new processes can be loaded directly into RAM. Without having to exchange applications in advance.

2. For development systems, many administrators agree that there should be as many VMs as RAM. The reason for this approach is that swap space is also used as a place to dump active memory during a system failure; then the stored memory dump can be reloaded during startup and saved as a kernel core file. If the configured swap space is smaller than the physical RAM, such a dump cannot be implemented.

3. In a high-performance deployment environment, you can configure the smallest possible VM because you only want to keep the application active in memory and do not require a lot of swapping. Crash recovery is not required, and the ability to determine the potential lack of physical RAM (due to the exhaustion of swap space) can provide a warning that you know that the application environment is not optimized or that you need to upgrade RAM.

4. For desktop environments, having a very large VM allocation might be good for you because it allows you to run a large number of applications (many of which may be idle and can be easily swapped so that More RAM is available for active applications).

Choosing the right amount of swap space is highly dependent on the platform you are configuring —— its intended use and how you want to handle the lack of available VMs.

Here are some conservative values:

1. On the desktop system, allocate at least as much swap space as physical RAM.

2. On the server, allocate at least 50% of the available RAM as swap space. Once you have a general number in your mind, you need to configure swap space.

Configuring swap space

All UNIX systems have multiple types of swap space. The primary swap space is the swap space that is typically configured at boot time and is typically set up as part of the operating system installation. All UNIX systems usually have a primary swap space allocation.

The way in which swap space is created and configured varies from system to system. Under Linux, the disk partition used for swapping is identified by the partition ID, which must be initialized with the mkswap command:

The code is as follows:

$ mkswap /dev/sda2

In most traditional UNIX environments, you can use any available partition without any preparation. On HP-UX, AIX®, and Linux, you can use the swapon command to add partitions to the available swap space:

The code is as follows:

$ swapon /dev/sda2

On Solaris, you can use the swap command to add swap space to the system:

The code is as follows:

$ swap -a /dev/dsk/c0t0d0s2

Please note that the swap space added in this way will not be retained after a reboot. To permanently add swap space to the system, you must configure one of the launch profiles to automatically enable swap space allocation.

Adding swap space at startup

In most UNIX variants, swap space is automatically added at boot time as part of kernel initialization. Most variants use the lines in the file system installation files (/etc/fstab, /etc/vfstab, or /etc/filesystems) to specify which partitions to use as swap space. You can see an example of this from the Solaris /etc/vfstab file

Specifying the partition to be used as swap space on Solaris:

The code is as follows:

# Device device mount FS fsck mount mount

#to mount to fsck point type pass at boot options

#

fd - /dev/fd fd - no -

/proc - /proc proc - no -

/dev/dsk/c0d0s1 - - swap - no -

/dev/dsk/c0d0s0 /dev/rdsk/c0d0s0 /ufs 1 no -

/dev/dsk/c0d0s7 /dev/rdsk/c0d0s7 /export/home ufs 2 yes -

/devices - /devices devfs - no -

Ctfs - /system/contract ctfs - no -

objfs - /system/object objfs - no -

swap - /tmp tmpfs - yes -
previous12next 2 pages in total

Copyright © Windows knowledge All Rights Reserved