内存警告但小活字节 [英] Memory Warning but Small Live Bytes

查看:161
本文介绍了内存警告但小活字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我重复一些操作(选择图片+处理)几次,然后崩溃后,我会收到级别1和2的内存警告。

In my application, I get a memory warning of level 1 and then 2 after repeating some action (choosing a picture + processing) several times and then a crash.

泄漏工具没有显示任何泄漏。我也在仪器中的分配工具,我的活字节大约4 MB,总共我分配113 MB。最大时我有大约20 MB的内存,当图片加载。

The leak tool doesn't show any leak. I'm also following the Allocations tool in Instruments and my Live Bytes are roughly 4 MB, overall I allocate 113 MB. At maximum I have maybe 20 MB in memory when the picture is loaded.

由于我必须重复一个动作才能得到崩溃,所以很可能是内存泄漏。但是,我不知道如何找到它,因为我的活字节是4 MB和事情应该分配(除了一个小的泄漏约100 KB在UIImagePickerController)。

Since I have to repeat an action to get to the crash, it is very likely to be a memory leak. However, I don't know how to locate it since my live bytes are 4 MB and things supposed to be allocated (apart a small leak of ~100 KB in the UIImagePickerController).

我可以相信内存泄漏/分配工具有多少?

How much can I trust the memory leak/allocation tools? Would you have an advice to help me locate the reason of the problem?

推荐答案

我不知道iPhone OS的工作原理,所以这基本上只是猜测,但在没有垃圾收集器压缩堆内存的系统中,它将随着时间的推移而碎片化。有很多的内存空间不意味着很多连续的内存是免费的。

I don't know how iPhone OS works, so this is basically just guessing, but in systems where no garbage collector compacts the heap memory, it will be fragmented over time. Having a lot of memory free does not mean that a lot of contiguous memory is free.

例如,如果你总是需要4MB的内存进行一些处理,此分配模式:

For example, if you always need 4MB of memory for some processing, and you have this allocation pattern:


  • 分配4MB

  • 分配1KB

  • 免费4MB

  • 分配1KB

  • Allocate 4MB
  • Allocate 1KB
  • Free 4MB
  • Allocate 1KB

它是计算结果,或者什么)

(You don't free the 1KB blocks because it's the computation result, or whatever)

你可能最终只有3,999K的可用连续内存 - 所以下一次分配4MB时,它将位于间隙,即使它几乎适合。这意味着即使几乎整个内存(或者更确切地说,是寻址空间)是空闲的,你也可以用完内存。

You may end up with only 3,999K of free contiguous memory - so next time you allocate 4MB, it will be located after the gap, even though it almost fits. This means you can run out of memory even though almost the entire memory (or rather, addressing space) is free.

授予,现代系统不应该遭受这个问题,但是它们可能,特别是如果应用程序从未关闭并且没有压缩的垃圾收集器。注意,一些系统有一个低碎片堆,特别是对于这样的情况(重新分配和释放相同大小的块),但通常需要明确请求它。

Granted, modern systems shouldn't suffer from this problem, but they may, especially if the application is never shut down and does not have a compacting garbage collector. Note that some systems have a low-fragmentation heap especially for situations like this (re-allocating and freeing blocks of the same size), but you usually need to explicitly request it.

这篇关于内存警告但小活字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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