36

This has been annoying me for a long time. My app runs taking up about 2.74MB of memory. That's fine. But then when it creates a UIWebView it goes up to around 5.87MB and proceeds to crash. Those are the values given under Live Bytes in Instruments while running on my 1st gen iPad.

There is no crash log that I can find. The following is from the console:

MyApp[1205] <Warning>: Received memory warning. Level=1
MyApp[1205] <Warning>: applicationDidReceiveMemoryWarning
SpringBoard[30] <Warning>: Received memory warning. Level=1
MobileMail[1199] <Warning>: Received memory warning. Level=1
configd[26] <Notice>: jetsam: kernel memory event (95), free: 428, active: 1853, inactive: 1011, purgeable: 338, wired: 15122
configd[26] <Notice>: jetsam: kernel termination snapshot being created
com.apple.launchd[1] <Notice>: (UIKitApplication:com.apple.mobilemail[0x8966]) Exited: Killed: 9
com.apple.launchd[1] <Notice>: (UIKitApplication:com.MyApp.MyApp[0xdd4f]) Exited: Killed: 9
SpringBoard[30] <Warning>: Application 'Mail' exited abnormally with signal 9: Killed: 9
kernel[0] <Debug>: launchd[1207] Builtin profile: MobileMail (sandbox)
SpringBoard[30] <Warning>: Application 'MyApp' exited abnormally with signal 9: Killed: 9
configd[26] <Debug>: CaptiveNetworkSupport:UIAllowedNotifyCallback:70 uiallowed: false
ReportCrash[1206] <Error>: libMobileGestalt loadBasebandMobileEquipmentInfo: CommCenter error: 1:45
ReportCrash[1206] <Error>: libMobileGestalt copyInternationalMobileEquipmentIdentity: Could not get mobile equipment info dictionary
ReportCrash[1206] <Error>: Saved crashreport to /Library/Logs/CrashReporter/LowMemory-2011-05-12-160645.plist using uid: 0 gid: 0, synthetic_euid: 0 egid: 0

I have removed all calls to imageNamed, changed autoreleased stuff to alloc/release. But I cannot work out why this is happening and it's driving me insane.

Thanks for any help!

Max
  • 2,760
  • 1
  • 28
  • 47
  • It looks like your app isn't crashing, it's being terminated to free up memory. Is it in the foreground at the time this happens? – Tony May 12 '11 at 15:51
  • Yep it's in the foreground the whole time. The app opens, using 2.74MB, then I tap to open a UIWebView which increases usage to 5.87MB, before being killed within a few seconds. And it occurs with any vaguely complex web page being loaded. – Max May 12 '11 at 16:02
  • 1
    I don't think 5.87mb is that big a deal. My guess is a view controller is being unloaded and that causes other issues. Try NSLogs or debugging viewDidUnload in your view controllers. – ax123man May 12 '11 at 16:20
  • There aren't actually any view controllers in use. It's pretty much just a UIView with a UITableView, UIView, and UIWebView. – Max May 12 '11 at 16:36
  • Could it have anything to do with an NSArray with 50 or so CTFrameRefs combined with the UIWebView causing the system to run out of memory allocated to graphics? – Max May 12 '11 at 16:41
  • @Alec please check the overall byte allocation, it is surprising when we use webviews. Also please check the maximum allocation spike in the allocation graph. – Ravin May 13 '11 at 09:02
  • Thanks @Ravin, overall bytes is ~65MB, but I'm not sure how to measure the max allocation spike? – Max May 13 '11 at 09:32
  • you have to move playehead over the allocation graph. also, you can do this by specifying inspection range. – Ravin May 13 '11 at 10:43
  • The biggest allocation spike is in the creation of the UIWebView, lasts ~0.2s and has 530KB Live and 750KB Overall Bytes. When I zoom in, this is made up of 2 spikes, allocating 260KB and 236KB respectively. – Max May 13 '11 at 11:27
  • I feel something is getting released which is required by the app for that time and is unavailable. as the error specifies "Could not get mobile equipment info dictionary". – Hanuman May 19 '11 at 22:41
  • @Hanuman I think that error is unrelated, it looks like that's the crash reporter trying to gather all the data it can about the device and not not getting stuff that's only applicable to iPhone. – Max May 21 '11 at 14:40

3 Answers3

51

You are almost certainly using a lot more memory than you think.

It's not obvious what you have to do to see what your app is really using, but once you do it a couple times, you'll remember.

  1. Run with the Allocations performance tool.
  2. Click the VM Tracker "row" under "Allocations" (in the screenshot)
  3. Click "Snapshot Automatically"

Then you will see your Dirty memory (currently 20.34MB in my screenshot).

This should give you a much better picture of why your app is getting quit. You probably hav some large leaking happening.

good luck!

This screenshot will help

Paul Bruneau
  • 1,026
  • 1
  • 9
  • 15
  • Thanks for the tip Paul! Dirty memory is resident 34MB and virtual 67MB. When I expand it I see several Core Animation type rows taking up a significant percentage, but nothing else that's familiar. Any suggestions or ideas as to how go about solving this further? – Max May 22 '11 at 17:32
  • It's difficult to say without knowing more about the way your app works, but a good place to start is to see what resources are being kept around maybe longer than you need them. Put some log statements in the memory-management methods of your view controllers to see if/when they are getting unloaded and/or dealloc'd. You might find some that are hanging around a lot longer than you think they should be. Then you can hunt down why. – Paul Bruneau May 24 '11 at 14:20
  • For instance, the app I was running for the screenshot above, it is a picture book app with a scrollview that contains the individual pages. It only has 18 pages and it can all remain in memory. But I still have code that unloads pages when they get to the end of a "page cache queue" that I keep track of (just an array). If a page gets viewed, it goes to the top of the array. If the array has more than x pages in it, then I drop page x + 1 and the memory gets freed. – Paul Bruneau May 24 '11 at 14:23
22

I have two things to add that may help:

  1. As mentioned in a previous answer , the bitmat of a UIImage is not considered in amount of memory Leaks tells you your app is using! so you may have a lot of UIImages that are using a lot of memory buy not showing in the total. My recommendation is to use Allocations to check out the number of UIImage objects created and destroyed while your app runs.
  2. As mentioned in this answer use the following code

    -(void) report_memory {
        struct task_basic_info info;
        mach_msg_type_number_t size = sizeof(info);
        kern_return_t kerr = task_info(mach_task_self(),
                                       TASK_BASIC_INFO,
                                       (task_info_t)&info,
                                       &size);
        if( kerr == KERN_SUCCESS ) {
            NSLog(@"Memory in use (in bytes): %u", info.resident_size);
        } else {
            NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
        }
    }
    

to see the amount of memory the operating system has assigned your app. That is more accurate number on the memory you app is using. (You will need to #import "mach/mach.h")

cheers!

Community
  • 1
  • 1
fsaint
  • 8,759
  • 3
  • 36
  • 48
-5

You press that menu button 2 times fast you will see the app click the x for all the apps then open the ones you need.

ForgeGaming
  • 1
  • 1
  • 1