[Study note-OBJECTIVE-C] "OBJECTIVE-C Programming 6th Edition" chapter 17th memory management and automatic counting

Source: Internet
Author: User

The 16th chapter of the book on the operation of the file and directory, the content is relatively simple, again skipped.
The 17th chapter of the study notes by myself summarized and presented to you.

Memory Management:
-Use memory management to clean up unused memory in order for the memory to be used again. If an object is no longer in use, you need to free the memory that the object occupies

Part 1. Basic memory management Model part 1.1 automatic garbage collection:
    • The system can automatically Jatze if the object has other objects, and the object that is no longer referenced will be automatically freed when the program executes the required space.
Part 1.2 manually manages memory counts:
    • The reference number +1 is required whenever a reference to an object is created.[myFraction retain]
    • When an object is no longer needed, send the release message to the object, which is the number of references-1.[myFraction release]
    • When the object's reference count is 0, the system knows that the object is no longer needed.
Part 1.3 Automatic reference count (ARC):
    • The system detects when the object needs to be persisted, when the object needs to be freed automatically, and when the object is disposed.
Part 2. Autoreleasepool Block
    • The auto-free pool does not include the actual object, just a reference to the object, which is freed when the auto-free pool is cleaned up.
    • Add an object to the list of objects that are maintained by the auto-release pool:[result autorelease]
    • Emptying the auto-free pool: Adding messages to the auto-release pool drain
    • When the end of the block is executed autorelease , the system frees the pool.
    • Objects created by the Alloc,copy,mutable,new method are not automatically freed. You need to be responsible for freeing the memory of these objects after you have finished using them. Proactively send messages to these objects release , or send messages to objects to join the object autorelease to the auto-free pool. ( Active Release Object )
    • method returns the object: Place the object in the auto-free pool: return [result autorelease] orFraction *result = [[[Fraction alloc] init ] autorelease]

      • Manage each temporary object created by the For loop iteration:
for0; i < n; ++i){  @autoreleasepool{    ...临时对象...  }}
Part 3. Time Loops and memory allocations
    • To handle the new event, a new auto-release pool is created.
    • Call some methods in the app to handle this event, and after the method returns, the system cleans up the auto-free pool.
    • The automatically freed objects that are created are destroyed unless they are used to retain survive the process of emptying the pool automatically.
Part 4. Strong variables and weak variables

Strong variables:

    • Pointers to all objects are strongly variable
    • References to old objects are freed before they are assigned.
    • Declaring objects:__strong Fraction *f1
    • Declaring attributes:@property(strong, nonatomic) NSMutableArray *birdNames;

Weak variables

    • The system will track the reference to this variable. When the referenced object is disposed, the variable is set to nil .
    • Declaring objects:__week UIView *parentView;
    • Declaring attributes:@property(week, nonatomic) UIView *parentView;

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Study note-OBJECTIVE-C] "OBJECTIVE-C Programming 6th Edition" chapter 17th memory management and automatic counting

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.