1.6 Memory Management

Source: Internet
Author: User

I. Introduction to memory Management

1. Why should I have memory management?

Mobile device memory is extremely limited, and each app can occupy a limited amount of memory.

The following behavior will consume memory

. Create an OC Object

. Define a variable

. Call a function or method

When the app takes up a lot of memory, the system issues a memory warning, and you need to reclaim some memory that is no longer in use.

If the app occupies too much memory, the system may force the app to shut down, causing a flashback and impacting the user experience

2. What is memory management

1> so-called memory management, is the memory management, involves the operation is:

Allocating memory: Creating an object, for example, increases memory consumption

. Clean up Memory: for example, destroying an object can reduce memory consumption

2> Management scope of memory management

. Any object that inherits the NSObject

Invalid for other non-object types (base data type/struct, etc.)

3> The essential reason why memory management is only required for OC objects

. OC objects stored inside the heap

Non-OC objects are generally placed inside the stack (stack memory is automatically reclaimed by the system)

3. Heap and Stack

1> stack (operating system): automatically allocated by the operating system release, storage function parameter value, local variable value, etc., its operation method is similar to the data structure in the stack (advanced post-out);

2> Heap (operating system): Usually released by the programmer, if the programmer does not release, the end of the program may have an OS recycle, distribution is similar to the list.

3> Example

  
 
  1. int main(int argc, const char * argv[])
  2. {
  3.    @autoreleasepool {
  4.        int a = 10; // 栈
  5.        int b = 20; // 栈
  6.        // p : 栈
  7.        // Person对象(计数器==1) : 堆
  8.        Person *p = [[Person alloc] init];
  9.    }
  10.    // 经过上一行代码后, 栈里面的变量a\b\c都会被回收
  11.    // 但是堆里面的Person对象还会留在内存中,因为它是计数器依然是1
  12.    return 0;
  13. }
Two. Reference counters

1. To manage memory consumed by objects, a counter is introduced to manage memory

Common actions for referencing counters

To send an retain message to an object, you can make the reference counter value +1 (The return object of the Retain method itself)

To send a release message to an object, you can reference the counter-1

The current reference counter value can be obtained by sending an Retaincount message to the object.

It is important to note that release does not represent destroying/reclaiming objects, only counters-1

2. When the reference counter of an object is 0 o'clock, then the object is destroyed, the memory occupied by the system is reclaimed, but when the object is destroyed, the system will automatically send an DEALLOC message to the object, so we can determine whether the object is destroyed according to the Dealloc method has not been called

 
   
  
  1. - (void)dealloc
  2. {
  3.    [super dealloc]; // 注:重写dealloc方法时,[super dealloc]一定要写在大括号中最下面
  4. }

3. Zombie object/Wild pointer/null pointer

1> Zombie Objects: Objects that have been destroyed (can no longer be used)

2>: Pointer to a Zombie object (memory address not available), sending a message to the wild pointer will report exc_bad_access error

3> NULL pointer: There is no pointer to the storage space (nil = = 0), and sending a message to the null pointer has no reaction

Three. Manual memory Management (MARC)

1. If you turn off automatic memory management arc

    • To manually invoke methods such as retain/release, you must turn off the automatic Memory management arc feature

    • By default, Xcode does not control zombie objects, and using a piece of freed memory does not give an error. For easy commissioning, zombie object monitoring should be turned on


2. Memory Management Principles

Apple's official policy on memory management

1> who created who release:

If you create an object through Alloc,new,copy, or mutablecopy , you must call release or Autorelease

2> who retain who release:

Once you call retain, you must call the release


A summary is

Plus, there's a minus.

once let the object counter +1, it must be at the end to let the object counter -1

Memory Management One:

  
 
    1. int main()
    2. {

  1.          Person *p = [[Person alloc] init];
  2.          Student *stu = [[Student alloc] init];
  3.          [stu release];
  4.          [p release];  

    1. }
Memory Management II:
  
 
  1. // 一个对象中拥有其他对象@property未用retain修饰时,在其对象销毁时,需要在系统调用dealloc方法时手动给其他对象release
  2. // Person.h文件
  3. #import <Foundation/Foundation.h>
  4. @class Car;
  5. @interface Person : NSObject
  6. // 人拥有车
  7. @property Car *car;
  8. @end
  9. // Person.m文件
  10. #import "Person.h"
  11. #import "Car.h"
  12. @implementation Person
  13. - (void)setCar:(Car *)car
  14. {
  15.    if (_car != car) {
  16.        [_car release];
  17.        _car = [car retain];
  18.    }
  19. }
  20. - (void)dealloc
  21. {
  22.    self.car = nil; // 当Person被销毁时,将其所拥有的Car对象release
  23.    [super dealloc];
  24. }
  25. @end




From for notes (Wiz)

1.6 Memory Management

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.