如何在Swift中实施didReceiveMemoryWarning ?

时间:2021-03-03 14:31:33

Whenever I create a new View Controller subclass, Xcode automatically adds the method

每当我创建一个新的视图控制器子类时,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? 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"?

通常我只是删除它或者忽略它。这也是我所见过的所有教程所做的。但是我假设因为Xcode每次都给我,所以它应该是很重要的,对吧?我应该在这里做什么?我假设处理资源意味着将它们设置为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. How does that affect what I should do in didReceiveMemoryWarning?

但它们都是预敏捷的。虽然我不太了解Objective-C,但我听说内存管理是不同的。这如何影响我在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的文档非常简短。

2 个解决方案

#1


28  

Swift

Swift uses ARC just like Objective-C does (source to Apple Docs). The same kind of rules apply for freeing memory, remove all of the references to an object and it will be deallocated.

Swift使用ARC就像Objective-C一样(来自苹果文档)。同样的规则适用于释放内存,删除对对象的所有引用,然后它将被释放。

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. Those images are always saved on the local file system.

假设你是一个处理大量图片的社交媒体应用。你想要一个快速的用户界面,这样你就可以在内存中缓存接下来的20张图片,使滚动速度更快。这些图像总是保存在本地文件系统中。

  • 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. If you remove everything from the screen you might at well crash (in my opinion).

永远不要释放屏幕上的东西。我看到了一些相关问题的答案释放UIViewController的视图。如果你从屏幕上删除所有内容,你可能会崩溃(在我看来)。

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.

如果你的应用程序得到了一致的内存警告,而你没有做大量的处理,确保你没有首先泄漏内存。检测内存泄漏超出了这个答案的范围。文档和教程。

#2


2  

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. If the memory app gets over the memory of device, iOS will kill your app immediately. "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叫时,它意味着你的应用程序是使用太多内存(与内存的设备),你应该释放任何额外的内存使用的视图控制器减少内存的应用程序。如果内存的应用程序在内存的设备,iOS将立即杀死你的应用。“可以重新创建的资源”指的是你可以在某个地方重新创建的东西,你现在不需要它们(不需要把它们放在内存中)。你可以在收到didReceiveMemoryWarning时发布它。

Here is another detail topic: ios app maximum memory budget

这里是另一个细节主题:ios应用程序最大内存预算。

#1


28  

Swift

Swift uses ARC just like Objective-C does (source to Apple Docs). The same kind of rules apply for freeing memory, remove all of the references to an object and it will be deallocated.

Swift使用ARC就像Objective-C一样(来自苹果文档)。同样的规则适用于释放内存,删除对对象的所有引用,然后它将被释放。

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. Those images are always saved on the local file system.

假设你是一个处理大量图片的社交媒体应用。你想要一个快速的用户界面,这样你就可以在内存中缓存接下来的20张图片,使滚动速度更快。这些图像总是保存在本地文件系统中。

  • 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. If you remove everything from the screen you might at well crash (in my opinion).

永远不要释放屏幕上的东西。我看到了一些相关问题的答案释放UIViewController的视图。如果你从屏幕上删除所有内容,你可能会崩溃(在我看来)。

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.

如果你的应用程序得到了一致的内存警告,而你没有做大量的处理,确保你没有首先泄漏内存。检测内存泄漏超出了这个答案的范围。文档和教程。

#2


2  

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. If the memory app gets over the memory of device, iOS will kill your app immediately. "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叫时,它意味着你的应用程序是使用太多内存(与内存的设备),你应该释放任何额外的内存使用的视图控制器减少内存的应用程序。如果内存的应用程序在内存的设备,iOS将立即杀死你的应用。“可以重新创建的资源”指的是你可以在某个地方重新创建的东西,你现在不需要它们(不需要把它们放在内存中)。你可以在收到didReceiveMemoryWarning时发布它。

Here is another detail topic: ios app maximum memory budget

这里是另一个细节主题:ios应用程序最大内存预算。