WWDC-iOS memory performance and Principle notes

Source: Internet
Author: User

WWDC-iOS memory performance and Principle notes
How is the iOS memory initialized and managed?

In iOS, the pointer address range is very large, 32-bit CPU has 4 GB size, 64-bit CPU has 18 EB size (about 2 to 60 power ), such a large pointer address range makes it seem that the system memory is so large. In fact, the physical memory may not be so large. This size is called virtual memory, in OS X, the system uses the hard disk to store infrequently used data in the memory as the backup storage of the memory. The pointer address of the hard disk data is stored in the memory, and the data is written into the memory only afterwards.

However, there is no backup storage in iOS, And the read-only data in iOS already exists on the hard disk and is written to the memory as needed. The read/write data is stored in the memory and will not be removed, once the available memory reaches the critical value, the system will issue a warning that the memory is insufficient and the application will release the resources. If the release fails or the warning is not cleared, the system will terminate the program directly.

Virtual Memory
IOS physical memory is split into 4 kb pages, and not all pages can be accessed by applications. the virtual memory is a layer between the kernel and the application layer. because the process of directly calling the kernel to apply for memory is very costly, therefore, the underlying layer will directly apply for space with the virtual memory, and the virtual memory will communicate with the kernel, and create VM objects to match the physical memory ,:

In the heap of the memory, only part of the data of our application is occupied, and other objects and caches created by the framework are available. In the memory, there are some static constants, thread stacks, and image data, CALayer cache, database cache

Memory type: Clean memory and dirty memory

Clean memory: Copies data from disks to memory space, such as code, framework, and memory ing files.
Dirty memory: other memory space, such as initialization data on the heap, database cache, and extracted image data.
Most of the data initialized by the application is dirty memory.
Example:

- (void)displayWelcomeMessage {NSString *welcomeMessage = [NSString stringWithUTF8String:“Welcome to WWDC!”];self.alertView.title = welcomeMessage;[self.alertView show];}

WelcomeMessage is dirty memory because the stringWelcome to WWDC!In the static data area, it copies a copy to the stack to give welcomeMessage.

- (void)displayWelcomeMessage {NSString *welcomeMessage = @”Welcome to WWDC!”;self.alertView.title = welcomeMessage;[self.alertView show];}

This welcomeMessage is a clean memory because one copy is not copied.

- (void)allocateSomeMemory {void *buf = malloc(10 * 1024 * 1024);…}

Although malloc initializes data on the stack, buf does not actually store the data, and all buckets are clean memory.

- (void)allocateSomeMemory {void *buf = malloc(10 * 1024 * 1024);for (unsigned int i = 0; i < sizeof(buf), i++) { buf[i] = (char)random();}…}

However, once the buf is used, the buf is dirty memory.

UIImage *wwdcLogo = [UIImage imageNamed:@”WWDC12Logo”];

Initialize UIImage. UIImage is actually the packaging of CGImage. CGImage generates jpeg and bitmap. There will be uncompressed bitmap data in the memory, which is dirty memory.

What happens when iOS runs in low memory?

At the beginning of iOS, there was a large proportion of clean memory. When we were running the app, initialization data generated dirty memory, which increased the proportion of dirty memory, when the proportion of clean memory is reduced, the memory pressure is generated and the memory is insufficient. At this time, the system will terminate the background application, release the dirty data of the application, and free up the memory space.

Memory warning this is a challenge that will occur on devices with limited memory. This is the final opportunity to protect the user experience. Ensure that your application can respond to the memory warning. The warning notification is triggered on the main thread, avoiding repeated initialization of big data is an opportunity to release the memory as much as possible, but there are several ways to pass the user experience memory warning to the application:
Notification
UIApplicationDidReceiveMemoryWarningNotification UIApplication proxy method -[id -applicationDidReceiveMemoryWarning:] UIViewController Method
-[UIViewController didReceiveMemoryWarning]

Be sure to pay attention to the dirty memory, because the dirty memory is created by the application. If it is not cleared, it can only be released after the application is terminated. Use the Profile VM Tracker to check the memory usage, avoid frequent fluctuations in a large range and reduce memory usage.@autoreleasepool.

Find out the memory problem and reduce memory usage: Clear the view level of our application and create only necessary views. Avoid heap growth caused by loops. Do not ignore small objects. Avoid memory growth:
Inaccessible, no pointer pointing to memory that cannot be used again
There is still a pointer to reference it, but the wasted cache has never been used
Referenced and waiting for use may never be used.

How to detect memory problems
The memory should not continue to grow during repeated operations, such as push and pop UIViewcontroller, sliding UITableview, and operating database search.

Tools and traps

Use Allocations Instrument to check whether the memory is leaked. In case of Memory leakage, you can check whether the self object is used in the block. Replace it with the self modified by _ weak without adding counting reference.
For more information, see the official manual.
Instruments Documentation
Instruments User Guide
Instruments User Reference
Address

Official Instructions on Virtual Memory

 

Related Article

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.