首页 > 代码库 > About Memory Analysis
About Memory Analysis
关于内存分析About Memory Analysis
每当应用程序创建对象时,都会为它们分配内存。传统上,它已被应用的工作跟踪这些对象并释放他们时,他们不再需要的内存可以分配其他对象。自动引用计数(ARC)是一种通过让系统负责内存管理而使事情变得更容易的特性。在启用ARC的情况下,系统处理监控对象分配,并在适当时释放它们,只剩下很少的应用程序要做。然而,不管内存是如何管理的,即使是最好的应用程序设计也会遇到难以分离的偶尔内存问题。Whenever your app creates objects, memory is allocated for them. Traditionally, it has been the app’s job to track these objects and release them when they are no longer needed so the memory can be reallocated for other objects. Automatic Reference Counting (ARC) is a feature that makes things easier by putting the system in charge of memory management. With ARC enabled, the system handles monitoring object allocations and releasing them when appropriate, leaving very little for your app to do. Regardless of how memory is managed, though, even the best app designs can encounter occasional memory issues that are difficult to isolate.
通过绘制应用程序如何使用内存的图片,工具可以帮助您识别潜在的问题区域。它甚至可以自动检测某些类型的内存问题,并将它们标记给您进行分析。使用仪器监视和跟踪下列内容:Instruments can help by painting a picture of how your app uses memory, making it easier to identify potential problem areas. It can even auto detect certain types of memory problems and flag them for you for analysis. Use instruments to watch for and track the following:
-
总体内存使用。在高层次上监视应用程序如何使用内存,并将其与系统上其他活动进程的内存使用情况进行比较。寻找大的或意外的内存增长区域。查看监视器内存使用情况。Overall Memory Use. Monitor at a high level how your app uses memory and compare it to the memory usage of other active processes on the system. Look for areas of large or unexpected memory growth. See Monitor Memory Usage.
-
泄漏的内存。这是在某个时间点分配的内存,但从未被释放,不再被应用程序引用。由于没有引用它,所以现在无法释放它,内存也不能再使用了。例如,假设您编写了一个应用程序,它在绘图中创建矩形对象,但在绘图关闭时从不释放对象。在这种情况下,当包含矩形的图形被关闭时,应用程序将泄漏越来越多的内存。要修复泄漏,您需要确定哪些对象未被释放,然后更新应用程序以在适当的时间释放它。查看查找内存泄漏。Leaked Memory. This is memory that was allocated at some point, but was never released and is no longer referenced by your app. Since there are no references to it, there’s now no way to release it and the memory can’t be used again. For example, suppose you’ve written an app that creates rectangle objects in a drawing, but never releases the objects when the drawing is closed. In this case, your app would leak more and more memory whenever a drawing containing rectangles is closed. To fix the leak, you need to figure out which object isn’t being released, and then update your app to release it at the appropriate time. See Find Memory Leaks.
-
被遗弃的记忆。这是应用程序因某种原因而分配的内存,但不需要,也不会被引用。例如,假设您的应用程序在缓存之后将图像添加到缓存中,使用两倍于相同图像的内存。或者,也许你的应用程序维护了一系列的对象,以防以后需要访问它们,但实际上你从来没有这样做过。不像泄漏的内存,像这样的废弃内存仍然在你的应用程序中被引用。它毫无意义。因为它仍然在技术上是有效的,它更困难的仪器识别和需要更多的侦探工作,你找到。看到被遗弃的记忆。Abandoned Memory. This is memory that your app has allocated for some reason, but it’s not needed and won’t be referenced. For example, suppose your app adds images to a cache after they’ve already been cached—using double the memory for the same images. Or, maybe your app maintains an array of objects in case you need to access them later, but you never actually do. Unlike leaked memory, abandoned memory like this is still referenced somewhere in your app. It just serves no purpose. Since it’s still technically valid, it’s more difficult for Instruments to identify and requires more detective work on your part to find. See Find Abandoned Memory.
-
僵尸.这是已经释放并且不再需要的内存,但是您的代码仍然在某处引用它。例如,假设您的应用程序包含一个图像缓存。一旦缓存被清除,您的应用程序不应该试图引用它先前包含的图像。对这些不存在的图像的调用被认为是对不再存在的对象的僵尸引用。见僵尸。Zombies. This is memory that has been released and is no longer needed, but your code still references it somewhere. For example, suppose your app contains an image cache. Once the cache has been cleared, your app shouldn’t attempt to refer to the images that it previously contained. Calls to these nonexistent images are considered zombies—references to objects that are no longer living. See Find Zombies.
因为内存问题可能很难找到,所以在不同阶段对应用程序进行周期性快照是有帮助的,以寻找意想不到的或无限制的内存增长。您可以比较快照以确定对象是如何分配和销毁的,以及内存是如何随时间使用的。Because memory problems can be difficult to find, it’s helpful to take periodic snapshots of your app at various stages in order to look for unexpected or unrestrained memory growth. You can compare snapshots to determine how objects are being allocated and destroyed, and how memory is used over time.
同样重要的是,通过不断地执行一组可重复的用户操作来测试应用程序,从而使应用程序进入一个新的状态,并在完成时恢复到原来的状态。这种类型的研究被称为世代分析。每次应用程序进入新状态时,都会生成一代“对象”。在大多数情况下,当应用程序返回其原始状态时,您应该期望这些对象被释放。例如,如果你的应用程序打开一个窗口,那么任何窗口对象应该释放当窗口关闭。如果不是,那么你可能有内存问题。在执行代分析时,由于缓存和其他初始化过程,初始代可能表现出比预期大的内存使用。因此,多次重复用户操作,以便更真实地了解应用程序的行为。It’s also important to test your app by continuously performing a set of repeatable user actions that cause your app to enter a new state and return to its original state when done. This type of research is known as generational analysis. Each time your app enters its new state, a “generation” of objects is created. In most cases, you should expect these objects to be released when your app returns to its original state. For example, if your app opens a window, then any window objects should be deallocated when the window is closed. If they aren’t, then you may have a memory problem. When performing generational analysis, initial generations may exhibit larger than expected memory use due to caching and other initialization processes. Therefore, repeat user actions multiple times in order to gain a truer sense of your app’s behavior.
TIP
For in-depth information about managing memory, see Advanced Memory Management Programming Guide.
About Memory Analysis