MacOS – Reboot an Application when it Takes too Much Memory

applicationsmacosmemory

Some applications start off with small memory footprints, but slowly grow to take up way too much RAM. Is there a way I can automatically detect when an app reaches a certain threshold, and then kill and relaunch the app?

I know that I can see the memory footprint on Activity Monitor, but that doesn't seem to be AppleScript-able. I can also see an application's memory footprint by using ps aux, but its numbers don't seem to match up with Activity Monitor's.

I'd appreciate any help with this

Best Answer

You can try cleanMyMac. It will show you available ram and allow you to clear it. You could also create a cron job that executes sudo purge to try to fix some of these issues. (though I know that solution isn't without its detractors and skeptics)

In my opinion, Applescript itself is a huge memory hog.

I've been trying to OCR a large number of documents via an Applescript that calls Abbyy Fine Reader. It will run for about an hour and then it crashes the mac (just prior I get an error saying all available application memory has been used).

I initially thought it was an issue with Abbyy and the size of the documents. But it turns out the problem is actually applescript. The My system has 16GB of memory. But Applescript would chew through that in less than an hour with this document dump.

I even tried hacking things by creating a cronjob that executes sudo purge every couple of minutes. That helped. But ultimately not a whole lot. The only thing that freed up the memory situation was to exit from Applescript.

All this led us to switch to controlling Mac applications with ruby using the rb-scpt ruby-gem. (That gem allows us to control any applescript scriptable application via ruby).

https://rubygems.org/gems/rb-scpt/versions/1.0.1

The ruby script solution fixed the problem of repeated crashing due to lack of available memory. I've been processing these docs for days (not hours) without a single problem. My available memory hasn't budged since I started the script. (ram monitored by CleanMyMac)

Applescript is fine for small jobs. But it simply isn't up to the task of doing serious work.