In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you the example analysis of the exchange space on the Linux system, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
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 all RAM is occupied and more memory is needed.
For example, suppose you have a 8GB RAM computer. If the program you start is not filled with RAM, everything is fine and does not need to be swapped. Suppose you are working on a spreadsheet, and as you add more rows, your spreadsheet will grow, plus all running programs, will take up all the RAM. If there is no swap space available at this time, you will have to stop working on the spreadsheet until you close some other programs to release some RAM.
The kernel uses a memory manager to detect blocks of memory (memory pages) that have not been used recently. The memory manager swaps these relatively infrequently used memory pages to a special partition on the hard disk specifically designated for "paging" or swapping. This frees up the RAM, making room for entering more data in the spreadsheet. Memory pages that are swapped out to the hard disk are tracked by the kernel's memory management code and can be paged back to RAM if necessary.
The total amount of memory in a Linux computer is RAM + swap partition, which is called virtual memory.
Linux switch Partition Typ
Linux provides two types of swap space. By default, most Linux will create a swap partition at installation time, but you can also use a specially configured file as the swap file. A swap partition, as its name implies, is a standard disk partition, and the swap space is specified by the mkswap command.
Swap files can be used if there is no available disk space to create a new swap partition, or if there is no space in the volume group to create logical volumes for swap space. This is just a regular file that has been created and pre-allocated the specified size. Then run the mkswap command to configure it as swap space. I do not recommend using files as swap space unless absolutely necessary. (LCTT translation note: recent versions of Ubuntu use swap files instead of swap space, so I have reservations about this statement.)
Frequent exchange
Frequent swapping may occur when the total virtual memory (RAM and swap space) becomes fast full. The system spends so much time switching memory blocks between swap space and RAM that there is little time for actual work. A typical symptom of this situation is that the system becomes slow or completely unresponsive, and the hard drive indicator lights almost continuously.
Use the free command to show the CPU load and memory usage, and you will find that the CPU load is very high, possibly 30 to 40 times the number of CPU cores on the system. Another situation is that RAM and swap space are almost completely allocated.
In fact, viewing the SAR (system activity report) data can also display this content. I install SAR on each of my systems and use these for data analysis.
What is the correct size of the swap space?
Many years ago, the amount of swap space allocated on the hard disk was twice as large as the RAM on the computer (of course, this was when most computers had RAM in KB or MB units). Therefore, if a computer has a RAM for 64KB, you should assign a swap partition for 128KB. The rule takes into account the fact that the RAM size was very small at the time and that allocating more than twice the RAM for swap space did not improve performance. Swapping with more than twice as much RAM takes more time than most systems actually do useful work.
RAM is cheap now, and most computers now have a few gigabytes of RAM. Most of my new computers have at least 8GB memory, one has 32GB memory, and my main workstation has 64GB memory. My old computer has 4 to 8GB memory.
When operating a computer with a large number of RAM, the limited performance factor of swap space is much less than 2 times. The Fedora 28 online installation guide defines the current method of swap space allocation. The following are my suggestions.
The following table provides the recommended size of the swap partition based on the size of the RAM in the system and whether there is enough memory for the system to sleep. The recommended swap partition size is established automatically during installation. However, in order for the system to sleep, you need to edit the swap space during the custom partition phase.
Table 1: recommended system swap space in Fedora 28 documentation
< 如显示不全,请左右滑动 >System memory size recommended swap space recommended swap space size (support hibernation mode) less than 2 GB2 times RAM3 times RAM2 GB-8 GB equals 2 times RAM size RAM8 GB-64 GB0.5 times RAM1.5 times RAM greater than 64 GB workload related not recommended sleep mode
The boundaries between each of the ranges listed above (for example, RAM for systems with 2GB, 8GB, or 64GB), use caution depending on the swap space selected and the support for hibernation. If your system resources permit, increasing swap space may lead to better performance.
Of course, most Linux administrators have their own ideas about how much space to swap. Table 2 below contains suggestions based on my personal experience in a variety of environments. These may not be right for you, but like Table 1, they may help you.
Table 2: system swap space recommended by the author
< 如显示不全,请左右滑动 >Recommended swap space ≤ 2GB2X RAM2GB-8GB = RAM > 8GB8GB for RAM size
What these two tables have in common is that as the number of RAM increases, adding more swap space beyond a certain point will only result in frequent swapping before the swap space is almost fully used. According to the above recommendations, you should add as much RAM as possible instead of adding more swap space. As in similar situations that affect system performance, please use the advice that works best for you. It takes time and effort to test and change according to the conditions in the Linux environment.
Add more swap space to a non-LVM disk environment
In the face of hosts with Linux installed and changing requirements for swap space, it is sometimes necessary to modify the size of the system-defined swap space. This procedure can be used in any situation where the size of the swap space needs to be increased. It assumes that there is enough disk space available. This process also assumes that the disk partition is "original" EXT4 and swap partition, rather than using logical volume management (LVM).
The basic steps are simple:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
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 to / etc/fstab.
Open the swap space.
There should be no need to restart the machine.
For security reasons, you should at least make sure that no applications are running and no swap space is in use before turning off swap space. The free or top command can tell you whether swap space is in use. 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 look at the existing partitions on the hard drive.
Fdisk-l
This displays the partition table on each drive. Identifies the current swap partition by number.
Start fdisk in interactive mode using the following command:
Fdisk / dev/
For example:
Fdisk / dev/sda
At this point, the fdisk is interactive and operates only on the specified disk drive.
Use the p subcommand of fdisk to verify that there is enough free space on disk to create a new swap partition. The space on the hard disk is displayed in 512-byte blocks and starting and ending cylinder numbers, so you may need to do some calculations to determine the free space between the allocated partitions and at the end.
Use the n subcommand to create a new swap partition. Fdisk will ask you to start the cylinder. By default, it selects the available cylinder with the number *. If you want to change this, enter the number of the starting cylinder.
The fdisk command allows you to enter the size of a partition in a variety of formats, including * a column number or byte, KB or MB size. For example, type 4000m, which will provide approximately 4GB space on the new partition, and then press enter.
Use the p subcommand to verify that the partition was created as specified. Note that unless you use the end cylinder number, the partition may not be exactly the same as you specified. The fdisk command can only incrementally allocate disk space across the cylinder, so your partition may be slightly smaller or larger than you specified. If the partition is not what you want, you can delete it and recreate it.
Now specify that the new partition is a swap partition. The subcommand t allows you to specify the type of partition. So enter t, specify the partition number, and when it requires the hexadecimal partition type, enter 82, which is the Linux swap partition type, and then press enter.
When you are satisfied with the partition you have created, use the w subcommand to write the new partition table to disk. The fdisk program exits and returns to the command prompt after writing the modified partition table. When fdisk finishes writing to the new partition table, it receives the following message:
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 use the partprobe command to force the kernel to reread the partition table so that there is no need to restart the machine.
Partprobe
Use the command fdisk-l to list the partitions, and the new swap partition should be in the listed partitions. Make sure the new partition type is "Linux swap".
Modify the / etc/fstab file to point to the new swap partition. As follows:
LABEL=SWAP-sdaX swap swap defaults 0 0
Where X is the partition number. Depending on the location of the new swap partition, add the following:
/ dev/sdaY swap swap defaults 0 0
Make sure you use the correct partition number. You can now perform the * * step of creating a swap partition. Use the mkswap command to define the partition as a swap partition.
Mkswap / dev/sdaY
* * one step is to enable swap space using the following command:
Swapon-a
Your new swap partition is now online with the previously existing swap partition. You can use the free or top commands to verify this.
Add swap space in a LVM disk environment
If your disk uses LVM, it will be quite easy to change the swap space. Similarly, assume that there is free space in the volume group in which the current swap volume resides. By default, Fedora Linux in the LVM environment creates the swap partition as a logical volume during the installation process. You can simply increase the size of the swap volume.
Here are the steps to increase the size of swap space in a LVM environment:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Turn off all swap space.
Increase the size of the logical volume specified for swap space.
Volume configuration that resizes for swap space.
Enable swap space.
First, let's use the lvs command (listing logical volumes) to verify that swap space exists and that it is a logical volume.
[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.17swap 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 space size is 8GB. In this case, we want to add 2GB to this swap volume. First, stop the existing swap space. If swap space is in use, 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 turn the entire 10GB 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 ~] #
Re-enable swap space.
[root@studentvm1] # swapon-a [root@studentvm1] #
Now, use the lsblk command to verify that the new swap space exists. Again, there is no need to restart the machine.
[root@studentvm1 ~] # lsblkNAME 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 253 fedora_studentvm1-pool00-tpool 0 04M 0 lvm | `- fedora_studentvm1-pool00-tpool 253 fedora_studentvm1-pool00-tpool 2 0 2G 0 lvm | |-fedora_studentvm1-root 253 3 0 2G 0 lvm / |`-fedora_studentvm1-pool00 253 fedora_studentvm1-root 60 0 2G 0 lvm |-fedora_studentvm1-pool00_tdata 253 fedora_studentvm1-pool00-tpool 1 0 2G 0 lvm | `- fedora_studentvm1-pool00-tpool 253 2 0 2G 0 lvm | | |-fedora_studentvm1-root 253fedora_studentvm1-pool00 3 0 2G 0 lvm / | `- fedora_studentvm1-pool00 253 fedora_studentvm1-pool00 60 2G 0 lvm |-fedora_studentvm1-swap 253 fedora_studentvm1-pool00 4 0 10G 0 lvm [SWAP] |-fedora_studentvm1-usr 253 fedora_studentvm1-pool00 5 015G 0 lvm / usr |-fedora_studentvm1-home 253 fedora_studentvm1-pool00 7 02G 0 lvm / home |- Fedora_studentvm1-var 253 fedora_studentvm1-var 8 0 10G 0 lvm / var `- fedora_studentvm1-tmp 253 var 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 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 input of device files in different forms. There are several ways to access a specific device in the / dev directory.
The above is all the contents of the article "sample Analysis of swap Space on Linux system". 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.
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.