kennyc
kennyc

Reputation: 5710

How do you simulate low memory conditions under macOS?

When developing and debugging macOS apps, is there a way to simulate a system-wide "low memory" event or notification?

The iOS Simulator has such a feature but I can't seem to figure out an equivalent under macOS.

I'm interested in simulating a low-memory situation to see how classes like NSPurgeableData and IOSurface handle being purged.

(Other than writing a simple application that just keeps allocating memory...)

Upvotes: 7

Views: 2208

Answers (2)

l'L'l
l'L'l

Reputation: 47189

The tool on macOS is for simulating low memory conditions would be memory_pressure (example):

sudo memory_pressure -S -l critical

The command above simulates (-S) a memory pressure of a critical level (-l critical).

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)

Upvotes: 11

TheDarkKnight
TheDarkKnight

Reputation: 27611

From code, you can use the setrlimit function to control system resource consumption in your application.

Alternatively, ulimit allows control of resources launched from the Terminal

Upvotes: 1

Related Questions