Skip to content

Instantly share code, notes, and snippets.

@eskatos
Last active January 13, 2017 10:02
Show Gist options
  • Save eskatos/8c05edb1c2ae0bd20a490fefb9724c66 to your computer and use it in GitHub Desktop.
Save eskatos/8c05edb1c2ae0bd20a490fefb9724c66 to your computer and use it in GitHub Desktop.
osx available memory
#!/bin/bash
set -e
vmstat=$(vm_stat)
page_size=$(echo "$vmstat"|grep 'page size'|cut -d ' ' -f 8)
free_pages=$(echo "$vmstat"|awk '/^Pages free/ {print substr($3, 1, length($3)-1)}')
inactive_pages=$(echo "$vmstat"|awk '/^Pages inactive/ {print substr($3, 1, length($3)-1)}')
speculative_pages=$(echo "$vmstat"|awk '/^Pages speculative/ {print substr($3, 1, length($3)-1)}')
file_pages=$(echo "$vmstat"|awk '/^File-backed pages/ {print substr($3, 1, length($3)-1)}')
avail_file_caches=$(($page_size*($free_pages+$file_pages)))
avail_inactive=$(($page_size*($free_pages+$inactive_pages))) # vm_stat already substract available pages from free to display free
echo "AVAIL.MEM. (file caches) $(($avail_file_caches/1024/1024))MB"
echo "AVAIL.MEM. (inactive) $(($avail_inactive/1024/1024))MB"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment