开发者

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

开发者 https://www.devze.com 2023-03-22 09:08 出处:网络
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 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?


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消