iPhone:我在迭代建立内存方面遇到了麻烦

时间:2022-06-05 22:26:54

I am having trouble with memory building up and am not able to empty it once I am done with it. When I look at the diagnostic tool ": Allocations: Instruments: Object summary: statistics", the memory is just continuously building up.

我在内存构建方面遇到了麻烦,一旦完成内存就无法清空内存。当我查看诊断工具“:Allocations:Instruments:Object summary:statistics”时,内存正在不断积累。

example:

for (int i=0; i<100000; i++){
    UILabel *lblPost = [[UILabel alloc] initWithFrame:CGRectMake(x,y,w,d)];
    [lblPost setText: "Hello World"];
    [self.view addSubview: lblPost];

    // tried each of the following
    //[lblPost dealloc]; //neither work to clear memory it just builds
    //[lblPost release]; //
}

--> Do I need to seperate CGRect out and clear that.

- >我是否需要分离CGRect并清除它。

--> (I know I can just keep writing to one label, this is a simplified version where in the bigger version, one label would not work so easily. )

- >(我知道我可以继续写一个标签,这是一个简化版本,在较大的版本中,一个标签不会那么容易。)

--> (I find it hard to believe that I can not create an object and then destroy it 10000 or 100000000 times over. In standard C, I can accomplish this with memory-blocks by using "free()" )

- >(我发现很难相信我无法创建一个对象,然后将其销毁10000或100000000倍。在标准C中,我可以通过使用“free()”来完成内存块

5 个解决方案

#1


0  

This is fun! I have decided to jump in. I posted this in my comments but here it is again:

这个很有趣!我决定跳进来。我在评论中发布了这个,但在这里又是:

I think madhu misunderstood the [lblPost release]. This "release" only applies to the lblPost instance. Not the ones retained by self.view... etc. So you still have 10000 label retained by self.view...

我认为madhu误解了[lblPost发布]。此“发布”仅适用于lblPost实例。不是self.view保留的那些...等等所以你仍然有自己的10000标签保留...

So, you create 10000 instances of lblPost and then release all of them (10000) by this line [lblPost release] in your for loop. That is just fine. But then in your for loop you also have this line [self.view addSubview: lblPost]. This line will add 10000 instances to your self.view. And they are the reason why your system crashed.

因此,您创建10000个lblPost实例,然后在for循环中通过此行[lblPost release]释放所有这些实例(10000)。那很好。但是在你的for循环中你也有这一行[self.view addSubview:lblPost]。此行将为您的self.view添加10000个实例。它们是您的系统崩溃的原因。

#2


3  

The view you are adding your label to is retaining it, that's why each none of the labels is deallocated (even if you send release message)

您要添加标签的视图是保留它,这就是为什么没有任何标签被解除分配的原因(即使您发送了发布消息)

#3


2  

Maybe i really don't understand what you're trying to do, but your each indiviual object you'r allocating is retained in the view. Let me try to explain it in the code:

也许我真的不明白你想要做什么,但你分配的每个单独对象都保留在视图中。让我试着在代码中解释它:

 for (int i=0; i<10000; i++){
  UILabel *lblPost = [[UILabel alloc] initWithFrame:CGRectMake(x,y,w,d)];
 // lblPost now has a retain count of 1, as you alloc'd it, you'll have to release it!
  [lblPost setText: "Hello World"];
  [self.view addSubview: lblPost];
 // lblPost now has a retain count of 2, as adding to the view adds a reference to it
  [lblPost release]
 // you alloc'd it, now you should release it. it now has a retain count of 1, which means it's in the ownerhsip of the self.view
}

Now, when you release or free self.view, the lblPost objects should be released as well

现在,当您释放或释放self.view时,也应该释放lblPost对象

#4


1  

Why are you allocating memory for 10000 UILabels and adding them as a subview exactly? That's iPhone torture. The items in bold cause your surge in memory. Plus you're releasing none of them.

为什么要为10000 UILabels分配内存并将它们准确地添加为子视图?那是iPhone的折磨。粗体项目会导致内存激增。另外你没有发布任何一个。

Also - never ever ever call dealloc yourself - dealloc is called automatically when you release something.

另外 - 永远不要自己调用dealloc - 释放内容时会自动调用dealloc。

iPhone:我在迭代建立内存方面遇到了麻烦

#5


0  

for (int i=0; i<10000; i++){
    UILabel *lblPost = [[UILabel alloc] initWithFrame:CGRectMake(x,y,w,d)];
    [lblPost setText: "Hello World"];
    [self.view addSubview: lblPost];

    [lblPost release];
    //You should release after adding it to your view


}

#1


0  

This is fun! I have decided to jump in. I posted this in my comments but here it is again:

这个很有趣!我决定跳进来。我在评论中发布了这个,但在这里又是:

I think madhu misunderstood the [lblPost release]. This "release" only applies to the lblPost instance. Not the ones retained by self.view... etc. So you still have 10000 label retained by self.view...

我认为madhu误解了[lblPost发布]。此“发布”仅适用于lblPost实例。不是self.view保留的那些...等等所以你仍然有自己的10000标签保留...

So, you create 10000 instances of lblPost and then release all of them (10000) by this line [lblPost release] in your for loop. That is just fine. But then in your for loop you also have this line [self.view addSubview: lblPost]. This line will add 10000 instances to your self.view. And they are the reason why your system crashed.

因此,您创建10000个lblPost实例,然后在for循环中通过此行[lblPost release]释放所有这些实例(10000)。那很好。但是在你的for循环中你也有这一行[self.view addSubview:lblPost]。此行将为您的self.view添加10000个实例。它们是您的系统崩溃的原因。

#2


3  

The view you are adding your label to is retaining it, that's why each none of the labels is deallocated (even if you send release message)

您要添加标签的视图是保留它,这就是为什么没有任何标签被解除分配的原因(即使您发送了发布消息)

#3


2  

Maybe i really don't understand what you're trying to do, but your each indiviual object you'r allocating is retained in the view. Let me try to explain it in the code:

也许我真的不明白你想要做什么,但你分配的每个单独对象都保留在视图中。让我试着在代码中解释它:

 for (int i=0; i<10000; i++){
  UILabel *lblPost = [[UILabel alloc] initWithFrame:CGRectMake(x,y,w,d)];
 // lblPost now has a retain count of 1, as you alloc'd it, you'll have to release it!
  [lblPost setText: "Hello World"];
  [self.view addSubview: lblPost];
 // lblPost now has a retain count of 2, as adding to the view adds a reference to it
  [lblPost release]
 // you alloc'd it, now you should release it. it now has a retain count of 1, which means it's in the ownerhsip of the self.view
}

Now, when you release or free self.view, the lblPost objects should be released as well

现在,当您释放或释放self.view时,也应该释放lblPost对象

#4


1  

Why are you allocating memory for 10000 UILabels and adding them as a subview exactly? That's iPhone torture. The items in bold cause your surge in memory. Plus you're releasing none of them.

为什么要为10000 UILabels分配内存并将它们准确地添加为子视图?那是iPhone的折磨。粗体项目会导致内存激增。另外你没有发布任何一个。

Also - never ever ever call dealloc yourself - dealloc is called automatically when you release something.

另外 - 永远不要自己调用dealloc - 释放内容时会自动调用dealloc。

iPhone:我在迭代建立内存方面遇到了麻烦

#5


0  

for (int i=0; i<10000; i++){
    UILabel *lblPost = [[UILabel alloc] initWithFrame:CGRectMake(x,y,w,d)];
    [lblPost setText: "Hello World"];
    [self.view addSubview: lblPost];

    [lblPost release];
    //You should release after adding it to your view


}