iPhone app uses 150 MB memory and still no low memory warning!
Asked Answered
W

3

4

I have a questionary application, navigation based which I create and push my tableviews eachtime from a nib. there is no leakage and in instruments live bytes seems around 2-3 MB.

I tested in real device (jailbroken IOS4 iPhone), when I go through deep in the navigation (around 200 page pushes) I can see that memory usage goes upto 150 MB! when I navigate back to root then they are all freed, but isnt this a weird behavior? (around 800 KB for each nib view and no big data or images in it)

The most weird thing is, I put some alerts to didreceivememorywarning and didunloadview methods, and yet didnt receive any memory alerts!

-Why I never get any memory warning and viewDidUnload even the app uses 150 MB and more of memory? -Application works but is this memory usage a problem for Apple store?

Worsted answered 22/7, 2011 at 8:36 Comment(2)
Have you tried your application on a non-jailbroken iPhone ? Also, you should try on an Edge iPhone or 3G iPhone for the real deal about memory usage.Tesstessa
@Luzal I target ios4, jailbroken can be an issue?Worsted
W
2

I just add self.view=nil in viewDidDisappear method, it works and I can recover back, much better now. tnx Felz for the help

Worsted answered 30/7, 2011 at 17:47 Comment(0)
A
16

Something Funky is going on. Try the following code to check the OS version of how much memory you app uses

-(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));
    }


}

You will need to #import "mach/mach.h"

This will tell you how much memory the operating system has granted your app. So if what you are seeing is some weird Instruments behavior, this should shed some light.

Agustinaah answered 22/7, 2011 at 10:21 Comment(8)
I tried this code in simulator, app starts with 28 Mb and goes up to 60-70MB as I go through navigation(adds 300Kb per push) I dont receive no memory warning at all.Worsted
Well, the simulator has a LOT more memory than the device. Try it on the device.Agustinaah
I tried on device, every push retains around 1 mb of memory! and I can go through 200MB of memory usage without any low memory warnining, but then when I open other apps to fill the memory then return back I see it has freed a around 50Mb or so but still no traces of that it hit the didreceivememoryWarning method?! what is going on here? and why every pushed view wastes 1 mb of memoryWorsted
The fact that every pushed view user 1Mb is not surprising. The views still exist when they are in the navigation stack. They are in memory. What is weird is that the app is not crushing. My usual problem is apps crushing! not the other way. Cheers.Agustinaah
should I unload the views each time I push controller? And what Apple store says about huge memory usage?Worsted
Huge memory usage will kill you app. I'm not sure you can unload your views in a UINavigationViewController. All the stack will be in memory. If there is a way around this I haven't seen it. If you really need to push that many views you could build your own controller that releases the views in the stack and re-allocates them as you come up the stack.Agustinaah
I just wrote self.view=nil in viewDidDisappear and it works and I can recover back, much better now.Worsted
Good question RRB. What I do is start a NSTimer that calls report_memory method every 5 secs when trying to nail down a memory bug.Agustinaah
W
2

I just add self.view=nil in viewDidDisappear method, it works and I can recover back, much better now. tnx Felz for the help

Worsted answered 30/7, 2011 at 17:47 Comment(0)
M
0

Old question, but complementing fsaint's answer in case someone still wants to know how to use it:

It can be put anywhere you want to log memory usage, like in a particular view controller. To log the entire app, you can put on your AppDelegate.m. On the top of the file:

#import <mach/mach.h>

Paste the method anywhere in the class:

- (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 ) {
        long mb = info.resident_size / 1000000;
        NSLog(@"Memory in use (in Mbytes): %lu", (long)mb);
    } else {
        NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
    }
}

Include a timer to call this method from didFinishLaunchingWithOptions:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    (...)
    [NSTimer scheduledTimerWithTimeInterval: 2.0
                                     target: self
                                   selector: @selector(report_memory)
                                   userInfo: nil
                                    repeats: YES];
}

Run the app and watch your logs for memory usage.

Magocsi answered 6/2, 2017 at 17:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.