简体   繁体   English

如何在Swift中实现didReceiveMemoryWarning?

[英]How to implement didReceiveMemoryWarning in Swift?

Whenever I create a new View Controller subclass, Xcode automatically adds the method 每当我创建一个新的View Controller子类时,Xcode都会自动添加该方法

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated
}

Usually I just delete it or ignore it. 通常我只是删除它或忽略它。 This is what all the tutorials I have seen do, too. 这就是我所见过的所有教程。 But I assume that since Xcode gives it to me every time, it should be somewhat important, right? 但我认为,因为Xcode每次都给我,所以它应该有点重要,对吧? What should I be doing here? 我该怎么办? I assume that disposing of resources means setting them to nil, but what exactly are "resources that can be recreated"? 我假设处理资源意味着将它们设置为nil,但究竟什么是“可以重新创建的资源”?

I have seen these questions: 我看过这些问题:

But they are all pre-Swift. 但他们都是斯威夫特之前。 Although I don't know much about Objective-C, I have heard that the memory management is different. 虽然我对Objective-C了解不多,但我听说内存管理不同。 How does that affect what I should do in didReceiveMemoryWarning ? 这有什么影响我应该在didReceiveMemoryWarning做什么?

Other notes: 其他说明:

  • I am fuzzily aware of Automatic Reference Counting and lazy instantiation 我模糊地了解自动引用计数和延迟实例化
  • The documentation on didReceiveMemoryWarning that I found was rather brief. 我发现的didReceiveMemoryWarning 文档相当简短。

Swift 迅速

Swift uses ARC just like Objective-C does ( source to Apple Docs ). Swift像Objective-C一样使用ARC( 源自Apple Docs )。 The same kind of rules apply for freeing memory, remove all of the references to an object and it will be deallocated. 同样的规则适用于释放内存,删除对象的所有引用,它将被释放。

How to free memory 如何释放记忆

I assume that disposing of resources means setting them to nil, but what exactly are "resources that can be recreated"? 我假设处理资源意味着将它们设置为nil,但究竟什么是“可以重新创建的资源”?

"resources that can be recreated" really depends on your application. “可以重新创建的资源”实际上取决于您的应用程序。

Examples 例子

Say you are a social media app that deals with a lot of pictures. 假设您是一个处理大量图片的社交媒体应用程序。 You want a snappy user interface so you cache the next 20 pictures in memory to make scrolling fast. 您需要一个快速的用户界面,以便在内存中缓存接下来的20张图片以快速滚动。 Those images are always saved on the local file system. 这些图像始终保存在本地文件系统中。

  • Images can take up a lot of memory 图像会占用大量内存
  • You don't need those images in memory. 你不需要在内存中存储这些图像。 If the app is low on memory, taking an extra second to load the image from a file is fine. 如果应用内存不足,花一点时间从文件加载图像就可以了。
  • You could entirely dump the image cache when you receive that memory warning. 收到内存警告时,您可以完全转储映像缓存。
  • This will free up memory that the system needs 这将释放系统所需的内存

You are creating an amazing game that has a number of different levels. 您正在创建一个具有多个不同级别的精彩游戏。 Loading a level into your fancy game engine takes a while so if the user has enough memory you can load level 3 while they are playing level 2. 将等级加载到您喜欢的游戏引擎需要一段时间,因此如果用户有足够的内存,您可以在玩2级时加载等级3。

  • Levels take up a lot of memory 级别占用了大量内存
  • You don't NEED the next level in memory. 你不需要内存的下一个级别。 They are nice to have but not essential. 他们很高兴但不是必不可少的。
  • LevelCache.sharedCache().nextLevel = nil frees up all that memory LevelCache.sharedCache().nextLevel = nil释放所有内存

What shouldn't you deallocate 什么不应该解除分配

Never deallocate the stuff that is on screen. 永远不要释放屏幕上的东西。 I've seen some answers to related questions deallocate the view of the UIViewController. 我已经看到相关问题的一些答案解除了UIViewController的视图。 If you remove everything from the screen you might at well crash (in my opinion). 如果你从屏幕上移除所有东西,你可能会崩溃(在我看来)。

Examples 例子

If the user has a document open that they are editing, DON'T deallocate it. 如果用户打开了一个他们正在编辑的文档,请不要取消分配。 Users will get exceptional angry at you if your app deletes their work without ever being saved. 如果您的应用删除了他们的工作而没有得到保存,用户将非常生气。 (In fact, you should probably have some emergency save mechanism for when that happens) (事实上​​,你可能应该有一些紧急保存机制,以便何时发生)

If your user is writing a post for your fabulous social media app, don't let their work go to waste. 如果您的用户正在为您精彩的社交媒体应用撰写帖子,请不要让他们的工作浪费掉。 Save it and try to restore it when they open the app again. 保存并尝试在再次打开应用程序时恢复它。 Although it's a lot of work to setup I love apps that do this. 虽然设置很多我喜欢这样做的应用程序。

Note 注意

Most modern devices rarely run out of memory. 大多数现代设备很少耗尽内存。 The system does a pretty good job of killing apps in the background to free up memory for the app running in the foreground. 该系统在后台杀死应用程序非常出色,可以为前台运行的应用程序释放内存。 You have probably seen an app "open" in the App Switcher yet when you tapped on the app it opened to its initial state. 您可能已经在应用程序切换器中看到了一个应用程序“打开”,但当您点击应用程序时,它已打开到其初始状态。 The OS killed the app in the background to free up memory. 操作系统在后台杀死了应用程序以释放内存。 See State Restoration for info on how to avoid this problem. 有关如何避免此问题的信息,请参阅状态恢复

If your app is getting consistent memory warnings when you aren't doing a huge amount of processing make sure that you aren't leaking memory first. 如果您的应用程序在没有进行大量处理时获得一致的内存警告,请确保您没有先泄漏内存。 Detecting memory leaks is outside the scope of this answer. 检测内存泄漏超出了本答案的范围。 Docs and a tutorial . 文档教程

When didReceiveMemoryWarning is called, it means your app is using too much memory (compare with memory of device), and you should release any additional memory used by your view controller to reduce the memory of your app. 当调用didReceiveMemoryWarning ,这意味着您的应用程序使用了太多内存(与设备内存相比),您应该release any additional memory used by your view controller以减少应用程序的内存。 If the memory app gets over the memory of device, iOS will kill your app immediately. 如果内存应用程序超过设备内存,iOS将立即终止您的应用程序。 "resources that can be recreated" means somethings you can recreate it again at somewhere, you don't need them now (don't need to put them in the memory). “可以重新创建的资源”意味着你可以在某个地方再次重新创建它,你现在不需要它们(不需要将它们放在内存中)。 And you can release it when get didReceiveMemoryWarning . 并且你可以在获得didReceiveMemoryWarning时释放它。

Here is another detail topic: ios app maximum memory budget 这是另一个细节主题: ios app最大内存预算

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM