Mac的资料真心少,先来看看iOS是怎么做的。
memory pressure observer on iOS
how to observe
- 可以监听 UIApplicationDidReceiveMemoryWarningNotification 通知。
|
|
- 实现 UIViewController 自身的方法didReceiveMemoryWarning,该方法在 App 接收到 memory warning 会被调用,可以在方法中释放当前不再使用的资源。
|
|
how to mock
模拟器菜单
hardware -> simulate memory warning模拟发通知 UISimulatedMemoryWarningNotification
|
|
- 私有 API
|
|
memory pressure observer on Mac
终于要切入正题,按照iOS调研的节奏开始查资料~
how to observe
以下来自 Github
|
|
其中 dispatch_source_create 的解释:
Creates a new dispatch source to monitor low-level system objects and automatically submit a handler block to a dispatch queue in response to events.
可以监控的 system objects 很多,包括 Timer,Signal, MemoryPressure 等。
其中 memoryPressure level 的定义如下:
|
|
how to mock
目前找到的方法,
在 terminal 输入:
|
|
-S causes the system to simulate the specified memory pressure (warn | critical), and -s tells how many seconds to hold that simulated pressure.