Mac Developer Tip: How to Simulate Memory Pressure

In iOS Simulator, you can, via a menu item, quickly simulate memory pressure for the simulated device to see if your app behaves correctly under low memory conditions (releasing caches, cleaning up resources, and so on).

Recently, I had the need for it on the Mac (where such a menu item is not available), while working on an XPC service for Yoink, to see if it terminated properly under certain conditions.

Meet the memory_pressure tool

OS X comes with the Terminal, and with the Terminal come some awesome tools. Among them is memory_pressure. Its man page says it all:

Apply real or simulate memory pressure on the system.

Usage of memory_tool

sudo memory_pressure -S -l critical

What this does is simulate (-S) a memory pressure of a critical level (-l critical).
sudo is needed, otherwise it will probably fail with an “Operation not permitted” error.

memory_tool’s options

-l:   Two levels of pressure are supported: ‘warn’ and ‘critical’.
-p:  For real pressure, this lets you define how much memory should remain free, in percent of total memory.
-S:  Simulate memory pressure, don’t really allocate memory.
-s:   If applying real pressure, this is the duration to wait before allocating or freeing memory.
       If it’s simulated pressure, the system will be maintained at an artificial memory level for this duration.

2 Comments, RSS

Comments are closed.