Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reducing memory of UIImages from camera using Instruments

My app, which uses ARC, does the following:

  1. Uploads a picture taken by the camera
  2. Compresses the picture for use as a thumbnail.
  3. I do this using [UIImage imageWithData:UIImageJPEGRepresentation( original, 0.1f )]
  4. I set the uncompressed picture reference to nil for ARC to free the memory

Repeating this sequence will mean that several compressed thumbnails are on the screen. After about 7 or 8 pictures, the app wil crash due to low memory.

In Instruments, I am attempting to use Allocations in tandem with Memory Monitor to find the source of my problem.

Some Instruments stats:

Allocation - Live Bytes jumps by about 2 MB after taking a picture, but then goes down by 1.5 MB after original picture reference is set to nil. That seems to be a good thing, but...

Here's an eventual state of the application. #Living seems to be very high relative to the Live Bytes, right?

  Live Bytes  #Living   #Transitory  Overall   #Overall Bytes    
   3.72 MB    24538     80679        90.1 MB   105301

Memory Monitor (tracking inspection head) - My application starts up at 7.5 MB and taking one picture results in that increasing by ~13 MB. For the state that I listed above, Memory Monitor says that the app is taking up 72.67 MB of "Real Memory" and 123.79 MB of Virtual Memory.

Given that the Live Bytes are very small, I know I'm doing something right. However, given that the memory footprint in other places is large, I'm also sure I'm doing something very wrong. Any ideas what that might be, or how to track it down?

like image 386
Eitan Avatar asked Jan 12 '12 01:01

Eitan


1 Answers

Eitan27,

This is not an ARC problem. This is about how to manage multiple large items in memory. iOS has several mechanisms to help you here. If you write the image to flash and then reopen it as memory mapped data, you will mostly solve your problem. How? The OS manages the mapping into your resident memory footprint with immutable data. Because these items are immutable and, hence, never dirty, it can then flush the mapped pages when necessary. The downside of this mechanism is the limited number of file descriptors available to each app.

Andrew

like image 197
adonoho Avatar answered Nov 25 '22 20:11

adonoho