Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Linux uses swap space

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "Linux how to use swap space", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Linux how to use swap space" this article.

I. how does UNIX use 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 the active application and the data it uses-physical random access memory (RAM) and the space allocated on the hard drive.

On computers with VM, 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 used actively are exchanged to disk (hence the term swap space).

The advantage of the VM system is that physical RAM is used more efficiently-- because it contains only the executing application and the data being used. Because RAM is faster than hard disk space, you get the performance you need from actively loaded applications, while also benefiting from the ability to run more applications than you normally support.

As a result, you get much more addressable memory space (4GB on 32-bit systems and even larger on 64-bit systems), making application development easier while still providing high performance.

Second, set the value of exchange space

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

Different UNIX variants use their swap space in different ways. Some variants use swap space only when they have run out of physical memory. Other variants use swap space for all inactive applications and unused pages. Some variants retain copies of the data in all applications-- including applications in both memory and swap space. By default, some variants avoid using swap space and, as far as possible, provide a mechanism that allows 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 cause more pages to be swapped, and lower values cause more applications to remain in memory (even if they are idle).

In addition to these basics, there are many ideas and rules about how 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 the physical RAM. This is a requirement of older versions of UNIX, especially SunOS, because of the way VM is used. In theory, having more VM than the physical RAM enables the operating system to swap out all current processes to disk when the system becomes idle, and improves system performance because new processes can be loaded directly into the RAM without having to swap applications in advance.

two。 For development systems, many administrators agree that there should be as many VM as RAM. The reason for this approach is that swap space is also used as the location to dump active memory during a system failure; the stored memory dump can then be reloaded during startup and saved as a kernel core file. If the configured swap space is less than the physical RAM, this type of dump cannot be achieved.

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

4. For a desktop environment, having a very large VM allocation may be to your advantage because it enables you to run a large number of applications (many of which may be free and can be easily swapped, thus making more RAM available to active applications).

Choosing the right amount of swap space depends largely on the platform you are configuring-its intended use and how you want to deal with the lack of available VM.

Here are some conservative values:

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

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

III. Configure swap space

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

The way 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 by 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 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

Note that swap space added in this way is not retained after reboot. To permanently add swap space to the system, you must configure one of the startup configuration files to automatically enable swap space allocation.

Add swap space at startup

Under most UNIX variants, swap space is automatically added at startup as part of kernel initialization. Most variants use lines in the file system installation file (/ etc/fstab, / etc/vfstab, or / etc/filesystems) to specify the partition to use as swap space. You can see such an example in the / etc/vfstab file of Solaris

Specify the partition to use as swap space in 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-

An example of the / etc/fstab file in Linux is shown below

The code is as follows:

#

/ dev/sda1 / boot ext2 noauto,noatime 1 1

/ dev/sda3 / ext3 noatime 0 0

/ dev/sda2 none swap sw 0 0

/ dev/hda1 / var/lib/mysql ext3 noatime 1 2

/ dev/hdb1 / export/data ext3 noatime 1 2

/ dev/hde1 / export/home ext3 noatime 1 2

/ dev/hde2 / var/tmp ext3 noatime 1 2

/ dev/hde3 / usr/portage/distfiles ext3 noatime 1 2

/ dev/hde4 / tmp ext3 noatime 1 2

/ dev/cdroms/cdrom0 / mnt/cdrom iso9660 noauto,ro 0 0

None / proc proc defaults 0 0

Finally, you can use the / etc/swapspaces file in AIX to configure swap space partitions

The code is as follows:

* This file is modified by the chps, mkps and rmps commands and referenced

* by the lsps and swapon commands.

Hd6:

Dev = / dev/hd6

Paging00:

Dev = / dev/paging00

Paging01:

Dev = / dev/paging01

Instead of editing the file directly, you should use the corresponding command or Smitty tool to modify the contents of the file.

To remove the additional swap space, you can use the corresponding swapoff or swap-d command to disable the swap partition. Note, however, that you can only disable the currently unused swap partition, and you cannot disable the primary swap partition.

Add temporary switching capacity

Sometimes you may run out of configured swap space partitions, requiring more VM in a short period of time until you can organize a more permanent solution. If a partition is available, you can prepare it as described above in the configure swap partition section.

If there are no free partitions available, you can create one or more files and temporarily add them to the available swap pool. Most systems even allow you to use swap files on NFS-mounted file systems, which you can use when you don't have local disk space available.

To use a file for swap space, first create an appropriate file. The mkfile > command is most appropriate here because it provides an easy way to create large files. You can specify the file name and file size on the command line:

The code is as follows:

Mkfile 512m / export/data/swapfile

You can then add the file as if you were adding a partition:

The code is as follows:

$swap-a / export/data/swapfile

Or

The code is as follows:

$swapon / export/data/swapfile

Swap files used in this way are by no means a long-term solution to the swap space problem. You should configure dedicated partitions for swap space as soon as possible.

VI. Monitoring the use of swap space

The monitoring of swap space and its usage includes two aspects: current usage and activity usage. You can find a number of different tools among the various UNIX variants that report available free memory and VM usage as well as current usage.

Under Solaris, the swap tool provides most of the switching interface functions, including reporting usage and available devices. For example, to get a list of configured switching devices, such as monitoring swap space usage under Solaris

The code is as follows:

$swap-l

Swapfile dev swaplo blocks free

/ dev/dsk/c0d0s1 102,1 8 1220928 1220928

Under AIX, you can use the lsps tool to get the same information

The code is as follows:

$lsps-a

Page Space Physical Volume Volume Group Size Used Active Auto

Hd6 hdisk0 rootvg 2048MB 3 yes yes

Under Linux, you can use the swapon tool

The code is as follows:

$swapon-s

Filename Type Size Used Priority

/ dev/sda2 partition 1004052 319744-1

In each of the above examples, you can see a list of available swap space locations and their purpose, and get an overview of the current usage of swap space.

These snapshot views are useful, but they only show current usage. There is very little information about the regular or full use of swap space. For performance reasons, or when diagnosing problems that you suspect might be related to swap space, you usually need a more interactive view of memory and paging usage.

7. Active VM monitoring

The vmstat command provides a long-term view of virtual memory usage, including information about the amount of memory being actively exchanged between swap space and RAM. On the command line, you need to specify the interval (in seconds) to use when reporting statistics. The resulting output shows a great deal of information and can be used to make decisions about VM configuration and allocation.

The following shows an example of output for a Solaris computer (with an interval of five seconds)

The code is as follows:

Kthr memory page disk faults cpu

R b w swap free re mf pi po fr de sr cd cd cd cd in sy cs us sy id

00 0 945932 385632 9 36 13 0 10 100 3 00 0 413 172 87 0 36 63

0 0 0 944616 384068 0 8 3 0 0 0 654 21 51 0 1 99

0 0 0 944612 384064 0 0 0 596 21 52 0 1 99

0 0 0 944612 384064 0 0 0 694 12 48 0 1 99

0 0 0 944612 384064 0 0 0 50 0 0 658 11 50 0 1 99

0 0 0 944612 384064 0 0 0 576 18 52 0 1 99

00 0 944500 383988 115 850 1 00 00 00 612 1656 120 15 94

0 0 0 942060 382444 6 62 0 0 0 669 256 146 0 2 98

0 0 0 942048 382432 9 105 0 0 0 699 414 188 0 2 98

00 0 946188 388188 481 2400 10 10 10 00 5 00 0 595 12613 6082 618 76

0 0 0 944488 386308 769 4753 1 88 0 0 70 0 0 625 15705 6285 8 29 63

0 0 0 943128 385104 839 5194 176 9 9 0 0 14 0 0 0 579 16077 6304 8 32 60

0 0 0 937692 377628 32 209 20 0 0 0 20 0 0 654 403 131 0 2 97

0 0 0 937228 377208 0 0 0 647 38 63 0 1 99

00 0 937220 377200 121 980 00 00 00 0 623 1246 218 1 6 93

0 0 0 937348 377320 126 1044 2 1 10 0 3 0 0 0 667 1266 228 1 7 93

0 0 0 937428 377408 57 686 0 0 0 702 890 180 1 5 94

0 0 0 937436 377404 72 861 0 0 0 54 0 0 0 689 1113 295 1 6 93

There are many columns-some are more important than others. Key columns include:

* swap shows the total swap space available

* free shows the amount of free swap space

* number of kilobytes converted by pi

* kilobytes exchanged by po

* kilobytes of free fr

A very low amount of free space and a high number of swapped in or out pages may indicate insufficient VM, and a very high number of swapped in and out pages may indicate insufficient physical RAM.

Use swap space for temporary storage

Under some UNIX variants, especially Solaris and Linux, you can use the tmpfs file system, which uses VM as the storage space. When you have enough RAM, this provides an ideal way to support ultra-fast disk access; unused files will be swapped to disk. The result is an efficient solution for quick access to the most commonly used files, and it can significantly improve the speed of applications, such as software development, where a large number of temporary files are created during the compilation process.

The above is all the content of the article "how Linux uses swap space". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report