In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you what is the use of Swap partition in Linux system installation. I hope you will get something after reading this article. Let's discuss it together.
Swap partition, that is, swap area, the function of Swap space can be simply described as: when the physical memory of the system is insufficient, a part of the physical memory needs to be released for use by the currently running program. The freed space may come from programs that have been inactive for a long time, and the freed space is temporarily saved in Swap space, and when those programs are about to run, the saved data is recovered from Swap to memory. In this way, the system does not swap Swap until there is not enough physical memory. In fact, the adjustment of Swap is very important to the performance of Linux servers, especially Web servers. By adjusting the Swap, you can sometimes overcome the system performance bottleneck and save the cost of system upgrade.
As we all know, modern operating systems have implemented the technology of "virtual memory", which not only breaks through the limitation of physical memory in function, but also enables programs to manipulate the space larger than the actual physical memory. more importantly, "virtual memory" is a security protection net that isolates each process, so that each process is not disturbed by other programs.
Computer users often encounter this phenomenon. For example, when using a Windows system, you can run multiple programs at the same time, and when you switch to a program that you haven't been paying attention to for a long time, you will hear the hard drive "clap". This is because the program's memory is "stolen" by frequently running programs and placed in the Swap area. Therefore, once the program is placed on the front end, it will retrieve its data from the Swap area, put it into memory, and then run it.
To be clear, not all data swapped out of physical memory will be put into Swap (if so, Swap will be overwhelmed), and a considerable amount of data will be exchanged directly to the file system. For example, some programs will open some files to read and write files (in fact, each program has to open at least one file, that is to run the program itself). When you need to swap out the memory space of these programs, there is no need to put the data of the file part into the Swap space, but you can put it directly into the file. If it is a file read operation, then the in-memory data is released directly and does not need to be swapped out, because the next time it is needed, it can be directly recovered from the file system; if you are writing a file, you only need to save the changed data to the file for recovery. However, the data of objects generated with malloc and new functions are different. They need Swap space because they do not have corresponding "storage" files in the file system, so they are called "Anonymous" in-memory data. This kind of data also includes some state and variable data in the stack. Therefore, the Swap space is the exchange space for "anonymous" data.
Break the 128m Swap limit
Often see some Linux (domestic Chinese version) installation manual has such a description: Swap space can not exceed 128m. Why is there such a saying? Before explaining the origin of the number "128m", give an answer to the question: there is no limit of 128m at all! The current limit is 2G!
The Swap space is paginated, and each page is the same size as the memory page, which facilitates the data exchange between the Swap space and memory. When the old version of Linux implemented Swap space, the Swap space * * page was used as a "bit mapping" (Bit map) of all Swap space pages. This means that every bit of the * page corresponds to a page of Swap space. If this bit is 1, the page Swap is available; if it is 0, the page is a bad block and cannot be used. In this way, the * Swap mapping bit should be 0, because the * page Swap is the mapped page. In addition, * 10 mapping bits are also occupied to indicate the version of Swap (the original version is Swap_space and the current version is swapspace2). So, if the size of a page is s, this Swap implementation can manage a total of "8 * (s-10)-1" Swap pages. For i386 systems, the total space size is 133890048, which is exactly 128m if you think 1 MB= 2 ^ 20 Byte.
The impact of Swap configuration on performance
Allocating too much Swap space wastes disk space, while too little Swap space causes an error.
If the system runs out of physical memory, the system will run slowly but still run; if the Swap space is used up, the system will have an error. For example, the Web server can spawn multiple service processes (or threads) according to the number of requests. If the Swap space is used up, the service process cannot be started, and there will usually be a "application is out of memory" error, which will cause deadlock of the service process. So the allocation of Swap space is very important.
In general, Swap space should be greater than or equal to the size of physical memory, the minimum should not be less than 64m, and usually the size of Swap space should be 2-2.5 times the size of physical memory. However, according to different applications, there should be different configurations: if it is a small desktop system, only a small Swap space is needed, while large server systems need different sizes of Swap space according to different circumstances. Especially the database server and Web server, with the increase of access volume, the requirement of Swap space will also increase. For specific configuration, please see the description of each server product.
In addition, the number of Swap partitions has a significant impact on performance. Because the operation of Swap exchange is the operation of disk IO, if there are multiple Swap exchanges, the allocation of Swap space will operate on all Swap in a rotational square, which will greatly balance the load of IO and accelerate the speed of Swap exchange. If there is only one switching area, all the switching operations will make the switching area very busy, leaving the system waiting most of the time and inefficient. Using the performance monitoring tool, you will find that the CPU is not very busy at this time, but the system is slow. This shows that the bottleneck lies in IO, and the problem cannot be solved by increasing the speed of CPU.
Create a blank file with contiguous space
The physical memory of the server is 512MB, and according to the principle of 1.5 times, I set the swap file to 1GB.
# root@aliyun: / srv# dd if=/dev/zero of=SWAPFILE bs=1024 count=1048576 1048576Secret0 records in 1048576Secret0 records out 1073741824 bytes (1.1GB) copied, 59.7957 s, 18.0 MB/s # root@aliyun: / srv# dd if=/dev/zero of=SWAPFILE bs=1024 count=1048576 1048576Secret0 records in 1048576Secret0 records out 1073741824 bytes (1.1GB) copied, 59.7957 s, 18.0 MB/s use swap file
Use the swapon command to have the system use this file as the swap file. However, this file cannot be used directly, otherwise an error will be reported:
Root@aliyun:/srv# swapon swapfile swapon: / srv/swapfile: read swap header failed: Invalid argument root@aliyun:/srv# swapon swapfile swapon: / srv/swapfile: read swap header failed: Invalid argument
You must first use mkswap to format the file in swap format (I don't know why 4KB is missing):
Root@aliyun:/srv# mkswap SWAPFILE 1048576 Setting up swapspace version 1, size = 1048572 KiB no label, UUID=1aaed031-33ef-479b-a9a4-2f008a7bbb2f root@aliyun:/srv# mkswap SWAPFILE 1048576 Setting up swapspace version 1, size = 1048572 KiB no label, UUID=1aaed031-33ef-479b-a9a4-2f008a7bbb2f
Use the formatted file:
Root@aliyun:/srv# swapon SWAPFILE root@aliyun:/srv# swapon SWAPFILE
View file usage:
Root@aliyun:/srv# swapon-s Filename Type Size Used Priority / srv/SWAPFILE file 1048572 95852-1 root@aliyun:/srv# swapon-s Filename Type Size Used Priority / srv/SWAPFILE file 1048572 95852-1 join automatic enable
To prevent swapfile from taking effect after restart, you can add the swap-enabled code to the startup file. For ubuntu server, edit / etc/rc.local file and add the following (specific file path is customized):
Swapon / srv/SWAPFILE swapon / srv/SWAPFILE
Or
Modify the / etc/fstab file by adding the following:
/ srv/SWAPFILE swap swap defaults 00 / srv/SWAPFILE swap swap defaults 00 after reading this article, I believe you have a certain understanding of "what is the use of Swap partition in Linux system installation". 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.