【问题标题】:Is there a way to send Memory Warning to iPhone device manually? [duplicate]有没有办法手动向 iPhone 设备发送内存警告? [复制]
【发布时间】:2012-01-14 16:53:23
【问题描述】:

这几天我遇到了一个问题。我正在使用图像缓存库,它运行良好,但最终我遇到了内存问题,应用程序自行退出(我想这是因为它只是内存不足)。从图像缓存库中读取源代码后,我发现据说当有内存警告事件时,它会释放所有缓存的图像(图像很大)。无论如何,我是否可以手动直接向设备发送内存警告事件?我正在使用 xcode 仪器工具来评估内存使用情况。

【问题讨论】:

    标签: iphone ios xcode cocoa-touch memory-warning


    【解决方案1】:

    可以通过调用 UIApplication 的私有方法来产生内存警告。它在 iOS 6.1 及更低版本上运行良好

      [[UIApplication sharedApplication]performSelector:@selector(_performMemoryWarning)];
    

    注意:在将应用程序提交到 iTunes 之前删除该选择器调用,否则它将被拒绝。

    【讨论】:

    • 在将应用程序提交给 Apple 之前不要忘记删除它,仅用于测试,否则您的应用程序将被拒绝。
    • 这在带有 iOS 7 的 Xcode 5 中没有任何作用
    【解决方案2】:

    您可以在模拟器中手动模拟:

    Hardware -> Simulate Memory Warning
    

    您也可以通过编程方式对其进行模拟:

    - (void)simulateMemoryWarning
    {
    #if TARGET_IPHONE_SIMULATOR
      #ifdef DEBUG
        CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(),    (CFStringRef)@"UISimulatedMemoryWarningNotification", NULL, NULL, true);
      #endif
    #endif
    }
    
    CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), (CFStringRef)@"UISimulatedMemoryWarningNotification", NULL, NULL, true);
    

    【讨论】:

    猜你喜欢
    • 2021-10-14
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 2020-03-06
    • 2010-09-16
    • 1970-01-01
    • 2016-08-02
    • 2022-10-24
    相关资源
    最近更新 更多