Created
March 3, 2025 18:20
-
-
Save havenwood/f2f5c49c2c90c6787ae2295e9805adbe to your computer and use it in GitHub Desktop.
Adjust wired limits to allocate more memory to the GPU with Apple Silicon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env -S zsh -e | |
zparseopts -D -F -- \ | |
g:=opt_gb -reserve-gb:=opt_gb \ | |
p:=opt_percent -reclaim-percent:=opt_percent \ | |
h=opt_help -help=opt_help \ | |
v=opt_version -version=opt_version | |
if (( ${#opt_help} )); then | |
print -- "Usage: $(basename $0) [options] | |
Options: | |
-g, --reserve-gb GB GB of memory reserved (default: 8) | |
-p, --reclaim-percent PERCENT Percentage of reclaimable memory (default: 20) | |
-h, --help | |
-v, --version | |
Adjust macOS GPU memory limits with sysctl." | |
exit 0 | |
fi | |
if (( ${#opt_version} )); then | |
print -- "$(basename $0) version 0.0.1" | |
exit 0 | |
fi | |
typeset -i reserve_gb=${opt_gb[2]:-8} | |
typeset -i reclaim_percent=${opt_percent[2]:-20} | |
typeset -i total_mb=$(( $(sysctl -n hw.memsize) / 1024 / 1024 )) | |
memory_minus_gb() { | |
local -i gb=${1:?Specify GB to subtract} | |
print -- $(( total_mb - gb * 1024 )) | |
} | |
memory_minus_percent() { | |
local -i percent=${1:?Specify percentage to subtract} | |
print -- $(( total_mb - total_mb * percent / 100 )) | |
} | |
# Set the uppper memory limit the GPU can allocate. | |
sudo sysctl -w iogpu.wired_limit_mb="$(memory_minus_gb $reserve_gb)" | |
# Set the low water mark (LWM) for reclaiming memory. | |
sudo sysctl -w iogpu.wired_lwm_mb="$(memory_minus_percent $reclaim_percent)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment