Programmer details about the native and third-party virtual memory mechanism of iOS

Source: Internet
Author: User

Virtual Memory. This technology is essentially a ing of memory addresses, making the process think that it has a continuous, large amount of memory, improve memory utilization, and reduce programming difficulty. Therefore, the virtual memory category can be divided into two categories: the first category: map the memory address occupied by the process to other locations in RAM, the second category: map the memory address occupied by the process to the disk. IOS5 must have the first type of virtual memory, but there is no second type.
 
First, we will introduce the virtual memory. This technology is essentially a ing of memory addresses, making the process think that it has a continuous, large amount of memory, improve memory utilization, and reduce programming difficulty. For example, a program is notified by the system that its available memory segments are 0 to 100 pages. In fact, the occupied memory segments may be scattered. It is possible that the actual physical usage range is 70-201 pages, 240 pages to pages, and 10 pages on the disk.
 
Therefore, the virtual memory category can be divided into two categories:
Category 1: map the memory addresses occupied by processes to other locations in RAM.
Category 2: map the memory address occupied by the process to the disk.
In general, the virtual memory is the second type.
 
The first type is implemented in RAM, which is fast and has dedicated hardware for conversion. It is like changing the room number of a hotel, the execution of the program has no effect.
 
The second type is because the disk speed is too slow to read and write, and many of them will have a limit on the number of reads and writes. Therefore, when the page on the disk is to be used, it is not directly modified on the disk. Instead, it is moved back to RAM and the process is temporarily frozen. After the migration is completed, it is modified in RAM. Non-active pages in RAM will also be moved to the disk when the memory is insufficient to provide available physical memory for the active process. That is to say, a disk is equivalent to a warehouse, and the real work is still in RAM.
In this way, some memory-consuming large programs can be run on some small memory machines, but the disadvantage is that it is slow and card.
 
IOS5 must have the first type of virtual memory, but there is no second type.
 
First of all, if you use virtual memory, it will inevitably lead to a certain degree of slowness, card, you should have experienced when the memory on the PC is full. This is what Apple does not want. Apple must make a technology available smoothly on devices. This is easy to understand. That's how multitasking works.
 
Second, the device will automatically turn off some background programs when the memory is insufficient. If this technology is used, there will be no insufficient memory. Once the memory is insufficient, the system will automatically move some non-active processes in the memory to the disk to provide space for the active programs, so that all programs will be retained in the background, in the end, the disk space occupied by the virtual memory will become larger and larger. In fact, this is not the case. Apple's design is to allow users not to close these background programs.
Of course, you can also think that the virtual memory of iOS is not provided to applications. But if so, what is the use of this virtual memory?
 
In addition, Apple also said at a WWDC conference after the press conference: limited memory/virtual memory/no swapfile. That is to say, there is no virtual memory in the general sense.
 
As for some developers, they found that there was a VM value during the top input in terminal, and suspected that it was the virtual memory size. I don't know what it is, but I don't think so. The value will indeed grow as the program grows, or even reach 4 GB.
 
The following is my analysis method. I filled up all the user and system disks and found that the system is still running normally. When I open those programs, the value of the VM increases as well, and the value can eventually reach 4 GB or above. Where is the space? If you say it is not impossible except the system disk and the user disk, but we can calculate it. I am a 32 GB user disk with a size of 29754 MB and a system disk with a size of 1024 MB. The total size is 30778M, that is, 30 GB. Where can I add the remaining 4 GB? Generally, the actual available space is smaller than the nominal space due to conversion and other factors.
 
What is the memory management in iOS? I guess this is the case.
 
① When the memory is insufficient, it is first called the background program or the system process to release. At this time, the background program will take the initiative to release some less important data, such as image information, retain the most important status information, at the same time may also be compressed memory data. At this time, because the CPU resources are occupied, choppy may occur.
 
② When the memory is still insufficient, the system will consider shutting down some background programs. At this point, the background program will receive a signal, and then start running, save the data, and then exit and release the memory. At this time, because it will occupy the processor and storage, it may cause a lag again.
 
③ If the problem persists, the system forcibly ends the foreground program and leaves a bunch of lowmemory error reports under/var/logs/AppleSupport. This is a common reason for the crash.
 
It can also be explained that the memory management in iOS is indeed very advanced, and there is indeed no need to close the background program. Of course, if you think that the small card caused by Step 1 makes you uncomfortable, you should take the initiative to close it.
 
After completing the memory management of the iOS system, let's talk about the virtual memory installed with deb, that is, the virtual memory in the true sense.
 
Some people say that it is useless to enable this virtual memory. It can only make the memory seem to increase a lot, but it is not actually used, it will also lead to system instability.
 
What I want to clarify here is:
 
① Virtual memory does not increase the memory of your device, but just makes room for running programs.
 
For example, the virtual memory does not increase the area of your work room, but it provides you with a warehouse to move something that is currently useless, in this way, you can have more space to do what you are doing, and the warehouse is not the place to work.
 
② Virtual memory would not have caused device system instability. People who used it in the iOS3 era should know that this was only a problem that emerged after the iOS4 era.
 
③ Whether the virtual memory will affect the service life of the device, which I think can be ignored. By checking the Memory Page output for one day, it is equivalent to the amount of data written into the flash memory. If the virtual memory is not enabled, it is about several MB. If the virtual memory is enabled, it is about MB. If you enable this function, it is about 256 MB, that is, only once a day can be fully written in this region.
 
Of course, some fengyou are worried about repeated writes to the same block. In fact, there is no need to worry about this, because there is a balance between flash memory loss, it will try to minimize the number of write writes, and every time you restart the virtual memory file is re-created. In addition, the flash memory is written only when the memory is insufficient, and the most important reading will not affect the service life. The number of nand FLASH memory writes is about 0.1 million. How much influence does it have when combined with the total capacity?
 
The main purpose of using the virtual memory is to provide more physical memory for the currently running program to prevent system freezing and crash due to insufficient memory, of course, you can also run more programs in the background.
 
To some extent, using the virtual memory may cause freezing of the switching program. At this time, the system is transferring data from the disk to the memory.
 
UNIX virtual memory is like this. When there is no shortage of memory, it begins to write some inactive pages in the memory to the disk. In this way, when the process requires memory, you can directly allocate this part to processes. If these non-active pages are not assigned and the processes occupying them need to modify the data stored in them, you can also directly modify the data, therefore, the only operation that may cause freezing is to activate the process where pages are exchanged to the disk. In this case, you only need to read part of the data on the disk to the memory. After testing, the read Speed of toumethane flash memory is close to 40 MB/s. That is to say, if a process occupies 40 MB of memory and is fully switched to the flash memory, the process will be suspended for 1 s at most. In fact, few programs occupy 40 m of memory, basically games, and rarely swap all of them to flash memory, there is no need to switch all pages to the memory to activate this process. In addition, do you still remember to switch the transition animation of the program? It seems that there are also 1 s. As a result, it is almost impossible to feel stuck. Even if there is one thing, it doesn't matter. It's better than the background being turned off and the front-end flashing back.
 
Therefore, the virtual memory is still very good, not just to make the memory look larger.
 
Next, let's take a look at the principle of deb virtual memory.
In fact, the deb principle of all virtual memory is exactly the same, so it is meaningless to compare its stability horizontally. In short, its functions only enable the native functions of the system.
 
After all the virtual memory deb is unwrapped, there is a plist file in/system/library/launchdeamons. This path stores the configuration files of all boot processes. Generally, this plist points to the started program dynamic_pager in/sbin. Some point to the vm, and this vm is a program in sbin after deb is installed. It is essentially the same as dynamic_pager. Other files are only for some auxiliary purposes, such as fm used to release the memory, and one is used to disable virtual memory encryption.
 
Next, let's talk about what dynamic_pager is.
In fact, it is not a virtual memory process. virtual memory does not require a process and is a function of the operating system. This process communicates with the system and is responsible for creating and deleting virtual memory files. If you force terminate the process, the virtual memory is still available, but you cannot increase the number of swap files. Once the swap file is full, the current program will be stuck.
 
Log on to root in terminal and enter dynamic_pager and press enter to enable virtual memory. This process has the following options:
-F the size of a single virtual memory swap file. The default value is 64 mb. The number of file bytes is entered later.
-S virtual memory swap file path and name, in/var/vm by default, the default file name is swapfile number.
-H: Create a new swap file when the total remaining space of the swapfile is less than the number of bytes.
-L Delete idle swap files when setting the total remaining space of swapfile to more than how many bytes
-P priority, but it seems useless.
 
I have clearly explained the role of this program. Next I will talk about its origins.
 
Many people think that this is a built-in system, but it is not. This is automatically installed on cydia after jailbreak. You can open cydia and find it in the software package that you just installed after jailbreak. You can view it in the file system after clicking it.
 
In short, dynamic_pager is very important. Although it is not native, it is used to enable the virtual memory of the system. We can know that iOS native supports Virtual Memory, it's just Apple's removal.
 
After that, I will explain how to manage the virtual memory swap file.
After my experiments and some materials, I summarized the management features.
The ing method is likely to be directly mapped from the memory address to the physical address of the flash memory. That is to say, the read and write operations are directly written according to the physical address of the flash memory without going through the file management system. Therefore, deleting a virtual memory file does not affect the operation of the virtual memory. The only purpose of generating this file is to take a place and let the operating system and other programs know that this region is useful, prevent other programs from creating files at this address, resulting in memory data tampering.
In addition, no matter how you set the permissions, even if all the permissions are revoked, each user group reads, writes, and executes the operation. This does not affect the virtual memory, and the swap files are modified. It is likely that it is completely independent from the file management system. Therefore, it makes no sense to modify its permissions. Many people say that the change to 777 is actually more stable.
 
Finally, we are most concerned about why the virtual memory is unstable. For details, see.
 
The direct cause of system instability caused by virtual memory is the crash and error of important processes.
Crash is a good result.
If it is a common program, it is a crash, safari is the most typical.
For springboard, it is the security mode.
If it is launch, restart.
 
Process errors may occur on the machine. The most serious problem is the modification of some files, which is also an important cause of white Apple Caused by virtual memory, of course, I have also encountered phenomena such as the disappearance of all programs.
 
Every time a process crashes, an error report is reported under/var/logs/AppleSupport. After a long period of collection and sorting, it is found that the main types of errors are SIGABRT or SIGBUS. This is a common memory error. It is generally caused by a wrong Memory Address requested by the process. This address is included in the error report. I also found that the request addresses are beyond the RAM range. That is to say, the request is the part that is exchanged to the flash memory.
 
A machine with virtual memory enabled. In this case, the system detects that the address of the process request overflows. In this case, an interrupt occurs, that is, the processor stops processing the task currently being processed, instead, it processes a newly added task, that is, it transfers the data mapped to the disk area to the memory, and then restores the previous task. That is to say, in this case, the system is probably not interrupted at this time. The currently running task is not stopped, and data transfer is not performed, which leads to memory address errors. This can also be interpreted as a process error. Data transfer may not be completed, but the original task starts to run. At this time, the overflow memory address has been mapped to the memory area and no memory error occurs, however, the data transfer is not completed. That is to say, the data in this area is not all the data in flash memory, but the result is a process error.
 
This is the cause.
 
To be smooth, Apple is extremely responsive. We also know that the rendering priority of the ios user interface is very high. It is entirely possible that Apple directly regards the rendering of the user interface as an interruption. Interruption also has a priority. If the interrupt priority is higher than the virtual memory, the above situation may occur. The data has not been transferred, but the virtual memory task has been stopped, the processor starts processing the task of rendering the user interface. If the data rendered on the user interface is switched to the flash memory, but the data is not transferred to RAM, the memory address overflow or process error may occur. The most typical is the security mode and the screen. Springboard is the most closely related to user interface rendering, which can also explain why the process crashes most times.
 
To solve this problem, it is necessary to reduce the user interface rendering priority or cancel the right of disconnections, of course, it can also increase the priority of virtual memory interruption. This step remains to be studied.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.