Jetsam模型
iOS使用的是低内存处理机制Jetsam,这是一个基于优先级队列的机制。相关的探究推荐这篇文章
Jetsam可以简单的抽象为:前台应用程序,在触发某个或多个条件时,触发系统事件,被系统kill掉。而OOM也就是因为触发了内存相关的系统事件,被系统kill掉了。
Memory Footprint
Footprint是苹果推荐的内存度量及优化的指标。而Memory Footprint的值达到Limit line时,就会触发内存警告,并进一步导致OOM,相关可以参考这条Stack Overflow。以下是摘抄的部分OOM时的Memory Footprint Limit Line。
device: (crash amount/total amount/percentage of total)
iPad1: 127MB/256MB/49%
iPad2: 275MB/512MB/53%
iPad3: 645MB/1024MB/62%
iPad4: 585MB/1024MB/57% (iOS 8.1)
iPad Mini 1st Generation: 297MB/512MB/58%
iPad Mini retina: 696MB/1024MB/68% (iOS 7.1)
iPad Air: 697MB/1024MB/68%
iPad Air 2: 1383MB/2048MB/68% (iOS 10.2.1)
iPad Pro 9.7": 1395MB/1971MB/71% (iOS 10.0.2 (14A456))
iPad Pro 10.5”: 3057/4000/76% (iOS 11 beta4)
iPad Pro 12.9” (2015): 3058/3999/76% (iOS 11.2.1)
iPad Pro 12.9” (2017): 3057/3974/77% (iOS 11 beta4)
iPod touch 4th gen: 130MB/256MB/51% (iOS 6.1.1)
iPod touch 5th gen: 286MB/512MB/56% (iOS 7.0)
iPhone4: 325MB/512MB/63%
iPhone4s: 286MB/512MB/56%
iPhone5: 645MB/1024MB/62%
iPhone5s: 646MB/1024MB/63%
iPhone6: 645MB/1024MB/62% (iOS 8.x)
iPhone6+: 645MB/1024MB/62% (iOS 8.x)
iPhone6s: 1396MB/2048MB/68% (iOS 9.2)
iPhone6s+: 1392MB/2048MB/68% (iOS 10.2.1)
iPhoneSE: 1395MB/2048MB/69% (iOS 9.3)
iPhone7: 1395/2048MB/68% (iOS 10.2)
iPhone7+: 2040MB/3072MB/66% (iOS 10.2.1)
iPhone X: 1392/2785/50% (iOS 11.2.1)
OOM触发的其他事件
这个是本文想要讨论的重点。我们已知,Compressed Image是Dirty Memory,也就是在Memory Footprint中的。在最新的Xcode 9.3升级后,发现某些特殊的图片,在加载后并不会引起Memory Footprint的升高,且不会受到Memory Footprint Limit Line的限制。
比如,以iPhone 6+为例,通常Memory Footprint值到达645MB时,就会crash掉,同时Xcode的console中有memory issue的提示。然而,当我们加载特殊的图片时,根据图片加载内存计算,当其加载到1100MB左右时,才会crash,并且同样在console中有memory issuer的提示。简要的说:
通过Xcode9.3打包出的程序,加载特殊图片可以使用更多内存。比如原来加载15张图片就会内存警告,OOM。现在则可以加载20张图片。
这个发现告诉我们,OOM的触发事件不只是footprint,至少还有另一个指标。目前已经向苹果提交了TSI,反馈给苹果的邮件如下。然而并未收到有效的回复,仅回复查看WWDC2018的session
上述现象的条件是使用Xcode9.3及以后打包,对iOS9~12有效,怀疑是LLVM的优化。我看了WWDC2018的内存和LLVM的session未发现有效的信息。
PLATFORM AND VERSION
iOS
IDE: Xcode 9.3 and above.
iOS:iOS 9 and above.
Device:iPhone 6p, iPhone 5s, iPhone x, iPhone 6sp, etc.
This appeared after I update Xcode 9.3, and
performed normal when I downgrade to Xcode 9.2.
DESCRIPTION OF PROBLEM
Memory Tool:IDE footprint memory, instruments-Allocations, source code;
Usually, when I load a 6000(px)*4000(px) image, it will cost 96MB memory. But I fount that, when I load some kind of picture, the footprint memory did not change obviously. And I can't find any memory increase in instruments-Allocations.
In addition, I find OOM is closely related to footprint memory level. But when I use this kind of picture, it can breakthroughs footprint memory limit.In other words, I can load more picture in my App when I use this kind of picture.
So, my question is, why this kind of picture has this feature, and what kind of picture will have this feature.
Usual picture:https://monkeytest.oss-cn-shanghai.aliyuncs.com/TestImageLarge.jpg
Unusual picture:https://monkeytest.oss-cn-shanghai.aliyuncs.com/gratisography-400H.jpg
or:https://gratisography.com/fullsize/gratisography-400H.jpg
Demo project:https://github.com/LululuSir/testDemo.git
STEPS TO REPRODUCE
1.Load some kind of picture.
2.Check the footprint memory use Xcode memory tool or source code.
NAME AND APPLE ID OF APP