内存不会减少释放对象时 [英] Memory dont decreases When releasing objects
问题描述
(^。^)嗨再次抱歉我的英语不好,如果有人喜欢纠正我的修改,我会很感激
(^.^)"Hi again sorry for my English is not good if someone like correct my redaction I would appreciate this"
是的,你是对的。但是:
首先,当我点击创建按钮时,这会创建带有alloc的新视图控制器,并自动保留计数+1,当我按下kill按钮时,保留计数-1和等于0这意味着视图控制器创建从理论上说它是从内存中删除的我更正了代码所以:
Yes you're right. but: First when I click the create button this create new View Controller with alloc, and retain count +1 automatically and when I press kill button the retain count -1 and Equals 0 this means the View Controller created in theory it was removed form the memory I corrected the code so:
- (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.
}
* 但当我制作proyect的个人资料时,我点击按钮创建和终止内存不减少只增长*
抱歉但我不能粘贴图像,因为我是新手发布但是当我初始化Allocations init中的配置文件和Live Bytes 584,19kb以及1分钟的实时字节数为1,08 mb,不释放任何东西只会增长。
Sorry But I cant paste the image because i'm Newbie posting but when I init the profile in Allocations init with Live Bytes 584,19kb and in 1 minute Live Bytes are in 1,08 mb dont release nothing only grow.
我现在不知道为什么如果我正确创建和免费请帮助。
I don´t now why if I create and free correctly please Help.
推荐答案
您可以使用以下两种方式 -
1.分配一次并以dealloc发布 -
You can use following two way - 1. Allocating once and release in 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。每次分配并释放 -
2. Allocating every time and releasing as well-
- (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];
}
现在您可以尝试其中任何一项,然后检查内存占用量。
Now you can try with any of these and then check the memory footprint.
这篇关于内存不会减少释放对象时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!