简体   繁体   English

Watch kit App:由于内存错误而终止

[英]Watch kit App : Terminated due to memory error

Hi I am developing an app in which I require to chache 50 images (size of all images is 2.5 mb) ,It is chaching the images but also increases the memory by 10 mb in Apple Watch App due to which app crashes . 嗨,我正在开发一个应用程序,其中我需要chache 50张图像(所有图像的大小是2.5 MB),它正在追逐图像,但也因Apple应用程序崩溃而在Apple Watch App中增加了10 MB的内存。

Xcode gives error in xCode “Message from debugger: Terminated due to memory error” Xcode在xCode中给出错误“来自调试器的消息:由于内存错误而终止”

The code i am using is below : 我使用的代码如下:

 for (var i : Int  = 1; i<26; i++) {

            let filenameHuman = NSString(format: "human_%d", i )
            let filenameZombie = NSString(format: "zombie_%d", i )

            var imageHuman : UIImage! =  UIImage(named: filenameHuman as String)
            var imageZombie : UIImage! =  UIImage(named: filenameZombie as String)

            WKInterfaceDevice.currentDevice().addCachedImage(imageZombie, name: filenameZombie as String)

            WKInterfaceDevice.currentDevice().addCachedImage(imageHuman, name: filenameHuman as String)

        }

        NSLog("Currently cached images: %@",WKInterfaceDevice.currentDevice().cachedImages)

Also the screenshot of Memory allocation and memory leak is : 内存分配和内存泄漏的屏幕截图是:

在此输入图像描述

Please Help, Thanks in advance . 请帮助,提前致谢。

  • Are any of your images actually animations (that would use up more space)? 您的任何图像是否真的是动画(会占用更多空间)?
  • Collect the return value of each call to addCachedImage(). 收集每个调用addCachedImage()的返回值。 False means it could not be added -- you need to check to that and it might give clues as to a particular problem image. False意味着它无法添加 - 您需要检查它,它可能会提供有关特定问题图像的线索。
  • Before calling anything, try to empty the cache, removeAllCachedImages. 在调用任何内容之前,请尝试清空缓存,removeAllCachedImages。 This means you will be clean from previous cache interactions using up the pool of memory. 这意味着您将使用内存池从先前的缓存交互中清除。

I think your problem is not a leak, I think your problem is over-retained allocations. 我认为你的问题不是漏洞,我认为你的问题是过度保留的分配。 So use the Allocations tool (with retain count tracking) to see how much memory was allocated (VM allocations) and how many entities are holding on to such memory (retain counts). 因此,使用Allocations工具(使用保留计数跟踪)来查看分配了多少内存(VM分配)以及有多少实体保留此类内存(保留计数)。

Inside your loop try to autorelease the memory that is used by the images since you don't want to wait for autorelease to happen later when the method returns. 在循环内部尝试自动释放图像使用的内存,因为您不希望等待自动释放在方法返回后发生。

for (var i : Int  = 1; i<26; i++) {
    autoreleasepool {
      /* code to cache images */ 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM