Wwdc-ios memory Performance and principle notes

Source: Internet
Author: User

Fundamentals of iOS Memory How is memory initialized and managed?
    • In iOS, the pointer address range is very large, 32-bit CPU on the 4GB size, 64-bit CPU on the 18EB size (about 2 of 60), such a large pointer address range will cause us to think that the system memory is so large, in fact, physical memory may not be so large, this size is called virtual memory, In OS X, the system uses a hard disk to store infrequently used data in memory as a backing store for memory, with a pointer address to the hard disk data in memory, which is required to be written to memory afterwards.

    • However, there is no backing store in iOS, the read-only type of data in iOS already exists on the hard disk, when needed to write memory use, can read and write data is resident memory, will not be removed, once the memory can be used to reach the threshold, the system will be issued with insufficient memory warning, the application actively release resources, If the release fails or does not exclude the warning, the program is terminated directly by the system.

    • Virtual memory
      The physical memory of iOS is divided into 4KB pages, and not all pages can be accessed by the app. Virtual memory is a layer between the kernel and the application layer, because when we need memory every time we call kernel to request the memory of the process is very expensive, so the underlying will directly with the virtual memory request space, virtual memory will communicate with kernel, create VMS object to match the physical memory:

      In the heap of memory, we apply only a portion of the data, and other frameworks created by the object and cache, there are some static constants in memory, thread stack, picture data, Calayer cache, Database Cache

Memory type: Clean memory and dirty memory

Clean Memory: A space that is copied from disk to memory, such as code, framework, and memory-mapped files.
Dirty Memory: Other memory space. For example, the initialization data on the heap, the database cache, the extracted image data and so on.
most of the data initialized by the application is dirty memory
Examples are as follows:

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

Welcomemessage is dirty memory because the string is Welcome to WWDC! in the static data area, it doubles as a copy to the heap on the welcomemessage.

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

This time welcomemessage is clean memory, because there is no copy.

- (void)allocateSomeMemory {voidmalloc(1010241024);…}

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

- (void)allocateSomeMemory {voidmalloc(1010241024);for (unsignedint0sizeof(buf), i++) { buf[i] = (char)random();}…}

But once buf has been used, BUF is dirty memory.

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

Initializing the uiimage,uiimage is actually cgimage wrapper, Cgimage generates JPEG and bitmap, there will be uncompressed bitmap data in memory, is dirty memory.

What happens when iOS is running low on memory

iOS started with a large proportion of clean memory, when we run the app, as the run, the initialization of data, resulting in dirty memory, the proportion of dirty memory is increased, clean memory ratio is reduced, the final memory pressure, memory is not enough, this time the system will end the background application, Frees the dirty data that belongs to the app and frees up memory space.

Memory warning This is a challenge.
    • Will occur on devices with limited memory
    • Is the last chance to protect the user experience
    • Ensure your app responds to memory warnings, warning notifications are triggered on the main thread, avoiding repetitive initialization of big data
It's a chance.
    • Free up memory as much as possible, but do not affect the user experience
    • Memory warnings are delivered to the application in several ways:
      1. Notice
        Uiapplicationdidreceivememorywarningnotification
      2. UIApplication Proxy method-[id <UIApplicationDelegate> -applicationDidReceiveMemoryWarning:]
      3. Uiviewcontroller method
        -[UIViewController didReceiveMemoryWarning]

Be sure to pay attention to dirty memory, because dirty memory is created by the application, if not cleared, only after the application is terminated to release, using the profile of the VM Tracker to detect memory usage, avoid a wide range of frequent @autoreleasepool fluctuations, reduce memory can also be used .

Problem with memory found
    • Reduce the use of memory: Clear the view level we apply, creating only the necessary views. Avoid loops that cause heap growth, and don't ignore those small objects.
    • Avoid memory growth:
      • Inaccessible, without any pointers pointing to the
      • Cannot be used again
    • Discarded memory
      • There are still pointers to reference it, but the wasted
      • Never been used.
    • Cache
      • Referenced and waiting to be used
      • May never be used.

how to detect memory problems
Memory should not continue to grow in one operation, such as: Push and pop Uiviewcontroller, swipe uitableview, manipulate database search

Using tools and Traps

Using allocations instrument to detect if memory is compromised, a memory leak can be used to check if the Self object is used in the block and should be replaced with a __weak decorated self so that no count references are added.
More information can be found in the Official handbook.
Instruments Documentation
Instruments User Guide
Instruments User Reference
Address

Official description of virtual memory

Video: WWDC2012 Application performance: Memory

If there is an incorrect, welcome criticism!

Wwdc-ios memory Performance and principle notes

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.