iPhone开发 - 内存管理的经验教训 [英] iPhone Development - Lessons in memory management

查看:92
本文介绍了iPhone开发 - 内存管理的经验教训的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要内存管理课程。我有一个使用多个视图(大约10个)的应用程序,其中一些附加到选项卡控制器。问题是我正在使用图像(我从Web服务加载的许多图像)。我面临以下问题。

I need lessons in memory management. I have an application that uses multiple views (around 10), some of these are attached to tab controller. Problem is that I'm using images (many images that I load from a web service). I'm facing the following issues.


  1. 当我在表格视图中滚动时内存不断增加(为什么?) - I检查Apple网站上的CustomTableViewCell应用程序,当我使用Instruments运行时,它显示相同的符号。

  1. The memory keeps increasing when I scroll in the table view (why?) - I checked the CustomTableViewCell application from Apple's site, and it's showing the same signs when I run it with Instruments.

我正在使用带有许多对象的autorelease,但是我看到这些对象实际上没有被释放并且内存已连线。如何摆脱这些对象?

I'm using autorelease with many objects, but I see that these objects don't actually get released and the memory is wired. How can I get rid of these objects?

如何告诉NSAutoreleasePool定期释放未使用的对象?我认为这可以帮助我摆脱有线内存。但我可以这样做吗?

How can I tell the NSAutoreleasePool to periodically release the objects that are not being used? I think this can help me get rid of wired memory. But can I do that?

是否有Apple或其他人(书籍或在线文章)解释如何使用Instruments(稍微详细的例子?)并微调应用程序的内存和性能?

Is there any example by Apple or someone else (book or online articles) explaining how to use Instruments (in a little detail with example?) and to fine tune the application for memory and performance?

谢谢。

推荐答案

现在我们已经对自动释放的只说拒绝答案,我想我会更加有效地添加如何使用自动释放的提示。无论好坏,不是每个人都会完全避免自动释放 - 如果没有其他原因,因为Apple提供了许多方便的方法来交给你自动释放的对象。

Now that we have the "just say no" answers to autorelease out of the way, I thought I'd add a tip on how to use autorelease more effectively. For better or worse not everyone's going to completely avoid autorelease-- if for no other reason than because Apple provides so many convenience methods that hand you autoreleased objects.

你可以'告诉autorelease池释放你没有使用的任何对象。没有垃圾收集,它怎么会知道呢?

You can't just tell the autorelease pool to free up any objects that you're not using. There's no garbage collection, and how else is it going to know?

你可以做的是创建一个本地自动释放池,然后当你不再需要它时释放它本地自动释放的对象。如果你有一个你正在创建自动释放对象的块,你将确保通过在块的开头创建一个本地自动释放池来释放它们(只需分配/初始化它,不需要魔法),然后释放池。块结束。而且,池中的对象也会被释放。

What you CAN do is to create a local autorelease pool and then release that when you no longer need the local autoreleased objects. If you have a block where you're creating autoreleased objects, you'll ensure they get freed by creating a local autorelease pool at the start of the block (just alloc/init it, no magic required) and then releasing the pool at the end of the block. And voila, and objects in the pool are also released.

自动释放池嵌套,所以如果你这样做,请记住这一点。如果您发布自动释放池,请确保它是最近分配的池而不是其他池。

Autorelease pools nest, so keep that in mind if you do this. If you release an autorelease pool, make sure it's the most-recently-allocated pool and not some other one.

这篇关于iPhone开发 - 内存管理的经验教训的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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