Macとかの雑記帳

非使用メモリを解放する「purge」を使ったシンプルなスクリプト

photo credit: fotoamater.com via photopin cc

非使用中のメモリが指定した数値より大きいときだけ$ purgeを実行し、結果を表示するスクリプトです。purgeを実行しても何も表示せず味気ないので、普段これを使ってます。どこで拾ったかは忘れました。

#!/bin/bash

# This script checks the available inactive memory.
# Memory is purged if the available number of MB is
# greater than the following "msize" variable. Attach
# this script to launchd to run it periodically.

msize=1200

MM=`vm_stat | awk '/Pages\ inactive\:/ {print int($3/256)}'`

echo "Testing status of inactive free memory..."

if [ "$MM" -gt "$msize" ]; then
    echo "You have too much inactive free memory. ${MM}MB Releasing now..."
    purge
else
    echo "Memory amount ${MM}MB does not meet purge threshold."
fi

https://gist.github.com/3796683#file_gistfile1.sh

8行目のmsizeでpurgeを実行する "非使用中メモリ" の最低値を指定します。単位は MB です。上の場合、非使用メモリが1200MB以上ならpurgeを実行します。

purge 01

スポンサード リンク