Skip to content

Latest commit

History

29 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

mac-free

A free command replacement for macOS that displays memory usage information similar to the Linux free command.

Overview

macOS doesn't include a free command like Linux. This utility provides a familiar interface for checking memory usage, using native macOS APIs to gather accurate memory statistics.

Features

  • Display total, used, free, and available memory
  • Show swap usage
  • Multiple output formats (bytes, KiB, MiB, GiB, human-readable)
  • Wide mode showing all macOS memory categories (active, inactive, wired, compressed)
  • Continuous monitoring with configurable refresh interval
  • Compatible with common free command options

Building

Requirements

  • macOS 10.12 or later
  • Xcode Command Line Tools (xcode-select --install)

Compile

make

Debug Build

make debug

Installation

git clone https://github.com/techquestsdev/mac-free.git
cd mac-free
# Build and install
make
sudo make install
# or use Homebrew
brew install techquestsdev/tap/mac-free

Installing from source will add free command to /usr/local/bin/. You can specify a different prefix:

sudo make install PREFIX=/opt/local

Usage

free [options]

Options

OptionLong FormDescription
-b--bytesDisplay output in bytes
-k--kibiDisplay output in kibibytes (default)
-m--mebiDisplay output in mebibytes
-g--gibiDisplay output in gibibytes
-h--humanDisplay output in human-readable format
-w--wideWide output (show all memory categories)
-t--totalShow total for RAM + swap
-s N--seconds NRepeat printing every N seconds
-c N--count NRepeat printing N times, then exit
-P--pressureShow macOS memory pressure (kernel signal)
-V--versionOutput version information and exit
--helpDisplay help and exit

Examples

Basic usage

$ free
total used free shared buff/cache available
Mem: 16777216 8388608 2097152 0 6291456 8388608
Swap: 2097152 524288 1572864

Human-readable format:

$ free -h
total used free shared buff/cache available
Mem: 16.0Gi 8.0Gi 2.0Gi 0B 6.0Gi 8.0Gi
Swap: 2.0Gi 512.0Mi 1.5Gi

Wide output with all memory categories:

$ free -w -h
total used free active inactive wired compressed available
Mem: 16.0Gi 8.0Gi 2.0Gi 4.0Gi 2.0Gi 2.0Gi 1.0Gi 8.0Gi
Swap: 2.0Gi 512.0Mi 1.5Gi

Monitor memory every 2 seconds:

free -h -s 2

Monitor memory 5 times with 1 second interval:

free -h -s 1 -c 5

Show macOS memory pressure (real kernel signal):

$ free -h -P
total used free shared buff/cache available
Mem: 16.0Gi 8.0Gi 2.0Gi 0B 6.0Gi 8.0Gi
Swap: 2.0Gi 512.0Mi 1.5Gi
Pressure: normal (50% available)

Show totals:

$ free -h -t
total used free shared buff/cache available
Mem: 16.0Gi 8.0Gi 2.0Gi 0B 6.0Gi 8.0Gi
Swap: 2.0Gi 512.0Mi 1.5Gi
Total: 18.0Gi 8.5Gi 3.5Gi

Memory Categories

Standard Output

ColumnDescription
totalTotal installed physical memory
usedUsed memory(Active + Wired + Compressed)
freeCompletely unused memory
sharedAlways 0: macOS exposes no system-wide shared-memory counter
buff/cacheReclaimable file cache (Inactive + Purgeable + Speculative)
availableEstimate of memory available for starting new applications

Wide Output(macOS - specific)

ColumnDescription
activeMemory currently in use or recently used
inactiveMemory marked as not recently used
wiredMemory that cannot be paged out(kernel, drivers)
compressedMemory that has been compressed to save space

How It Works

This utility uses macOS - specific APIs to gather memory information:

  • host_statistics64() - Retrieves VM statistics including page counts for different memory states
  • sysctl() - Gets total physical memory and swap usage
  • sysctlbyname("kern.memorystatus_vm_pressure_level") - Real kernel memory-pressure level (for -P)
  • vm_page_size - System page size for converting page counts to bytes

Memory Calculation Notes

  • Used Memory = Active + Wired + Compressed
  • Available Memory = Free + Inactive + Purgeable + Speculative (approximation, as macOS can reclaim these pages)
  • Shared Memory = reported as 0; macOS has no cheap system-wide shared-memory counter (unlike Linux Shmem). Deriving a real value would require summing the shared footprint across every task (as top/libtop do).

Differences from Linux free

  1. buff/cache: On Linux, this shows buffer and cache memory separately. On macOS, we sum the reclaimable file cache: inactive + purgeable + speculative pages.

  2. shared: Linux tracks shared memory segments (Shmem). macOS exposes no equivalent system-wide counter, so this is reported as 0 rather than a misleading approximation.

  3. available: Both systems estimate available memory, but the calculation differs due to different memory management strategies.

  4. Wide mode: Shows macOS-specific categories (compressed memory is unique to macOS).

  5. Memory pressure (-P): macOS relieves memory pressure with an in-RAM compressor before it touches swap, so the Linux-style free/swap columns can look healthy while the system is actually thrashing. -P prints the real kernel signal (kern.memorystatus_vm_pressure_level — the same one Activity Monitor uses): normal, warning, or critical.

Uninstall

sudo make uninstall

Or manually:

sudo rm /usr/local/bin/free

Troubleshooting

"Permission denied" when installing

Use sudo for installation:

sudo make install

Values don't match Activity Monitor exactly

This is expected.Activity Monitor may use slightly different calculations or update at different intervals.The values should be close but not identical.

Swap shows 0 total

macOS uses dynamic swap. If no swap file has been created yet, total swap may show as 0. Swap files are created on - demand in /private/var/vm/.

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

Building for Development

# Build with debug symbols
make debug
# Run tests
make test# Clean build artifacts
make clean

License

MIT License - See LICENSE file for details.


Made with ❤️ and C

About

A 'free' command replacement for macOS - displays memory usage information similar to the Linux free command, using native macOS APIs

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

2 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages