这是释放记忆的好方法吗? [英] Is this a good way to release memory?

查看:125
本文介绍了这是释放记忆的好方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在iPhone上进行了一段时间的编程,并且在内存管理方面遇到了不好的经历。我想知道以下方式是否是释放内存的好方法。

I have been programming on iPhone for quite sometime and have had bad experiences with memory management. I was wondering if the following way is a good way to release memory.

int count = [someObject retainCount];

for (int i = 0; i < count; i ++) 
{
[someObject release];
}

这种方法在少数情况下是一种绝望的行为(特别是 UIWebViews )我曾面对过。变量的retainCount减少到零,这将释放它使用的内存。该方法有点脏,但是它有任何瓶颈吗?

This method was an act of desperation in a few situations (particularly UIWebViews) I had faced. The variable's retainCount is reduced to zero which will release the memory being used by it. The method is a little dirty but are there any bottlenecks associated with it?

推荐答案

你不应该依赖 retainCount 因为有可能通过 iOS 框架保留对象,

You should not rely on the retainCount because there are possibility of retaining the object by iOS frameworks,

请阅读Apple 关于 retainCount 的说法。


重要提示:此方法通常为
调试内存
管理问题无价值。因为任何数量
的框架对象可能保留了
一个对象,以便保存对它的引用
,同时
自动释放池可能持有任何

对象的延期发布数量,你不太可能从
方法中获得有用的信息。

Important: This method is typically of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.

要了解您必须遵守的内存管理的基本规则,请阅读内存管理规则。要诊断内存管理问题,请使用合适的工具:

To understand the fundamental rules of memory management that you must abide by, read "Memory Management Rules". To diagnose memory management problems, use a suitable tool:

这篇关于这是释放记忆的好方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆