Skip to content

Instantly share code, notes, and snippets.

@geoffrepoli
Created February 19, 2020 12:34
Show Gist options
  • Save geoffrepoli/11398a2451327b5fee35cab6ba8588bc to your computer and use it in GitHub Desktop.
Save geoffrepoli/11398a2451327b5fee35cab6ba8588bc to your computer and use it in GitHub Desktop.
Total free and used memory
#!/bin/bash
# Free memory
memory_free=$(vm_stat | awk '/Pages free/{ print $NF }' | sed 's/.$//')
memory_speculative=$(vm_stat | awk '/speculative/{ print $NF }' | sed 's/.$//')
memory_inactive=$(vm_stat | awk '/Pages inactive/{ print $NF }' | sed 's/.$//')
# Used memory
memory_app=$(( $(sysctl -n vm.page_pageable_internal_count) - $(vm_stat | awk '/Pages purgeable/{ print $NF }' | sed 's/.$//') ))
memory_wired=$(vm_stat | awk '/Pages wired/{ print $NF }' | sed 's/.$//')
memory_occupied=$(vm_stat | awk '/Pages occupied/{ print $NF }' | sed 's/.$//')
total_free=$(( ( memory_free + memory_speculative + memory_inactive ) * 4096 / 1048576 ))
total_used=$(( ( memory_app + memory_wired + memory_occupied ) * 4096 / 1048576 ))
echo $total_free MB
echo $total_used MB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment