开发者

Memory Warning but Small Live Bytes

开发者 https://www.devze.com 2023-01-01 11:44 出处:网络
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.

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.

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.

Since I have to repeat an action to get to the crash, it is very likely to be a memory开发者_JAVA百科 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?


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.

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

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

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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号