iOS Review Note 4: Memory management

Source: Internet
Author: User

A basic principle
1 Why memory management is required
Because mobile devices have limited memory, each app occupies a limited amount of space.
When the app takes up too much memory space, the system issues a memory warning, which reclaims some unused memory.
For example, class objects and instances that are no longer used.


2 Managing Objects
Any object that inherits from the NSObject class


3 Memory Area
Heap: Actively allocating space, requiring management
Stacks: Local variables, automatic management
For example:
int a = 1;
person* p = [[Person alloc] init];
A,p on the stack.
The person allocates space in the heap area


Two reference counts
1 definitions
Each OC object has a reference count, which is a 4-byte integer representing the "number of times referred to".
You can use the Retaincount method to view a reference count for an object.


2 effects
1 When an object is created (alloc/new/copy);
The reference count becomes 0 o'clock and is reclaimed by the system.
The reference count does not change to 0 o'clock, and the memory it consumes cannot be recycled unless the entire program exits.


3 Reference count Operations
Retain+1
Release -1


4 Object Destruction
An object reference count is 0 o'clock, and the memory it consumes is destroyed by the system.
When destroyed, the system sends an DEALLOC message to the object.
Therefore, it is generally necessary to override the Dealloc method, first calling the parent class's dealloc, and then releasing its own resources.
After the object is destroyed, its memory is recycled and continued use causes the program to crash (the wild pointer).
*dealloc cannot be called manually


Example
@interface Person@property int m_nage; @end @implementation person-(void) Dealloc{nslog (@ "Dealloc");//Verify that the object is recycled [super Dealloc];} @end #import <foundation/foundation.h>int Main () {//default = 1person* p = [[person] init]; Nsuinteger count = [P retaincount]; NSLog (@ "Retaincount = =%ld", count);//+1, to 2,retain the return value is the object itself [P retain];//-1, becomes 1[p release];//-1, becomes 0, is recycled, the p becomes a wild pointer after being recycled, The object being pointed to becomes a zombie object//Cannot send any message to the zombie object, including Retain[p release];//error: exc_bad_access//p.m_nage = 10//To prevent wild pointer errors, because sending a message to the nil object does not give error, only warning p = nil;//turns on zombie check before error: Setxxx,message sent to deallocated instancep.m_nage = 10return 0;}


Using the object being reclaimed error: exc_bad_access, access to a piece of reclaimed memory, that is, the wild pointer errors


Three summary
1 Basic methods
Retain
Release
Retaincount
Dealloc


2 Concepts
Zombie Objects: Objects that are recycled within the occupied:
Wild hands: pointing to Zombie objects
Null pointer: No point to any address, storage address is 0/null/nil

iOS Review Note 4: 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.