How to Use the vm_stat and purge Commands in macOS Terminal to Monitor and Clear Memory

Unlike traditional desktop operating systems that leave unused RAM empty, the Darwin kernel underpinning macOS treats free physical memory as wasted potential. macOS intentionally utilizes available RAM for file caching, pre-allocated application states, and compressed memory pools to maximize overall system responsiveness. However, developers running large local Docker containers, compiling Xcode targets, or executing memory-intensive simulation scripts frequently need to analyze exact memory pressure and reclaim inactive file caches. The built-in vm_stat and purge terminal utilities provide direct access to the Darwin virtual memory subsystem.

Understanding Darwin Virtual Memory Page States

The vm_stat tool reports memory in terms of system pages (by default, 4,096 bytes or 4KB on Intel Macs, and 16,384 bytes or 16KB on Apple Silicon Apple M-series chips). The kernel divides memory into distinct functional categories:

  • Wired Memory (Pages wired down): Crucial operating system components, kernel allocations, and driver tables that cannot be compressed or paged out to disk storage.
  • Active Memory (Pages active): Memory recently accessed and actively used by currently executing software processes.
  • Inactive Memory (Pages inactive): Memory containing data recently used by closed or idle processes. It remains cached in RAM so that re-launching those apps is instantaneous, but can be released if active applications demand space.
  • Compressed Memory (Pages occupied by compressor): Memory compressed in RAM by the macOS memory compressor to avoid swapping to the internal SSD.
  • Free Memory (Pages free): Unallocated RAM waiting for assignment.

Step 1: Check System Memory Page Size

Because page size varies between CPU architectures, check your hardware’s page size to accurately interpret numerical totals:

pagesize

Intel-based Macs will output 4096 (4KB per page), while Apple Silicon Macs (M1 through M4) will output 16384 (16KB per page).

Step 2: Inspect Real-Time Memory Allocations with vm_stat

To view an instantaneous breakdown of virtual memory statistics, execute vm_stat in the Terminal:

vm_stat

The output provides a detailed listing formatted as follows:

Mach Virtual Memory Statistics: (page size of 16384 bytes)
Pages free:                                24512.
Pages active:                             345210.
Pages inactive:                           210450.
Pages speculative:                         15200.
Pages throttled:                               0.
Pages wired down:                         189420.
Pages purgeable:                           32140.
"Translation faults":                 1452901230.
Pages copy-on-write:                    34219080.
Pages zero filled:                     891240192.
Pages reactivated:                       4129010.
Pages purged:                            2314092.
File-backed pages:                        142090.
Anonymous pages:                          413570.
Pages stored in compressor:               120940.
Pages occupied by compressor:              38120.

Step 3: Monitor Live Memory Changes Continuously

Similar to the top or vmstat commands on Linux, you can pass an interval integer to vm_stat to observe real-time page transitions during heavy workloads:

# Print memory statistics every 2 seconds
vm_stat 2

Each new row displays incremental deltas in free, active, inactive, and compressed memory, allowing you to observe exactly when your application triggers compression or swap operations.

Step 4: Convert vm_stat Pages to Megabytes Using a One-Liner

Because calculating raw page counts into megabytes or gigabytes mentally is cumbersome, pipe vm_stat output through awk to convert pages into legible megabytes automatically:

vm_stat | perl -ne '/page size of (\d+)/ and $s=$1; /Pages\s+([^:]+)[^\d]+(\d+)/ and printf("%-20s: %8.2f MB\n", $1, $2 * $s / 1048576);'

This script parses your machine’s exact page size and displays active, inactive, wired, and compressed memory in clean megabyte units.

Step 5: Force Inactive Memory Reclamation Using the purge Command

When developing software or conducting benchmark testing, you often need to simulate a cold application start where disk caches and inactive memory pools are cleared. The built-in purge command instructs the Darwin kernel to flush all inactive and disk cache allocations immediately:

sudo purge

Upon entering your administrator password, macOS forces disk-backed inactive pages to release, transferring them directly into the Pages free pool. You can verify the immediate impact by running vm_stat before and after the purge command.

Auditing Memory Pressure via memory_pressure

Raw free RAM is not the primary performance indicator on macOS; Memory Pressure is what determines whether the system is memory-constrained. Run the native memory_pressure command to check whether your hardware is experiencing physical bottlenecking:

memory_pressure

If the reported pressure level is Normal, the system is performing optimally. If the level escalates to Warn or Critical, macOS is actively swapping memory to the SSD, indicating that active applications require more physical RAM than is available.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.