ios - 如何响应来自 GCD 的内存压力通知?

标签 ios memory memory-management grand-central-dispatch memory-pressure

我正在使用 GCD 获取内存压力通知。

GCD documentation像这样描述一些常量:

DISPATCH_MEMORYPRESSURE_WARN

The system memory pressure condition is at the warning stage. Apps should release memory that they do not need right now.

DISPATCH_MEMORYPRESSURE_CRITICAL

The system memory pressure condition is at the critical stage. Apps should release as much memory as possible.

似乎合乎逻辑,我应该释放未使用的内存。然而,在其他地方( man pagessource code )我发现这个注释与这些常量相关:

Elevated memory pressure is a system-wide condition that applications registered for this source should react to by changing their future memory use behavior, e.g. by reducing cache sizes of newly initiated operations until memory pressure returns back to normal.

However, applications should NOT traverse and discard existing caches for past operations when the system system tem memory pressure enters an elevated state, as that is likely to trigger VM operations that will further further ther aggravate system memory pressure.

这让我很困惑。那么我应该释放内存,还是应该停止分配新内存?

最佳答案

MacOS 有一个使用后备存储的虚拟内存 (VM) 系统:文件系统。文件系统用于保存当前未使用的内存。当系统的实际内存 (RAM) 不足时,内存中未被积极使用的内容可以写入磁盘并稍后加载回 RAM。

iOS 有一个虚拟内存系统,但没有后备存储。当内存不足时,系统会要求应用程序降低其内存占用量。如果这不能释放足够的内存,系统将开始终止应用程序。

您从 libdispatch header 中引用的指南指的是 MacOS 虚拟内存系统,而不是 iOS。

在 iOS 上,应用程序应该丢弃对象在处理内存警告时减少缓存大小。应特别注意使用脏(不可清除)内存的对象。这是系统无法自行自动重用的内存 - 它必须首先被应用程序丢弃。在典型的 iOS 应用程序中,图像(图片)使用最脏的内存。

关于ios - 如何响应来自 GCD 的内存压力通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35654297/

相关文章:

ios - 将表添加到现有数据库 IOS sqlite

ios - Objective-C,边框颜色不适用于 UIColor

haskell - foreign.Storable 中的大尺寸 Bool

c - 在 C 中调整二维数组的大小

c++ - 可以手动泄漏内存吗?

ios - 在特定位置将 UILabel 分成两行

android - 滚动事件上的视频播放(移动设备)

ios - 注销应用程序时如何释放内存?

java - 简单的 Java 程序 - 分析显示意外行为

ios - [CFString保持]:消息发送到释放