In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you a sample analysis of the exchange space of the Linux system. I hope you will get something after reading this article. Let's discuss it together.
Swap space is a common aspect of today's computing, regardless of the operating system. Linux uses swap space to increase the amount of virtual memory available to the host. It can use one or more dedicated swap partitions or swap files on a regular file system or logical volume.
There are two basic types of memory in a typical computer. The first type, random access memory (RAM), is used to store data and programs that are being actively used by computers. Computers cannot use programs and data unless they are stored in RAM. RAM is volatile memory; that is, if the computer is turned off, the data stored in RAM will be lost.
A hard disk is a magnetic medium used for long-term storage of data and programs. Magnetic media are non-volatile; data stored on disk remains unchanged even if the power supply is removed from the computer. CPU (central processing unit) cannot directly access programs and data on the hard disk; it must first be copied to RAM, which is where CPU can access its programming instructions and the data they are going to operate on. During the boot process, the computer copies specific operating system programs (such as kernel, init or systemd) and data from the hard drive to RAM, which is directly accessed by the computer's processor CPU.
Swap space
Swap space is the second type of memory in modern Linux systems. The main function of swap space is to replace RAM memory with disk space when the actual memory is filled and more space is needed.
For example, suppose you have a computer system with 8GB RAM. If the program you start does not populate the RAM, everything is fine and does not need to be swapped. But suppose that the spreadsheet you are working on grows as you add more rows, and, along with everything else that is running, will now populate all the RAM. If there is no swap space available, you will have to stop working on the spreadsheet until you can release some limited RAM by closing some other programs.
The kernel uses memory managers to detect blocks of memory that have not been used recently, that is, pages. The memory manager swaps these relatively rarely used memory pages to a special partition on a hard drive specifically designated for "paging" or swapping. This frees up RAM and makes room for more data to be entered into the spreadsheet. The kernel's memory management code keeps track of memory pages that are swapped to the hard disk and can be paged back to RAM if necessary.
The total amount of memory in a Linux computer is RAM plus swap space, which is called virtual memory.
Type of Linux exchange
Linux provides two types of swap space. By default, most Linux installations create swap partitions, but you can also use specially configured files as swap files. The swap partition is exactly what its name implies-a standard disk partition, by the mkswap command.
Swap files can be used if there is no available disk space to create a new swap partition, or if you can create logical volumes for swap space in a volume group. This is just a regular file that is created and pre-assigned to the specified size. The mkswap command then configures it as swap space. I do not recommend using files as swap space unless absolutely necessary.
Shock
Concussion occurs when the entire virtual memory (including RAM and swap space) is almost full. The system spends a lot of time paging memory blocks between swap space and RAM, and then paging, leaving little time for actual work. The typical symptoms are obvious: the system becomes slow or completely unresponsive, and the activity of the hard drive is almost always on.
If you can manage to issue the following command free, which shows the CPU load and memory usage, you will see that the CPU load is very high, perhaps 30 to 40 times the number of CPU cores in the system. Another symptom is that RAM and swap space are almost completely allocated.
After that, viewing the SAR (system activity report) data can also show these symptoms. I installed SAR on every system I worked on and used it for forensic analysis after repair.
What is the appropriate swap space?
Many years ago, the rule of thumb was that the amount of swap space that should be allocated on a hard disk was twice the amount of RAM installed on a computer (of course, most computers' RAM is calculated in KB or MB). Therefore, if the computer has a 64-KB RAM,128 KB swap partition, it would be the optimal size. This rule takes into account the fact that the size of the RAM was usually small at the time and that allocating more than 2X RAM to swap space did not improve performance. With more than twice the swap memory, most systems take more time than it actually takes to perform useful work.
RAM has become a cheap commodity, and now most computers have a lot of RAM, which can be expanded to dozens of GB. Most of my newer computers have at least 8GB RAM, one with 32 GB, and my master workstation with 64 GB. My old computer has 4 to 8GB memory.
When dealing with computers with a large number of RAM, the performance factor of swap space is much lower than that of 2X multipliers. Fedora 28, which defines the current idea of swap space allocation. I list some of the discussions and suggestions in this document below.
The following table provides the recommended size of the swap partition, depending on the number of RAM in the system and whether sufficient memory is required for the system to hibernate. The recommended swap partition size is determined automatically during installation. However, to allow hibernation, you need to edit the swap space during the custom partition phase.
Recommended system swap space in table 1:Fedora 28 documentation
Number of system RAM recommended swap space recommended switching with hibernation less than 2 times the number of 2GBRAM, 3 times the number of RAM equal to 2 times the number of RAM RAM, 0.5 times the number of 8GB-64 GB memory, 1.5 times the number of RAM more than 64 GB workload dependent on hibernation is not recommended
The boundaries between each of the ranges listed above (for example, systems with 2GB, 8GB, or 64 GB system RAM), use caution with the choice of swap space and hibernation support. If your system resources permit, increasing swap space may lead to better performance.
Of course, most Linux administrators have their own ideas about the right amount of swap space-and almost everything else. Table 2 below is my advice based on my personal experience in a variety of environments. These may not apply to you, but like Table 1, they may help you get started.
Table 2: system swap space recommended by the author
Number of RAM recommended swap space ≤ 2GB2x RAM2GB-8GB=RAM > 8GB8GB
One consideration in both tables is that as the number of RAM increases beyond a certain point, adding more swap space will only cause confusion before the swap space is almost filled. If you follow these recommendations with too little virtual memory, you should add more RAM, not more swap space, where possible. As with all recommendations that affect system performance, use the one that is most effective for a particular environment. This will take time and effort to experiment and make changes based on the conditions in your Linux environment.
Add more swap space to a non-lvm disk environment.
Due to the change in swap space requirements on hosts that already have Linux installed, you may need to modify the amount of swap space defined for the system. This process can be used in any general situation where additional swap space is required. It assumes that there is enough disk space available. This procedure also assumes that the disk is partitioned in the "original" EXT 4 and swap partition without logical volume management (LVM).
The basic steps to take are simple:
Close the existing swap space.
Create a new swap partition of the desired size.
Reread the partition table.
Configure the partition as swap space.
Add a new partition / etc/fSTAB.
Open the swap.
A reboot should not be required.
For security reasons, you should at least make sure that the application is not running and swap space is not being used before turning off swapping. This, this, that, that free or top command can tell you whether you are using swap space. For more security, you can revert to runlevel 1 or single-user mode.
Turn off the swap partition using the command to turn off all swap space:
Swapoff-a
Now display the existing partition on the hard drive.
Fdisk-l
This displays the current partition table on each drive. Identifies the current swap partition by number.
Start fdisk in the mode of interacting with commands:
Fdisk / dev/
For example:
Fdisk / dev/sda
At this point, fdisk is now interactive and will only run on the specified disk drive.
Use the fdiskp subcommand to verify that there is enough free space on the disk to create a new swap partition. The space on the hard disk is represented by 512-byte blocks and the number of starting and ending cylinders, so you may need to do some calculations to determine the available space between and at the end of the allocated partition.
Use the n command to create a new swap partition. Fdisk will ask you about starting the cylinder. By default, it selects the lowest numbered available cylinder. If you want to change, type the number of starting cylinders.
This, this, that fdisk command now allows you to enter the size of the partition in a variety of formats, including the last cylinder number or the size in bytes, KB, or MB. Enter 4000 meters, which will provide approximately 4GB space on the new zone (for example), and press Enter.
Use the p subcommand to verify that the partition is created in the way you specified. Note that unless you use the ending cylinder number, the partition may not be specified by you. This, this, that, that fdisk command can only incrementally allocate disk space on the entire cylinder, so the partition may be smaller or larger than specified. If the partition is not what you want, you can delete it and create it again.
It is now necessary to specify the new partition as the swap partition. The branch command t allows you to specify the type of partition. So enter t, specify the partition number, and when it requests the hexadecimal code partition type, type 82, the Linux swap partition type, and then press Enter.
When you are satisfied with the partition you have created, use the w command to write the new partition table to disk. This, this, that, that fdisk program will exit and return to the command prompt after completing the modified partition table. You may receive the following message: fdisk has finished writing the new partition table:
The partition table has been altered calling ioctl () to re-read partition table.WARNING: Re-reading the partition table failed with error 16: Device or resource busy.The kernel still uses the old table.The new table will be used at the next reboot.Syncing disks.
At this point, you can use the partprobe command to force the kernel to reread the partition table so that a reboot is not required.
Partprobe
Now use the following command fdisk-l to list partitions and new swap partitions, which should be in the listed partitions. Make sure the new partition type is "Linux swap".
In order to point to the new swap partition, the / etc/fSTAB file must be modified. The existing lines might look like this:
LABEL=SWAP-sdaX swap swap defaults 0 0
Where X is the partition number. Add a new line similar to this, depending on the location of the new swap partition:
/ dev/sdaY swap swap defaults 0 0
Make sure you use the correct partition number. You can now perform the final step of creating the swap partition. Use the mkswap command to define the partition as a swap partition.
Mkswap / dev/sdaY
The final step is to turn on the swap using the following command:
Swapon-a
Your new swap partition is now online with the previously existing swap partition. You can use free or top commands to verify this.
Add swapping to the lvm disk environment.
If your disk installer uses LVM, changing the swap space will be fairly easy. Again, this assumes that there is available space in the volume group where the current swap volume resides. By default, the installation of FedoraLinux in a LVM environment creates the swap partition as a logical volume. This is easy because you can simply increase the size of the swap volume.
The following steps are required to increase swap space in a LVM environment:
Turn off all swaps.
Increase the size of the logical volume specified for swapping.
Configure the resized volume as swap space.
Open the swap.
First, let's use the lvs command (list logical volumes).
[root@studentvm1] # lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert home fedora_studentvm1-wi-ao---- 2.00g pool00 fedora_studentvm1 twi-aotz-- 2.00g 8.172.93 root fedora_studentvm1 Vwi-aotz-- 2.00g pool00 8.17 swap fedora_studentvm1-wi-ao- -8.00g tmp fedora_studentvm1-wi-ao---- 5.00g usr fedora_studentvm1-wi-ao---- 15.00g var fedora_studentvm1-wi-ao---- 10.00g [root@studentvm1 ~] #
You can see that the current swap size is 8GB. In this example, we want to add 2GB to this swap volume. First, stop the existing exchange. If you are using swap space, you may have to terminate the running program.
Swapoff-a
Now increase the size of the logical volume.
[root@studentvm1] # lvextend-L + 2G / dev/mapper/fedora_studentvm1-swap Size of logical volume fedora_studentvm1/swap changed from 8.00 GiB (2048 extents) to 10.00 GiB (2560 extents). Logical volume fedora_studentvm1/swap successfully resized. [root@studentvm1 ~] #
Run the mkswap command to put the entire 10 GB partition into swap space.
[root@studentvm1 ~] # mkswap / dev/mapper/fedora_studentvm1-swapmkswap: / dev/mapper/fedora_studentvm1-swap: warning: wiping old swap signature.Setting up swapspace version 1, size = 10 GiB (10737414144 bytes) no label, UUID=3cc2bee0-e746-4b66-aa2d-1ea15ef1574a [root@studentvm1 ~] #
Open the swap.
[root@studentvm1] # swapon-a [root@studentvm1] #
Now, use the List block device command to verify that the new swap space exists. Again, no reboot is required.
[root@studentvm1 ~] # lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTsda 8:0 0 60G 0 disk |-sda1 8:1 0 1G 0 part / boot`-sda2 8:2 059G 0 part |-fedora_studentvm1-pool00_tmeta 253disk 0.04M 0 lvm | `- fedora_studentvm1-pool00-tpool 253disk 2 02G 0 lvm | |-fedora_studentvm1-root 253 : 3 0 2G 0 lvm / | `- fedora_studentvm1-pool00 253 lvm 60 0 2G 0 lvm |-fedora_studentvm1-pool00_tdata 253 lvm 1 0 2G 0 lvm |`-fedora_studentvm1-pool00-tpool 253 fedora_studentvm1-pool00_tdata 2 0 2G 0 lvm | |-fedora_studentvm1-root 253 lvm 3 0 2G 0 lvm / | `- fedora_studentvm1-pool00 253 lvm 60 0 2G 0 lvm |-fedora_studentvm1-swap 253 purl 4 010G 0 lvm [SWAP] |-fedora_studentvm1-usr 253v5 0 15G 0 lvm / usr |-fedora_studentvm1-home 253 rom 7 0 2G 0 lvm / home |-fedora_studentvm1-var 253 fedora_studentvm1-home 8 0 10G 0 lvm / var `- fedora_studentvm1-tmp 253 fedora_studentvm1-home 9 0 5G 0 lvm / tmpsr0 11:0 1 1024M 0 rom [root@studentvm1 ~] #
You can also use the swapon-s command, or top, free, or any of several other commands to verify this.
[root@studentvm1] # free total used free shared buff/cache availableMem: 4038808 382404 2754072 4152 902332 3404184Swap: 10485756 0 10485756 [root@studentvm1] #
Note that different commands display or require device special files as input in different forms. There are many ways to access specific devices in the / dev directory.
After reading this article, I believe you have some understanding of "sample Analysis of Linux system Exchange Space". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.