iOS 低内存崩溃,但内存使用率非常低

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5980636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:53:37  来源:igfitidea点击:

iOS Low Memory Crash, but very low memory usage

iphoneobjective-ciosipadmemory-management

提问by Max

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.

这让我烦恼了很长时间。我的应用程序运行时占用了大约 2.74MB 的内存。没关系。但是当它创建一个 UIWebView 时,它会上升到大约 5.87MB 并继续崩溃。这些是在我的第一代 iPad 上运行时在 Instruments 中的 Live Bytes 下给出的值。

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.

我已经删除了对 imageNamed 的所有调用,将自动释放的内容更改为 alloc/release。但我无法弄清楚为什么会发生这种情况,这让我发疯。

Thanks for any help!

谢谢你的帮助!

回答by Paul Bruneau

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"
  1. 使用分配性能工具运行。
  2. 单击“分配”下的 VM 跟踪器“行”(在屏幕截图中)
  3. 点击“自动快照”

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

然后你会看到你的脏内存(目前在我的屏幕截图中为 20.34MB)。

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

此屏幕截图将有所帮助

回答by fsaint

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 answeruse 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));
        }
    }
    
  1. 正如之前的回答中提到的,UIImage 的位元不考虑内存泄漏量,告诉您您的应用程序正在使用!所以你可能有很多使用大量内存的 UIImages 没有显示在总数中。我的建议是使用 Allocations 来检查在您的应用程序运行时创建和销毁的 UIImage 对象的数量。
  2. 本答案所述,使用以下代码

    -(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")

查看操作系统为您的应用分配的内存量。这是您应用程序使用的内存上更准确的数字。(你需要#import "mach/mach.h")

cheers!

干杯!

回答by ForgeGaming

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.

您快速按下该菜单按钮 2 次,您将看到该应用程序单击所有应用程序的 x,然后打开您需要的应​​用程序。