用 didRecieveMemoryWarning 到底部

发布于 2024-09-26 10:46:13 字数 632 浏览 3 评论 0原文

我必须用 didRecieveMemoryWarning 方法深入到底。我在这个论坛上阅读了大约三十个关于这个主题的帖子。 每个答案都是不同的。

  1. 第一个问题。您应该释放 didRecieveMemoryWarning 中的对象还是只是将它们设置为 nil?或者两者兼而有之?

  2. 我读到,我应该在 didRecieveMemoryWarning 方法中释放 tableView 数据源(如果有的话)。我还读到,您应该仅在此方法中释放 IBOutlets。我很困惑,这里什么是正确的?

  3. 是否只有我在 viewDidLoad 中启动的对象才应该在 didRecieveMemoryWarning 中释放?或者我只是分配给它的属性,例如:labelTitleText.text = @“Woodie Guthrie”? labelTitleText 是我的头文件中的一个属性。

在今天的应用程序中,我在 dealloc 方法中释放了所有内容(也包括像上面示例一样分配的标签)。这感觉不对。

我真的很感激一个小代码示例,这样它就变得更容易理解,我注意到,这就是其他线程所缺少的。

祝大家有美好的一天!

I have to go to the bottom with the didRecieveMemoryWarning method. I have read like thirty threads about this topic on this forum. And every answer is differerent.

  1. First question. Should you release objects in didRecieveMemoryWarning or just set them to nil? Or both?

  2. I have read that I should release the tableView data source, if you have one, in the didRecieveMemoryWarning method. I have also read that you should just release IBOutlets i this method. I'm confused, what is correct here?

  3. Is it only objects I initiate in viewDidLoad I should release in didRecieveMemoryWarning? Or is it properties I just assign to, for example: labelTitleText.text = @"Woodie Guthrie"?
    labelTitleText is a property in my header file.

In my application today, I release everything (also labels assigned like the example above) in my dealloc method. This doesn't feel right.

I would really appreciate a small code example so it becomes easier to understand, this is what the other threads is missing to, I've noticed.

Have a great day everyone!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抱猫软卧 2024-10-03 10:46:13

最重要的是,当您收到内存警告时,您实际上不需要释放任何内容。然而,什么都不做会大大增加某些应用程序被杀死的可能性,而该应用程序可能是你的。但事实可能并非如此。

如果您实际上正在使用已分配的所有内存(例如,不是可以稍后重新加载的图像等),则什么都不做(或保存重要状态以防万一),并希望其他一些后台进程被杀死或饥饿。执行此操作的应用程序比您想象的要多。

如果您确实有可以发布的内容,请发布大内容(至少 1 个 VM 页面或更大)。释放小东西(短字符串等)对于某些后台应用程序是否被杀死或饥饿几乎没有影响。

将指针(或对象)设置为 nil 而不释放内存(或释放对象)是极其糟糕的形式,因为这只会泄漏内存并会增加发生坏事的可能性。释放内存/对象后,您可以将它们设置为零。

The bottom line is that you don't actually need to release anything when you get a memory warning. However doing nothing greatly increases the likelihood that some app will be killed, and that app might be yours. But it might not be.

If you are actually using all the memory you have allocated (e.g. not images you could just reload again later, etc.) then do nothing (or save important state just in case), and hope some other background process gets killed or starved. More apps do this than you might think.

If you do have stuff you can release, release the big stuff (at least 1 VM page and larger). Releasing the small stuff (short strings, etc.) will make almost no difference as to whether or not some background app gets killed or starved.

Setting pointers (or objects) to nil without freeing memory (or releasing objects) is extremely bad form, as this just leaks memory and will increase the likelihood of bad things happening. You can set them to nil after you free/release the memory/objects.

套路撩心 2024-10-03 10:46:13

当手机内存不足时,会调用 didRecieveMemoryWarning。您应该释放以后需要时可以检索的所有资源。想想缓存、未使用的对象和类似的东西。

据我了解,您不应该在 didRecieveMemoryWarning 中释放 IBOutlets 等,因为应用程序需要这些内容才能正常工作。

但是,您应该在 viewDidUnload 之类的方法中释放它们,当然还有您所说的 dealloc 中。 (如果您在某个时候保留了它们,很可能使用某个属性)

didRecieveMemoryWarning is called when the phone is low on memory. You should release any resources that you can retrieve later when needed. Think about caches, unused objects and that kind of stuff.

It is my understanding that you shouldn't release IBOutlets and such in didRecieveMemoryWarning, because those are needed for the app to work correctly.

You should however release these in methods like viewDidUnload and of course in the dealloc as you've stated. (if you retained them at some point, most likely using a property)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文