【问题标题】:why did my app just used about 36MB memory but i still receive low memory warning?为什么我的应用程序只使用了大约 36MB 内存,但我仍然收到内存不足警告?
【发布时间】:2011-03-27 05:10:35
【问题描述】:

我使用工具来跟踪我的应用程序的内存使用情况,在内存监视器中,我发现实际内存低于 36MB,大多数时候只有 32MB,但我仍然收到管理器内存不足和崩溃的警告。

Incident Identifier: CFEF044E-E839-4DB7-9ED6-E22CD92B9171
CrashReporter Key:   80954447762f1882da7df309d5493cf8647f4c8b
OS Version:          iPhone OS 3.2 (7B367)
Date:                2010-08-10 15:21:25 +0800

Free pages:        593
Wired pages:       18705
Purgeable pages:   1664
Largest process:   iPad4HB

Processes
         Name                 UUID                    Count resident pages
         iPad4HB <51ec4f748adb8d99e4456ba49644e516>   27182 (jettisoned) (active)
     debugserver <6f10136b8d55ad8cbd29c09a402b1f94>     149
    syslog_relay <4ceaed776d2df957fa130712f4ef21d0>      70
notification_pro <4c9a7ee0a5bbe160465991228f2d2f2e>      71
             lsd <a4d852c1c8da2b3d231bdc90887b52ba>     143
            apsd <f655e615956fa9881fd423d762791e7d>     197
         notifyd <5e9d5bee7c3eae1c8b494c79eb11406e>      62
        BTServer <64e4a6ea6b1240db2331e05a29caa862>     132
      CommCenter <97bf297944ac4bde19bcee96dd23bd5f>     191
     SpringBoard <c7a5904c12db7b14334a4edaa4cabaa9>    4844 (active)
      accessoryd <b4e1403f53e4c7b53b0ec833007f6589>     122
         configd <aca9fa3380322669164fd6b1a3864300>     374
   fairplayd.K48 <2d997ffca1a568f9c5400ac32d8f0782>      86
       locationd <dd1ea88105c62173908ce767db5c4d37>     654
   mDNSResponder <820560222d47a1f2a0dce98a7f8a9721>     112
    mediaserverd <ea8bac28b06fe3980fdd44b5caceb563>    5931
       lockdownd <497fd54c79a680bf29f5d9320f514613>     579
MobileStorageMou <c277b79c2157c4dc5cfc5c3ca35bd5f2>      76
         syslogd <66247e305d5c0bf6f1ce1cc950653263>      80
            ptpd <83de0f774bd6553d513ae9e19b0f9b56>     233
         launchd <66972eee4d865c4383b33d985d22994b>      72

**End**
Tue Aug 10 15:12:05 hokudaiaoniaowangruande-iPad SpringBoard[26] <Warning>: Received memory warning. Level=1
Tue Aug 10 15:12:05 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: Received memory warning. Level=1
Tue Aug 10 15:12:20 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:12:32 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playable
Tue Aug 10 15:12:32 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:12:37 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:13:28 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playable
Tue Aug 10 15:13:28 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:13:44 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:14:11 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playable
Tue Aug 10 15:14:11 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:14:20 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:14:50 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playable
Tue Aug 10 15:14:50 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:14:56 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:15:02 hokudaiaoniaowangruande-iPad mDNSResponder[19] <Error>: ERROR: mDNSPlatformReadTCP - recv: 60 (Operation timed out)
Tue Aug 10 15:16:11 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playable
Tue Aug 10 15:16:11 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:17:33 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:17:44 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playable
Tue Aug 10 15:17:44 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:18:01 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:18:07 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playable
Tue Aug 10 15:18:07 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:18:07 hokudaiaoniaowangruande-iPad configd[24] <Notice>: jetsam: kernel memory event (90), free: 695, active: 1734, inactive: 985, purgeable: 3456, wired: 18656
Tue Aug 10 15:18:07 hokudaiaoniaowangruande-iPad SpringBoard[26] <Warning>: Received memory warning. Level=2
Tue Aug 10 15:18:07 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: Received memory warning. Level=2
Tue Aug 10 15:18:07 hokudaiaoniaowangruande-iPad SpringBoard[26] <Warning>: Memory level is urgent (10%) and there are no background apps to ask to exit.
Tue Aug 10 15:19:07 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:19:15 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playable
Tue Aug 10 15:19:15 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:20:02 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:20:06 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playable
Tue Aug 10 15:20:06 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:21:01 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:21:06 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playable
Tue Aug 10 15:21:06 hokudaiaoniaowangruande-iPad iPad4HB[107] <Warning>: playing
Tue Aug 10 15:21:24 hokudaiaoniaowangruande-iPad configd[24] <Notice>: jetsam: kernel termination snapshot being created
Tue Aug 10 15:21:24 hokudaiaoniaowangruande-iPad com.apple.launchd[1] (UIKitApplication:com.yourcompany.iPad4HB[0x4df4][107]) <Notice>: (UIKitApplication:com.yourcompany.iPad4HB[0x4df4]) Bug: launchd_core_logic.c:3232 (24063):0
Tue Aug 10 15:21:24 hokudaiaoniaowangruande-iPad com.apple.launchd[1] (UIKitApplication:com.yourcompany.iPad4HB[0x4df4][107]) <Notice>: (UIKitApplication:com.yourcompany.iPad4HB[0x4df4]) Bug: launchd_core_logic.c:3309 (24063):0
Tue Aug 10 15:21:24 hokudaiaoniaowangruande-iPad com.apple.debugserver-44[106] <Warning>: 1 [006a/1703]: error: ::read ( 6, 0x2809f4, 1024 ) => -1 err = Bad file descriptor (0x00000009)
Tue Aug 10 15:21:24 hokudaiaoniaowangruande-iPad com.apple.launchd[1] (UIKitApplication:com.yourcompany.iPad4HB[0x4df4][107]) <Notice>: (UIKitApplication:com.yourcompany.iPad4HB[0x4df4]) Bug: launchd_core_logic.c:2650 (24063):10
Tue Aug 10 15:21:24 hokudaiaoniaowangruande-iPad com.apple.launchd[1] (UIKitApplication:com.yourcompany.iPad4HB[0x4df4][107]) <Notice>: (UIKitApplication:com.yourcompany.iPad4HB[0x4df4]) Working around 5020256. Assuming the job crashed.

【问题讨论】:

  • “仅”32 MB 超出了在旧 iOS 设备上杀死应用程序的硬性限制。这对于 iPhone 应用程序来说是一个非常大的内存。
  • 但我在 Instruments 中根本找不到任何泄漏。只是 32-36MB 的实际内存使用率很高。
  • 大量内存使用并不总是由于泄漏。正如其他人所建议的那样,您需要使用 Instruments 来确定在您的应用程序中分配最多内存的位置(很可能是大型 UI 元素或图像)。
  • @Brad Larson 谢谢你,现在我把它归结为 20+ MB 的使用量。

标签: iphone memory ipad


【解决方案1】:

您是否想在很短的时间内加载大量数据?尝试运行图像视图动画序列时尤其如此。当内存小于 20MB 时,我遇到了崩溃。经过调查,我发现我的图像序列包含非常大的图像,崩溃的原因不是总内存负载。原因是内存带宽。

【讨论】:

  • “内存带宽”不会导致崩溃。
  • 真的吗?尝试将 20MB 的数据从磁盘加载到 RAM 以及从 RAM 加载到 VRAM,看看会发生什么。
  • 一次我加载的最多数据可能是数十个 *.png 。大约数百 KB。
  • 那么这可能不是你的问题。我在尝试加载大约 15 320 x 480 png 图像时在 2g 设备上崩溃了。但预加载后,问题解决了。
  • @taskinoor - 在您的情况下导致崩溃的原因不是内存带宽,而是内存使用量激增。如果您的应用程序超过了被终止的阈值(在早期设备上的总使用量为 30 MB),它将被终止。
【解决方案2】:

如果您使用的是 2g 或 3g 等旧型号,则 36 megs 可能比您必须使用的内存更多。我通常会在 25mb 左右开始收到警告。

【讨论】:

    【解决方案3】:

    当手机内存不足时,您会收到内存警告。 “1 级”内存警告很正常。如果您的应用程序崩溃,那么可能是因为您没有正确处理内存警告,而不是因为您使用了太多内存。有两个修复:

    • 忽略内存警告(不要在 didReceiveMemoryWarning 中进行超级调用)
    • 修复您的代码

    【讨论】:

      【解决方案4】:

      似乎是内存泄漏。要找到它,不仅要使用“泄漏”工具。 “分配”也可以帮助你。积极使用它、标记堆、比较内存使用情况并检查“仍然存在”的对象(尤其是您自己的类的对象)。

      有时无法自动检测内存泄漏(使用“Leaks”仪器)。例如,假设您有一些 UIImageView。这个图像视图应该在用户点击“创建”按钮时创建,并在用户点击“销毁”按钮时销毁。您可以在下面的代码中实现此行为:

      - (IBAction)bCreateTouchUpInside:(id)sender {
          ivSomeImageView = [[UIImageView alloc] initWithImage:iSomeImage];
          [self.view addSubview:ivSomeImageView];
          bCreate.enabled = NO;   bDestroy.enabled = YES;
      }
      
      - (IBAction)bDestroyTouchUpInside:(id)sender {
          [ivSomeImageView release];
          bCreate.enabled = YES;   bDestroy.enabled = NO;
      }
      

      并且会出现内存泄漏:两次保留(creation + addSubview),一次释放(release)。要解决这个问题,您应该在直接发布消息之前添加[ivSomeImageView removeFromSuperview];。但是“泄漏”仪器不会检测到这种泄漏,因为有一个正确的指向您的图像视图的指针(父视图存储指向它的子视图的指针)。为您检测到此泄漏的唯一机会是多次按“创建”并查看内存使用量每次都在增加。

      【讨论】:

        【解决方案5】:

        我认为它可以准确地告诉您问题所在:您使用了太多内存。如果操作系统告诉您这一点而您对此不采取任何措施,那么它可能(确实)将您的程序从内存中弹出。

        您没有发布任何代码,因此不清楚您是否有内存泄漏或只是使用了太多内存。尝试使用静态分析器(在 Xcode 的构建菜单中)和 Instruments 的 Leaks 部分来检查泄漏。如果它没有找到,那么您可能只需要优化您的应用程序以使用更少的内存。

        我不确定你在哪里发现 36Mb 是可以的。在第一代设备上,您将被踢出远高于 20Mb 的内存。我认为没有任何绝对数字记录在案,因此您需要做两件事:在真实设备上进行测试,并在它告诉您使用过多时释放尽可能多的内存。

        【讨论】:

        • 是的,我用我的 ipad 测试它,我认为问题很可能是我使用了太多内存,我可以在 36-40MB 内存上运行我的应用程序至少 20 分钟。我想这是正常的
        【解决方案6】:

        我在最近的项目中遇到了类似的问题。对我来说,问题在于隐藏在委托属性上的模糊保留语句中,该语句持有保留计数,导致视图无法成功解除分配。

        试试这个故障排除技术..... 大概你可能有一个 uinavigation 控制器推送/弹出视图控制器。

        考虑在视图中有一些东西持有保留计数导致视图留在内存中(即使视图已经弹出并且您已经发布了发布声明)。另一个症状(不是特定于这个问题,但相关的是如果弹出所有视图控制器,您甚至可能会遇到应用程序崩溃。)

        所以在视图控制器的 dealloc 方法中 对各个视图的保留计数执行日志语句。 (以及您正在合成的其他对象。) 例如。

        UiNavController
        
        -Level0ViewController
                -(void)dealloc{
                 // HERE TEST FOR RETAIN COUNT OF 2  (2 = one for the alloc / one for the addSubview)
                 DLog(@"level0view  retain count: :%@",[level0view retainCount]);
                 [level0view release]; // this will make retain count 1.
                 [super dealloc];
          -- Level0View
             -(void)dealloc{
                DLog(@"dealloc"); // set break points here and confirm this is being called.
             }
           -- SomeImageView.h
              // for me my problem lied here with the infringing line below being RETAIN! it should  have been assign. changing this successfully resolved app crash and memory leak. 
              @property(retain) id delegate
        

        附:查看 Karl Kroft 的 DLog - 日志记录非常好,可以为您提供类的行号并隐藏 NSLog 通常添加的内容。

        【讨论】:

        • 绝对保留计数没有用。
        猜你喜欢
        • 2012-04-08
        • 1970-01-01
        • 2012-02-21
        • 2013-11-15
        • 2012-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-10
        相关资源
        最近更新 更多