views:

485

answers:

1

Hi everyone,

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 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?

+2  A: 

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.

OregonGhost
I don't think this is the reason. I don't repeat my actions often enough to lead to that much fragmentation.
Kamchatka
Out of curiosity, how much memory does the iPhone / a process in the iPhone have available?
OregonGhost
I tried rebooting the iPhone before, so I would guess it has the whole memory, around 160 MB available.
Kamchatka