【发布时间】:2020-10-23 04:09:21
【问题描述】:
我正在使用示例 «Detecting Human Body Poses in an Image» 中的代码(源代码可通过链接下载)。
当我在我的 iPhone 7+ 上启动此代码时,使用内存会大大增加,在我的设备变热并且应用程序将崩溃之后。
在我的代码中,我使用了Posenet 类的predict 方法。调试后发现有调用模型时出现问题,但不知道如何解决。
func predict(_ image: CGImage) {
DispatchQueue.global(qos: .userInitiated).async {
let input = PoseNetInput(image: image, size: self.modelInputSize)
//Problem is below
guard let prediction = try? self.poseNetMLModel.prediction(from: input) else {
return
}
//Problem is above
let poseNetOutput = PoseNetOutput(prediction: prediction,
modelInputSize: self.modelInputSize,
modelOutputStride: self.outputStride)
DispatchQueue.main.async {
self.delegate?.poseNet(self, didPredict: poseNetOutput)
}
}
}
方法是从captureOutput 调用的。
问题是什么?如何解决?
附: 这是启动应用程序 1.5 分钟后的内存使用情况。当内存使用量达到 600 MGB 时,应用程序崩溃。
【问题讨论】:
-
这些泄漏与您的应用程序内存使用问题无关。您在指责泄漏(它们的总大小为 +- 250kB),但您的应用程序消耗了 180MB 的内存,之后又消耗了 600MB。你显然没有发布一些东西,你可以有很强的参考周期,......你分享的代码是从示例中复制和粘贴的。我尝试在我的旧 iPhone SE 上运行示例代码,即使几分钟后我的内存使用量也低于 70MB。分享你的代码,你实际在做什么,而不是示例代码的逐字副本。
-
这也取决于从哪里调用
predict()。我假设它来自某种循环。你可能需要在某处扔一个autoreleasepool。
标签: ios swift iphone graphics coreml