When I look at the code, I can see that an anonymous memory is mapped through mmap, but why do I have to map an anonymous memory? What is the use of the anonymous memory? I have been puzzled by this problem. Today, google has a lot of information to summarize what I understand.
1. Anonymous memory Definition
Anonymous memory refers to the memory that does not have the corresponding "reserve file" in the file system. The memory of heap and stack space belongs to anonymous memory.
2 anonymous memory usage
Anonymous memory comes from swap partitions, that is, swap partitions in linux.
The role of the Swap space is described as follows: when the system's physical memory is insufficient, a part of the physical memory needs to be released for use by the currently running program. The released space may come from some programs that haven't been operated for a long time. The released space is temporarily saved to the Swap space and will be executed by those programs, then, the stored data is restored from Swap to the memory. In this way, the system always performs Swap switching when the physical memory is insufficient.
You may often encounter this phenomenon. For example, when you use a linux system to run multiple programs at the same time, when you switch to a program that has not been noticed for a long time, you will hear the hard disk crash. This is because the memory of this program is "stolen" by frequently running programs and put 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 the memory, and then run.
It should be noted that not all data exchanged from the physical memory will be put into Swap (if so, Swap will be overwhelmed ), A considerable amount of data is directly exchanged to the file system. For example, some programs open some files and read and write the files (in fact, each program must open at least one file, that is, run the program itself ), when you need to Swap out the memory space of these programs, there is no need to put the data in the file part into the Swap space, and you can directly put it in the file. If it is a file read operation, the memory data is directly released and does not need to be exchanged, because it can be directly restored from the file system when needed next time; if it is a file write operation, you only need to save the changed data to the file for recovery. However, the data of objects generated using the malloc and new functions is different. They need Swap space because they do not have the corresponding "reserve" file in the file system, therefore, it is called "Anonymous" (Anonymous) memory data. This type of data also includes some status and variable data in the stack. Therefore, the Swap space is the Swap space for "anonymous" data.
3. Impact of swap partition on Performance
If you allocate too much Swap space, the disk space will be wasted, and the Swap space is too small, the system will encounter an error. If the system's physical memory is used up, the system will run slowly but still run. If the Swap space is used up, the system will encounter an error. For example, a Web server can generate multiple service processes (or threads) based on the number of requests. If Swap space is used up, the service process cannot be started, generally, the error "application is out of memory" may occur. In severe cases, the service process may be deadlocked. Therefore, Swap space allocation is very important.
4. swap space monitoring method
The allocation of Swap space is important, but the performance monitoring during system operation is more valuable. Through performance monitoring tools, you can check the performance indicators of the system and find the bottleneck of the system performance. This article only describes some Swap-related commands and usage in Solaris.
The most common command is the vmstat command, which allows you to view most performance metrics.
Command description:
Parameters after vmstat specify the time interval for performance indicator capture. 3 indicates capture every three seconds. The first line of data does not need to be viewed and has no value. It only reflects the average performance since the startup. Starting from the second line, the system performance indicators within three seconds are reflected. Swap-related performance indicators include the following:
W under procs
It indicates the number of processes that need to be released and swapped out in the current (within three seconds.
Swpd under memory
It indicates the size of the Swap space used.
Si, so under Swap
Si indicates the total number of Swap memories exchanged per second (within three seconds), measured in kbytes. so indicates the total number of Swap memories exchanged per second (within three seconds, the Unit is kbytes.
The larger the number of indicators, the more busy the system. The system busy level of these indicators depends on the specific configuration of the system. During normal system operation, the system administrator should write down the values of these indicators and compare them when a system problem occurs. Then, the system administrator will soon find the problem, standard indicators for normal operation of the system are formulated for performance monitoring.
5. add and delete swap space
To increase the Swap space, take the following steps:
1) become a Super User
$ Su passwd root
2) create a Swap file
# Dd if =/dev/zero of = swapfile bs = 1024 count = 65536
Create a swap file with continuous space.
3) activate the Swap file
#/Usr/sbin/swapon swapfile
Swapfile refers to the swap file created in the previous step. 4) now the newly added Swap file has taken effect, but after the system restarts, it does not remember the previous steps. Therefore, you must record the file name and Swap type in the/etc/fstab file, for example:
/Path/swapfile none Swap sw, pri = 3 0 0
5) check whether the Swap file is added
/Usr/sbin/swapon-s
Delete unnecessary Swap space.
1) become a Super User
2) use the Swapoff command to reclaim Swap space.
#/Usr/sbin/swapoff swapfile
3) edit the/etc/fstab file and remove the entity of the Swap file.
4) reclaim the file from the file system.
# Rm swapfile
5) Of course, if the Swap space is not a file but a partition, you need to create a new file system and then mount it to the original file system.