Original Address http://blog.chinaunix.net/uid-20274021-id-1969706.html
Suitable for general Linux systems, so turn around.
Solaris Swap Space Management
What is swap (swap) space
For a typical Solaris system administrator, there is little exposure to swap space, where they see the swap as just one or two partitions on disk or several swap files, and when the system does not have enough physical memory to handle the current process, The idea of using swap space as a temporary storage space for virtual memory is technically correct, but Solaris has a unique place to implement swap.
Swap space function
As we all know, modern operating systems have realized the technology of "virtual memory", not only the functional breakthrough in the physical memory limit, so that the program can manipulate more than the actual physical memory space, more importantly, "virtual memory" is the isolation of each process security protection network, so that each process is not interfered by other programs.
The role of swap space can be simply described as: when the system's physical memory is not enough, you need to release some of the space in physical memory for use by the currently running program. The freed space may come from programs that are not operating for a long time, and these freed spaces are temporarily saved to the swap space, waiting for those programs to run, and then recover the saved data from swap to memory. In this way, the system always swaps when there is not enough physical memory. This phenomenon is often encountered by computer users.
One thing to declare is that not all data exchanged from physical memory is put into swap (in which case, swap is overwhelmed), and a significant portion of the data is exchanged directly to the file system. For example, some programs open some files, read and write to the file (in fact, each program has at least one file open, that is, running the program itself), when the memory space of these programs need to swap out, the file part of the data is not necessary to put into the swap space, if it is read file operations, Then the memory data is released directly, and does not need to be swapped out, because the next time you need it, you can restore it directly from the file system; If you write a file, you just need to save the changed data to a file for recovery. However, the data for objects generated with the malloc (3C) and new functions are different and require swap space because they do not have a corresponding "reserve" file in the file system, so called "anonymous" (Anonymous) memory data, This kind of data also includes some state and variable data in the stack, so that the swap space is the swap space of "anonymous" data.
The performance impact of swap configuration
Too much swap space will waste disk space, and too little swap space, the system will be wrong.
If the system runs out of physical memory, your system will run slowly, but it can still run, and if the swap space runs out, the system will be wrong. For example, a Web server can derive multiple service processes (or threads) depending on the number of requests, and if the swap space is exhausted, the service process cannot be moved in, and the "application is out of memory" error usually occurs, causing a deadlock in the service process. Therefore, the allocation of swap space is very important.
Typically, the swap space should be greater than or equal to the physical memory size, the minimum should not be less than 64M, and usually the size of the swap space should be 2-2.5 times times the physical memory (the Solaris 2 version has changed, see below). But according to different applications, there should be different configuration: If the small desktop system, only need a small swap space, and large server systems, depending on the situation requires different size of swap space. In particular, the database server and Web server will increase the amount of traffic, the demand for swap space will also increase, specific configuration see the respective server product description.
In addition, the number of swap partitions has a significant impact on performance. Because swap is the operation of disk I/O, if there are more than one swap swap area, the allocation of swap space will rotate in the way of all the swap, which will greatly balance the I/O load, speed swap exchange speed. If there is only one swap area, all of the swap operations will make the swap area very busy, make the system most of the time in the waiting state, inefficient, with performance monitoring tools will find that at this time the CPU is not very busy, and the system is slow, this shows that the bottleneck in I/O, rely on increasing the speed of the CPU can not solve the problem.
Performance monitoring
The allocation of swap space is very important, while the system performance monitoring at runtime is more valuable, through performance monitoring tools can check the performance of the system indicators, to find the system performance bottlenecks. This article only describes some of the commands and uses that are related to swap under Solaris.
The most commonly used is the Vmstat command, which is available under most Unix platforms, and this command can view most performance metrics.
Using Swap-s can also be a simple view of the current swap resource usage. For example:
# swap-s
total:65896k Bytes Allocated + 56840k reserved = 122736k used, 1069456k available
It is easy to see the use of swap space and the size of unused resources. Swap should be kept below the load of 30% to ensure the system's good performance.
Features of swap in Solaris
Virtual Swap Space
Originally swap space is for virtual memory service, and now the Solaris swap space is also virtual, this is what is going on.
Let's take a look at an example to see that a problem often arises when programming on a previous Solaris 2 version of Solaris (or other Unix, such as Linux):
Assuming that the system currently has available memory space of 30M and only 10M of swap space, then if a process starts running and attempts to execute malloc (15*1024*1024) command (allocating 15M space), the process will fail because of this command.
Why, then? Does the system have 30M of available memory space? The reason is: your swap space is not enough, the system that you do not allocate space, the ability (space) in the occurrence of page exchange, this part of the data saved, so that you do not qualify to allocate this space. It's not so unfair. Perhaps this 15M space does not need to exchange at all, the current system but also has 30M memory space surplus Ah!
What's more unfair? Some large systems are equipped with a huge amount of memory, 1G or 4G, with so much memory is to avoid switching, improve the speed of the system, but also for the system to allocate unnecessary swap space, occupy a large number of disk resources.
To remedy this flaw, sun designed virtual swap space for later versions of Solaris 2. The so-called virtual swap space, the concept is actually very simple, swap space is no longer a single hard disk partition or file. The virtual swap space consists of two parts: some physical memory and a traditional swap partition. After the appropriate configuration, you can make the system need swap space, first use the memory part of the swap space, if the memory part of the swap space is not enough, then use the disk part of the swap space. That way, maybe the swap space on your hard drive is rarely used, or even the swap partition is not needed at all.
The relationship between swap space and Tmpfs file system
Do you know. Virtual swap space has a significant relationship with the/tmp directory. Sun is implementing
/tmp directory, take full account of the efficiency of the application running. Many applications, especially database services, are used frequently
The/tmp directory acts as a staging data save area, and Solaris
Files in the/tmp directory are placed in memory instead of the hard drive, which can greatly increase the efficiency of your application.
But the/tmp directory space is squeezed out of the system virtual space and is part of the virtual swap space. If you use the/TMP space, that is, use the swap space, so be careful to monitor the system/TMP directory usage, do not run out, otherwise the system will be paralyzed! Here are two suggestions for your reference:
1. In the mount/tmp directory, use the (-o Size) option to control the size of the/tmp directory.
2. When using the compiler to compile the file, if you do not want to occupy the swap space, use the TMPDIR environment variable to point to another temporary directory instead of the/tmp directory.
system commands for Swap space operations
Increase swap space
1. Become Superuser $su-root
2. Create swap file #mkfile NNN[KLBLM] FileName
such as: #mkfile 100m swapfile1
3. Activate swap file
#/usr/sbin/swap-a/path/filename
The swap file must be specified as an absolute path, and filename refers to the file created in the previous step.
4. The new swap file is now working, but after the system restarts, the previous steps will not be remembered. Therefore, to record the name of the file in the/etc/vfstab file, and the type of swap, such as:
/path/filename--Swap-no-
5. Whether the parity swap file plus/usr/sbin/swap-l
Remove Excess swap space
1. Become a Super User
2. Use the swap-d command to reclaim swap space.
#/usr/sbin/swap-d/path/filename
3. Edit the/etc/ufstab file and remove the entity from the swap file.
4. Recycle this file from the file system.
#rm Swap-filename
5. Of course, if this swap space is not a file, but a partition, you need to create a new file system, and then hook up to the original file system.
As we all know, modern operating systems have realized the technology of "virtual memory", not only the functional breakthrough in the physical memory limit, so that the program can manipulate more than the actual physical memory space, more importantly, "virtual memory" is the isolation of each process security protection network, so that each process is not interfered by other programs.
The role of swap space can be simply described as: when the system's physical memory is not enough, you need to release some of the space in physical memory for use by the currently running program. The freed space may come from programs that are not operating for a long time, and these freed spaces are temporarily saved to the swap space, waiting for those programs to run, and then recover the saved data from swap to memory. In this way, the system always swaps when there is not enough physical memory. This phenomenon is often encountered by computer users.