iOS Interview - Memory Management Answer

Storage operating system divided into two categories: 1. The set of instructions stored in the data storage 2.
Since the main set of instructions stored by the hardware registers to help me complete, most efficient; the number of poly store our primary concern in the development;

iOS system from low to high data storage divided into five categories: code region, constant region, global / static variable region (region initialized period for initializing section area), heap, stack area

Because in addition to the heap, the other data storage areas are distributed operating systems and recycling, our primary concern in the development process is a heap of storage problems:

Heap area management reference count manager is: oc every object has a reference count their own mark, when a new pointer to it, its reference count by one, when a pointer to it is set Nill, it Save a reference count; when it is referred to as 0, the system will be destroyed;

Reference count management idea is: who created, who is released.

Reference count manager is divided into two cases:
the MRC manual management: explicitly in the program executes the following instructions:
When we execute an object corresponding to alloc / new / copy / mutilCopy / on holding the object reference, or perform retain , the reference count of the object is increased by one, as we'll perform a Save Release when one pair, the reference count of the object;
the ARC automatic management mode: the program at compile time, the compiler will be added to retain in place / release ;

The introduction of the reference count management issues:

  1. Memory leak: When two objects held for each other, the two sides can not reference count is 0, then they will never be destroyed by the operating system, stay consistent in memory, resulting in a memory leak.

Reference count management and java in the gc (garbage collection mechanism What is the difference):
gc advantages:

  1. Any pattern can be recovered without being held by the object, comprising a circular reference portion;
  2. gc running in the background, can be periodically garbage inspection; (indefinite periods of time, may be detected insufficient memory or a fixed period of time after the detection operation)
    gc disadvantages:
  3. Since the gc program run at a later stage, you need to consume a certain cpu, memory resources, but also have an impact on power consumption, it runs, it should be the highest priority, some threads may hang in the app, slightly affect performance ;
  4. Because the object is detected, the release in the background, the timing of the release can not be determined;

Reference count advantages:

  1. Real-time object is not referenced in a timely manner of the release;
  2. No running in the background without taking up cpu, memory resources, will not affect app performance;

Reference count Disadvantages:

  1. We can not deal with the problem cache references;

Common memory problems:

  1. Memory Leak

Memory leak reason: refers to the objects dynamically allocated, not caused by Cao Tong recycling system after use is completed. It causes the object to stay consistent in memory, and other code can not access this phenomenon as a memory leak.

  1. Wild pointer
  1. 2. The value pointer points to an address not assigned by the operating system of the target early release, not a pointer variable is set to nil after release; when then use the pointer to a memory address trash, save EXC_BAD_ACCESS
  1. Null pointer

Refers not point to any content pointer. Generally when the object is destroyed, the pointer variable assignment to avoid 0, 0 for a null pointer, and does not complain, just do not have it.

  1. Circular references

Holding each other to each other, the operating system can not release them according to the rules. Solutions are not strong party objects to hold or run out timely destruction.

  1. Other memory problems
    after 5.1 NSNotification addObserver, remember to add remove the dealloc inside.
    5.2 animation repeat count is infinite, and does not take the initiative to stop the animation, the basic equivalent to an infinite loop.
    5.3 forwardingTargetForSelector return to the self.

Guess you like

Origin blog.csdn.net/weixin_34279061/article/details/90922046