VIEWCONTROLLER.M//  OC17 memory management and automatic reference counting//// created by zoujie ON 15/10/25.//  COPYRIGHT ? 2015 year zoujie. all rights reserved.//# import "ViewController.h" #import "Fraction.h" @interface ViewController () @ end@implementation viewcontroller- (void) viewdidload { [super Viewdidload]; #pragma mark basic knowledge of memory management/application not enough memory to sit more things, there is a possibility of collapse. Memory management is concerned with cleaning up (recycling) unused memory so that memory can be reused. 1. Frees memory// 2 that are not used by objects. Determines that the object does not need to use// 3. Reference count (manual, used to support legacy non-ARC engineering) [self managermemorybymyself];// 4. Xcode4.2 after ARC Auto reference count [self autoARC]; }-(void) managermemorybymyself{// when an object is created, the initial reference count is 1// whenever creating a reference to an object needs to be a reference object +1 , send RetaiN Message id obj,obj1;// [obj retain];// &NBSP by sending the release message to an object when the object is not needed; 1// [obj release]; for reference counting // when the reference count is 0, the system knows that the object is not required to use // The Add method adds a reference count [obj addObject:obj1]; [obj Addsubview:obj1]; // remove will reduce the object reference count [obj removeObjectAtIndex:0]; [obj removeFromSuperview]; // NSAutoreleasePool Auto-release pools can help track objects that need to be released for some time to delay, By sending an drain message to the auto-release pool, the auto-free pool is cleaned up and the object is freed// [obj1 autorelease]; Fraction *frac1 = [[Fraction alloc]init]; Fraction *frac2 = [[fraction alloc]init]; // [frac1 release];// [frac2 release]; }-( void) autoarc{}- (void) didreceivememorywarning { [super Didreceivememorywarning]; // dispose of any resources that can be recreated.} @end
OC17 memory management and automatic reference counting