macOS 下如何模拟内存不足的情况?

ken*_*nyc 3 macos xcode cocoa memory-management appkit

在开发和调试 macOS 应用程序时,有没有办法模拟系统范围内的“内存不足”事件或通知?

iOS 模拟器有这样的功能,但我似乎无法找出 macOS 下的等效项。

我感兴趣的是模拟内存不足的情况,看看班怎么样NSPurgeableDataIOSurface手柄被清除。

(除了编写一个不断分配内存的简单应用程序......)

l'L*_*L'l 6

macOS用于模拟低内存条件的工具是memory_pressure(示例):

sudo memory_pressure -S -l critical
Run Code Online (Sandbox Code Playgroud)

上面的命令模拟 ( -S) 一个临界级别 ( -lcritical)的内存压力。

NAME
memory_pressure -- 在系统上应用真实或模拟内存压力的工具。

概要
memory_pressure [-l级别] | [ -ppercent_free] | [-S -l级别]

选项在系统上
-l 应用真实或模拟内存压力(如果与模拟参数一起指定),直到生成对应的低内存通知。支持的值为“warn”和“critical”。

 -p <percent_free> Allocate memory till the available memory in the system
 is <percent_free> of total memory. If the percentage of available memory
 to total memory on the system drops, the tool will free memory till
 either the desired percentage is achieved or it runs out of memory to
 free.

 -S Simulate memory pressure on the system by placing it artificially for
 <sleep_seconds> duration at the "warn" or "critical" level.

 -s <sleep_seconds> Duration to wait before allocating or freeing memory
 if applying real pressure. In case of simulating memory pressure, this is
 the duration the system will be maintained at an artifical memory level.
Run Code Online (Sandbox Code Playgroud)

描述
一种在系统上应用真实或模拟内存压力的工具


另请参见vm_stat(1)