Data Recovery -> Linux Swap

Linux Swap

The function of swap area, or swap space, is: releasing some space for the present running programs when the physical internal storage is not enough. The released space is mostly from the programs which have not being operated for a long time. This released space is saved to Swap temporarily until the programs need to run again. Thus the system only uses Swap to exchange when the physical storage is not enough. Actually adjustment of Swap is very important for Linux, especially Web server. Through Swap, some terrible troubles can be got trhough and system upgrade cost can be saved.
As we all know that the modern operating systems have realized "virtual memory" technology and it not only break through the limit of physical memory, which makes the programs use more space than the physical memory, it is also a security net for isolating every program. That protects every program from interrupted. Users usually come to such trouble that when using Windows, you try to switch to one long time unoccupied program, you will hear the hard disc making noise. That is because the memory of this program has been "stolen" by some frequently running programs and has been put into the Swap. Therefore, once the program is placed to the front, it will get its data back from the Swap and then go on running. Generally if the situation is that Swap partition is too small, or there is any need to create Linux Swap partition or delete, format, it can be done by the professional partition management software MiniTool Partition Wizard.
In addition, not all the data from the physical memory will be put into the Swap (if it is so, Swap will collapse). A certain amount of data will be exchanged to file system directly. For example, some programs will open some files to read or write (one program will open at least one file---the program itself). When there is any need to exchange the space of these prorams out, it can be directly placed at the files. If it is the read operation, the memory data will be released directly because at the next time the data can be recovered from the file system immediately. If it is the write operation, it only needs to save the changed data to the file for the recovery. But the malloc and new function data are different. They need the Swap space as they have no corresponding reservation files in file system. They are called Anonymous memory data. This kind of data also includes some state or variable data in the stack. So the Swap is the exchange space for Anonymous data.

The Breakthrough of 128M Swap Limit
The Swap space is devided into pages. The size of every page equals to the memory page. It is convenient for the exchange between Swap and memory. The old version Linux uses the first page of Swap space as the Bit map of all the Swap space pages. That means every bit of the first page corresponds to every page of Swap space. If the bit is 1, it means this page is available; if it is 0, it means this page is bad block and cannot be used. So the first Swap map should be 0, because the first page is the map page. Besides, the last 10 bits are occupied to show the version of Swap (the original version is Swap_space, now it is swapspace2). If the size of one page is s, this kind of realization way can manage "8*(s-10)-1" Swap pages. For the i386 system the s equals 4096, so the space is 133890048. If 1MB=2^20 Bytes, it is just 128M. Manage the Swap space like this so that there will not be any bad blocks. If the system finds any bad blocks, it will mark 0 on the corresponding bit to show that this page is unavailable. So when using Swap, it avoids bad blocks and errors.
Generally Swap space equals or is larger than physical memory and it should not be smaller than 64M. It is usually 2-2.5 times larger than physical memory. But with the different applications, it has different configurations: if it is the small desktop system, it only needs smaller Swap space. And the large server system needs different Swap space according to different situations. Especially the data base server and web server, with the increasing visits, the requirement for the Swap space increses, too. Specific configurations please refer to the product introductions of the servers.
Besides, the number of Swap partition has a great effect on the performance, because the operation of Swap is the IO operation of disc. If there are a few Swap apsces, the allocation of Swap space will be in an alternate way to apply to every Swap. That will balance the load of IO and speed up the exchange of Swap. If there is only one Swap space, all the exchange operation will make the space busy and the system be waiting for a long time. The efficiency is very low. Use the performance monitor you will find the CPU is not very busy but the system is still in low speed. That is the problem of IO. Speeding up the CPU will not solve the problem.

System Performance Monitor
The allocation of Swap is important, but the performance monitor while system is running is more worthwhile. Through the performance monitor, you can examine the entire performance index of system and find the problem of system performance. This article only introduces some related orders or usages of Solaries and Swap.
The most common order is Vmstat (most of Unix platforms have such order). This order can examine most of performance index. For example:
# Vmstat 3
Procs memory Swap IO system CPU
      R B W swpd free buff cache si so bi bo in cs us sy id
      0 0 0 0 93880 3304 19372 0 0 10 2 131 10 0 0 99
  0 0 0 0 93880 3304 19372 0 0 0 0 109 8 0 0 100
  0 0 0 0 93880 3304 19372 0 0 0 0 112 6 0 0 100
…………

Introduction of Orders:
The following parameter of Vmstat decides the interval of performance index capture. 3 means one capture every 3 second. The first line of data is worthless. It shows the average performance after the starting up. From the second line it shows the system performance index of every 3 second. Among these indexes the ones related to Swap are the following:
W under procs
It shows the present (within 3 seconds) procedures number that need to release or exchange.
Swpd under memory
It shows the space of Swap in use.
Si, so under Swap
Si shows the present (within 3 seconds) amount of exchanged data that Swap in every one second in Kbytes. So shows the present (within 3 seconds) amount of exchanged data that Swap out every one second in Kbytes.
The amount of index id bigger, the busier the system is. The system busy degree that those indexes show is depended on the specific configuration of system. system manager should record these indexes while the system is operating normally. Compare them with theose when the system is wrong so that the problem will be soon found. Make a standard index value of normally operating system for the performance monitor.
Besides, using Swapon-s can examine the service condition of Swap. For example:
# swapon-s
Filename Type Size Used Priority
/dev/hda9 partition 361420 0 3
It is convenient to see the occupied and unoccupied space of Swap.
Keep the load of Swap under 30% so that the system can keep its fine performance.
 
To enlarge the space of Swap:
1) Become the superuser
$su - root
2) Create Swap files
# dd if=/dev/zero of=swapfile bs=1024 count=65536
Create a exchange file with continuous space.
3) Activate Swap file
#/usr/sbin/swapon swapfile
Swapfile is the exchange file of last step.
4) Now the newly added Swap file is working. But after the reset, the former steps will not be recoeded. So you should record the file name in etc/fstab and the type of Swap. For example:
/path/swapfile none Swap sw,pri=3 0 0
5) check that if Swap file is added with
  /usr/sbin/swapon -s

Delete the redundant Swap space.
1) Become the superuser
2) Use the order Swapoff to take back the Swap space
#/usr/sbin/swapoff swapfile
3) Edit etc/fstab file and take out the entity of this Swap file.
4) Take this file back from the file system.
#rm swapfile
5) Certainly, if this Swap space is not a single file but one partition, you need to create a new file system and connect it to the original file system.
Publisher, webmasters and all viewers are allowed to reprint all useful articles on our site freely.
But note that you must add the following credit information to anywhere that you put our website resources:
"Article reprint is permitted by www.data-recovery-app.com" and a hyperlinke back to http://www.data-recovery-app.com is required.