如何在 macOS 下模拟低内存条件?
How do you simulate low memory conditions under macOS?
在开发和调试 macOS 应用程序时,有没有办法模拟系统范围的 "low memory" 事件或通知?
iOS 模拟器有这样的功能,但我似乎无法在 macOS 下找出等效项。
我有兴趣模拟低内存情况,看看 类 像 NSPurgeableData
和 IOSurface
如何处理被清除。
(除了编写一个只不断分配内存的简单应用程序...)
从代码中,您可以使用 setrlimit 函数来控制应用程序中的系统资源消耗。
或者,ulimit 允许控制从终端启动的资源
macOS
上用于模拟低内存条件的工具是 memory_pressure
(示例):
sudo memory_pressure -S -l critical
上面的命令模拟了(-S
)临界级别(-l
临界)的内存压力。
NAME
memory_pressure -- Tool to apply real or simulate memory pressure on the
system.
SYNOPSIS
memory_pressure [-l
level] | [-p
percent_free] | [-S
-l
level]
OPTIONS
-l
Apply real or simulate memory pressure (if specified alongside
simulate argument) on the system till low memory notifications corresponding to are generated. Supported values are "warn" and "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.
DESCRIPTION
A tool to apply real or simulate memory pressure on the system
SEE ALSO
vm_stat(1)
在开发和调试 macOS 应用程序时,有没有办法模拟系统范围的 "low memory" 事件或通知?
iOS 模拟器有这样的功能,但我似乎无法在 macOS 下找出等效项。
我有兴趣模拟低内存情况,看看 类 像 NSPurgeableData
和 IOSurface
如何处理被清除。
(除了编写一个只不断分配内存的简单应用程序...)
从代码中,您可以使用 setrlimit 函数来控制应用程序中的系统资源消耗。
或者,ulimit 允许控制从终端启动的资源
macOS
上用于模拟低内存条件的工具是 memory_pressure
(示例):
sudo memory_pressure -S -l critical
上面的命令模拟了(-S
)临界级别(-l
临界)的内存压力。
NAME
memory_pressure -- Tool to apply real or simulate memory pressure on the system.SYNOPSIS
memory_pressure [-l
level] | [-p
percent_free] | [-S
-l
level]OPTIONS
-l
Apply real or simulate memory pressure (if specified alongside simulate argument) on the system till low memory notifications corresponding to are generated. Supported values are "warn" and "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.
DESCRIPTION
A tool to apply real or simulate memory pressure on the systemSEE ALSO
vm_stat(1)