内存不会减less释放对象时
(^。^)“嗨,再次抱歉我的英文不好,如果有人喜欢纠正我的编辑,我会感激这个”
你是对的。 但是:首先当我点击创buildbutton时,创build新的视图控制器与分配,并保留计数+1自动,当我按下杀死button保留计数-1和等于0这意味着理论上创build的视图控制器,它被删除forms内存我纠正了这样的代码:
- (IBAction)create:(id)sender{ if(vc == nil){ //if is not nil this mean vc have some space of memory reference and vc is not created //if == nil this mean vc does not have space of memory reference so create. vc = [[VC alloc] initWithNibName:@"VC" bundle:[NSBundle mainBundle]];// retain count + 1 [_VW addSubview:vc.view]; }
和
- (IBAction)kill:(id)sender{ [vc.view removeFromSuperview]; //When view removeFromSuperview is called also dealloc is called of the vc view [vc release];// retain count - 1 the curren count is equal 0 this mean vc does not have space of memory vc = nil; // remove the reference of memory. }
* 但是,当我做了专家的configuration文件,我点击button创build和杀死内存不要只减less增长*
对不起,但我不能粘贴图像,因为我是新手张贴,但是当我初始化分配初始化与生活字节584,19kbconfiguration文件和在1分钟生活字节在1,08 MB不要释放只有增长。
我现在不为什么如果我创build和释放正确,请帮助。
您可以使用以下两种方式 – 1.分配一次并释放dealloc –
- (IBAction)create:(id)sender{ if(vc == nil){ vc = [[VC alloc] initWithNibName:@"VC" bundle:[NSBundle mainBundle]]; [_VW addSubview:vc.view]; } } - (IBAction)kill:(id)sender{ [vc.view removeFromSuperview]; } - (void)dealloc { [vc release]; [super dealloc]; }
2.每次分配和释放 –
- (IBAction)create:(id)sender{ if(vc == nil){ vc = [[[VC alloc] initWithNibName:@"VC" bundle:[NSBundle mainBundle]] autorealease]; [_VW addSubview:vc.view]; } } - (IBAction)kill:(id)sender{ [vc.view removeFromSuperview]; }
现在您可以尝试使用其中任何一种,然后检查内存占用情况。