Objective-C advanced programming workshop memory management workshop learning notes

Source: Internet
Author: User
This log is used to record the problems encountered during the learning process.

"Objective-C advanced programming" is a great book written by the People's post and telecommunications Publishing House. Japanese authors are very careful about the details of objective-C. It is recommended that students of objective-C learn to buy it.

 

#1 How is the retaincount variable stored in the memory?

In gnustep (a cocoa framework interchange framework), retaincount is placed together with the object before the object address. Therefore, you can obtain the retaincount in gnustep as follows:

-(NSUInteger) retainCount{    return NSExtraRefCount(self) + 1;}inline NSUIntegerNSExtraRefCount(id anObject){    return ((struct obj_layout *) anObject )[-1].retained;}

However, Apple manages a hashtable and adds the reference technology retaincount and the address of the object memory block to the corresponding location through Hash (OBJ.

Although the implementation of gnustep may be more intuitive, Apple also has the following benefits:

  1. For object memory block allocation, you do not need to consider the header of the memory block.
  2. Each record in the reference counter table contains a memory block address, which can be traced back to the memory block of each object from each record. It is important in the debugging process. In addition, when a tool is used to monitor memory leakage, each record in the reference counter table also helps to monitor whether the owner of each object exists.

#2 autorelease & nsunloop?

This class uses the manager mode for pool objects, that is, it manages many pools and records the pools currently in use, if [[[class alloc] init] autorelease] is used elsewhere, the object will be added to the pool in use.

  1. The [pool drain] method discards the pool and performs a release on all objects in the pool;
  2. The newly defined pool is always the pool currently in use. Therefore, when multiple NSAID utoreleasepools are nested, the pool is always the innermost layer;
  3. The nsunloop represents the main loop of the program. An autoreleasepool is defined at the beginning of each nsunloop. When the runloop ends, the drain method of the pool is called. The specific nsunloop will be introduced in subsequent chapters.

 

#3 How to Improve the call speed of the objective-C method?

"IMP Caching" Method

Improve the speed of the autorelease method that is frequently called: Example

id autorelease_class = [NSAutoreleasePool class];SEL autorelease_sel = @selector(addObject:);IMP autorelease_imp = [autorelease_class methodForSelector:autorelease_sel];- (id) autorelease{    (* autorelease_imp)(autorelease_class,autorelease_sel,self);}- (id)autorelease{    [NSAutoreleasePool addObject:self];}

These methods are cached during framework initialization. The call speed of the first autorelease is twice that of the latter. Because oc-runtime has the overhead of sending messages between objects.

#4 what is the difference between SEL and imp?

typedef struct objc_selector *SEL;
typedef id (*IMP)(id, SEL, ...);

 

IMP is a function pointer. The directed function contains an object ID (Self pointer) for receiving messages, an optional sel (method name) for calling methods, and an indefinite number of method parameters, and return an ID. That is to say, IMP is the Execution Code of the final call of the message and the real implementation code of the method. We can use this function pointer like in C.

 

# What happens when 5 _ weak is assigned to _ strong?

  1. If the _ strong variable is nil, add 1 to the Reference Counter of the object referred to by the _ weak object;
  2. If the _ strong variable is an object, hash (OBJ) will call the internal release and Add 1 to the Reference Counter of the object referred to by _ weak;
  3. If both the _ strong variable and _ weak indicate an object, no operation is performed. (If it is retain after release? Then there will definitely be a problem. If the retaincount of the object referred to by exactly _ strong is 1, it will be verified)
  4. If _ weak refers to nil, it is equivalent to OBJ = nil; For the retaincount-1 that originally points to the object;

 

#6 _ unsafe_unretained why?

In fact, nil pointer (_ weak) is not automatically set, so a wild pointer may appear.

 

#7 @ autorelease Block

Call when the block ends. [pool drain];

 

#8 C ++ smart pointer problem STD: shared_ptr & STD: weak_ptr & STD: auto_ptr

Introduces the concept of referencing counter.

 

#9 What is nszone?

When encountering allocwithzone, copywithzone always has a question: What is this nszone? To put it simply, you can think of a memory pool, alloc or dealloc. These operations are all performed in this memory pool. Cocoa always configures a default nszone. Any Default memory operation is performed on this "zone. The default nszone defect is that it is global and takes a long time, which will inevitably lead to memory fragmentation. If you need a large number of alloc objects, then the performance will be affected. All cocoa methods are provided. You can generate an nszone by yourself and limit all alloc and copy methods to this zone.

 

#10 what should I pay attention to when deleting dynamic arrays?

All elements must be set to nil. Otherwise, memory leakage may occur. Even if memset and other functions are used to fill the memory with 0, the assigned objects are not released. For the compiler, you must explicitly use the source code assigned to the variable with _ strong modifier.

for( NSUInteger i = 0; i < entries ;++i )    array[i] = nil ;free(array);

 

# 11_weak table

Under the arc condition, when the _ weak points to the object is released, _ weak will be automatically set to nil. How can this be done in runtime?

Maintain a weak table in the memory.

/* OC Code */{ID _ weak obj1 = OBJ;}/* compiler simulation code */ID obj1; objc_initweak (& obj1, OBJ); objc_destroyweak (& obj1 );

As in the preceding code, the key is the object address OBJ, and the values is the _ weak address & obj1. An object address can correspond to multiple _ weak variable addresses. when an object is destructed, The _ weak variable pointed to in the weak table will be set to nil. Delete the record in the weak table.

Because the use of the _ weak variable will cause the above system overhead, you only need to use the _ weak modifier to avoid loop references.

#12 how do I obtain retaincount under Arc conditions?

Use: _ objc_rootretaincount (OBJ );

Objective-C advanced programming workshop memory management workshop learning 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.