Camera Memory Issues in iOS7

VelocityVelocity NZMember ✭✭✭

Has anyone had any issues with the UIImagePickerController under iOS7?

We have an existing app which has been rock-solid under iOS6, but camera functionality is flakey under iOS7. Code has even gone through numerous rewrites which various implementations of camera control using both Xamarin.Mobile component and direct control of UIImagePickerController.

The Problem

  • Presenting a UIImeagePickerController either natively or using Xamarin.Media, throws "Memory Warning received" errors to console log. This occurs before tapping "Take Photo".
  • Mem usage spikes on "Take Photo" and then backs off back to normal levels.
  • Taking multiple photos on low-end devices w/512MB RAM (ie. iPad Mini, iPad2) typically crashes app after taking 4-5 photos.

Tried so far

  • Extensive work ensuring efficient memory management. All UIImageViews and UIImage C# are correctly disposed of to permit GC of the "wrapper" objects and eventual removal of native objects by the OS.
  • "DidReceiveMemoryWarning" is overridden and fires a GC.Collect() in a last-ditch attempt to free memory.
  • Tried different build configs, incl experimental SGen GC.

Testing shows...

  • Crash after 4-5 photos on devices w/512MB ram.
  • No crash on devices w/1GB RAM, but application still receives on/off memory warnings. App is not terminated though.
  • Works flawlessly on same low-end devices running iOS6.

Build configuration

  • Stable channel, XS running on OSX Mavericks.
  • Monotouch v7.x
  • SDK v6.1 (app not yet targeted at iOS7 as requires UI rewrite!)

Posts

  • SebastienPouliotSebastienPouliot CAXamarin Team Xamurai

    Mem usage spikes on "Take Photo" and then backs off back to normal levels.

    Several people reported those spike in iOS7. However it's not totally a new spike but a different way for Apple to report the memory usage.

    When used with an iOS7 device Xcode5's Instruments can report the VM memory, i.e. the memory that the OS is using on your application behalf. That's not the case when you use an iOS6 device so you cannot compare the results (without hiding the VM memory).

    Still iOS7 memory requirement are higher than the one of iOS6. Some devices have high a camera resolution / RAM ratio that makes them more likely to issue memory warnings.

  • VelocityVelocity NZMember ✭✭✭

    Thanks Sebastien.

    From a Xamarin perspective, do you have any recommendations as to best-practice when using UIImagePickerController? So far, the only solution would appear to be disabling camera functionality on the low-end devices and just allowing "picking" of images only.

  • SebastienPouliotSebastienPouliot CAXamarin Team Xamurai

    I do not believe there's nothing fundamentally wrong with UIImagePickerController. It just requires memory and RAM is a shared commodity.

    IOW some devices might not have much RAM (compared to what taking pictures requires) but it really depends on your (and others) applications running (and iOS should give priority to one in the foreground).

    Still knowing about this might help you design your application differently. E.g. if you're caching data it would be a good time to flush the cache before using this API. Think of it of pre-empting the likely memory warning.

    However disabling the camera might be a bit drastic. The memory spike I've seen looks big (and that might be misleading, read more on how Instruments reports memory) but it's short-lived. If your application is so close to the maximum available RAM then it may be worth running Instruments on other parts of the application and see if it's possible to reduce it's memory requirements.

  • manishTargusmanishTargus USMember
    edited January 2014

    I have same problem as my app crashes just after I am using camera in iPad with received memory warning. I have tried so many thing but still no luck.
    Can someone please tell me how to resolve it?

  • UrsinBrunnerUrsinBrunner CHMember ✭✭

    Is there any news on this topic? I also have memory issues when using the Xamarin MediaPicker to take a photo on iPhone 4 and iPhone 4s.
    As TheEngineer pointed out, the first memory warning appears before the photo is taken (the is no memory spike visible at this point).
    After the photo has been taken, there is a huge memory spike (also because I load the image to compress it and save it to the camera roll).

    I use using (UIImage img = UIImage.FromFile(imagePath)) to load the image, which seems to be the right way regarding your guide Memory best practices

    In a 'normal' situation, the memory warning is not a problem... the iOS kills a few apps, the memory goes back to a normal level... But it happened to me a lot of times, that in a "real" situation, e.g. after using my app intensively for a few hours, it crashes when opening the MediaPicker (last log-message: memory warning), which is ofc not a nice scenario.

    Which brings me to another question: How much memory does your MonoTouch app consume? On the XCode-Instruments -> Memory Monitor, how much real/virtual memory is used by your app?

  • Joshua_DJoshua_D USMember ✭✭

    Quite the bump, but I'm experiencing similar behavior in iOS8 (version 8.3). Does anyone have any insight as to what could be causing this? Or is the use of the Camera truly a large pull of resources, requiring a fairly large release of resources prior to launching it?

  • nick5454nick5454 USUniversity ✭✭✭

    After extensive testing and using instruments i've found, yes it spikes but its also rendering a HUGE image. I shrink mine down quickly.

    However, I would not necessarily say this is because of the ImagePicker. Any app that uses images like taking photos should have the entire app tested for leaks. When dealing with images null out, dispose, or use "using" statements as much as possible. I also null out the image coming from the response.

    I would optimize your entire app, as memory is always precious, and if you have a logout feature log in and out many times. I had camera issues that took a lot of memory tweaking and instance management using instruments to allow up to 50 images to be snapped in sequence between view controllers.

    I would also suggest ditching the ImagePicker as a whole and using the AV layer. It's faster, doesn't have display issues in ios7 and the memory is lighter after you tweak it for a very long time.

  • nick5454nick5454 USUniversity ✭✭✭
    edited May 2015

    I have my down to a 4 meg spike using zombie testing in instruments. I would like to brag that I can take 50 photos consecutively on the iPhone4 ( not the S ) without crashing the app. I do receieve memory warnings after about the 10th, but i'm calling dispose on the lot of things to keep the memory as low as possible and calling GC.Collect a modest amount of times. It's tough, but feels good after you have it working right.

Sign In or Register to comment.