IOS memory management-personal understanding

Source: Internet
Author: User
IOS memory management: IOS has no garbage collection mechanism (Mac OS) for different Mac OS and iOS, mainly because mobile devices have limited resources? Or is his exclusive framework cocoa not supported? Or the two are listed. Therefore, in IOS programming, memory management is very important-reference the counter (RC). If you do not pay attention to it, there may be a memory leak or bad access (access to a destroyed object address) error.
  1. The basic principle of Memory Management: You can only cancel (release) or release (autorelease) the objects you own, according to the naming rules of cocoa, you generally have the ownership of a when creating an object A by using the method named alloc, copy *, New * (xxx * Indicates containing XXX. Or use retain to obtain the ownership of an object.
  2. Use release and autorelease to release the ownership of an object. Release means immediate release, and the Object Reference Counter-= 1 (note that using the object xxxcount to view the object reference count may cause misunderstanding. According to XXX, when the reference count of an object is = 0, the delloc object is called and the object is destroyed, however, when you call xxxcount of an object to view the number of object references, it is usually greater than your expected value 0. You do not have to worry about this because you do not know the relationship between classes in this framework, you do not need to know. All you need to do is ensure that get (get ownership) in your code)
    --> Release/autorelease ownership. In iOS, you are just a passer-by, not a master. You have to check out your reservation (not accurate enough? Indeed, the reservation has no ownership.
  3. The second point above only refers to release. Here we will talk about autorelease, auto... (autorelease, the same as below) means 'release will send release message'. In fact, it is to put the object into the corresponding Auto Release pool (proximity principle ?), When will it be released? This is related to autoreleasepool (automatic release pool). Before the release/drain pool, a release message is sent to each object in the pool. The automatic release pool has its own scope. Well, let's talk about the places where autorelease is used most...
    -(Void) creatobject {nsstring * s = [[nsstring alloc] init] // create a String object s return [s autorelease] // You must develop a good habit, today, when you create an object in the // return method (please --), put it into the Auto Release pool before returning it}

  4. Autoreleasepool. Cocoa applications always expect an available Auto Release pool. If the Auto Release pool is unavailable or does not exist, the automatically released object cannot be released, memory leakage (leak ).
    1. The automatic release pool can be nested. What is nesting? Check the Code:
      -(Void) run {ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init] // pseudo code, meaning meeting... // some code here nyothoreleasepool * innerpool = [[nyothoreleasepool alloc] init] // nested automatic release pool... // some code here [innerpool release] [pool release]}

      In fact, in the stack where the cocoa application runs, but you create an automatic release pool, it will be added to the top of the stack (along with the method), so to prevent memory leakage, there should also be an automatic release pool at the bottom of the stack. No. There is no such thing in the IOS main method .... The scope of the automatic release pool is
      Init-> release (in the same Upper and Lower environments, you can simply think of it in the same method), nested automatic release pool, usually used to create a large number of temporary objects.

      -(void)run{      for (int i= 0,i <99999,i++){       NSAutoReleasePool *innerpool= [[NSAutoReleasePool alloc] init]        NSString *s = [[NSString alloc] init]        ... //some code here      [s autorelease]       [innerpool release]}}

  5. Thread and pool. Each thread has its own thread stack.
  6. Exception and pool. parse the fourth red font.
  7. On the question of 'chicken eggs, egg bean', you just need to confirm that the chicken can have eggs.
  8. (To be continued)


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.