Applescript to monitor Roon Server RAM usage

Whenever Roon Server’s RAM usage goes above 5GB (or thereabouts) I start to experience laggy behaviour in the Roon client so wrote a script to check RAM usage and restart Roon Server when it exceeds 5GB. I don’t know if anyone else will find this useful, but have included it below. It works for me, but I haven’t checked it on any other systems, so YMMV.

set pid to do shell script "ps -A | grep RoonAppliance | grep -v grep | awk '{print $1}'"
set VMemRS to do shell script ("vmmap " & pid & "| grep 'written=' -A 1")
set relevant_bit1 to offset of "written=" in VMemRS
set the_starting_point to relevant_bit1 + 8
set relevant_bit2 to offset of "(" in VMemRS
set the_ending_point to relevant_bit2 - 2
set RAM_usage to characters the_starting_point thru the_ending_point of VMemRS as string
if RAM_usage > 5 then
	try
		tell application "RoonServer" to quit
		delay 10
		tell application "RoonServer" to activate
		set theDialogText to "RAM Usage was " & RAM_usage & " GB when Roon Server was restarted."
		display alert theDialogText
	end try
end if

I have this embedded in another (constantly running) script, that runs the check at 5.30 in the morning.

A shorter version, which you can use to just get the info on how much RAM is being used, is as follows. You can save this as an AppleScript app or assign it to a key sequence (using something like Better Touch Tool).

set pid to do shell script "ps -A | grep RoonAppliance | grep -v grep | awk '{print $1}'"
set VMemRS to do shell script ("vmmap " & pid & "| grep 'written=' -A 1")
set relevant_bit1 to offset of "written=" in VMemRS
set the_starting_point to relevant_bit1 + 8
set relevant_bit2 to offset of "(" in VMemRS
set the_ending_point to relevant_bit2 - 2
set RAM_usage to characters the_starting_point thru the_ending_point of VMemRS as string
set theText to "Roon Server is currenty using  " & RAM_usage & " GB of RAM"
display alert theText
5 Likes

It turns out that vmmap reports memory usage a bit differently to Activity Monitor so I’ve changed the script to take account of this by summing ‘resident’ memory with ‘swapped_out’. This produces an almost identical result to Activity Monitor, as follows:

set pid to do shell script "ps -A | grep RoonAppliance | grep -v grep | awk '{print $1}'"
set VMemRS to do shell script ("vmmap " & pid & "| grep 'written=' -A 1")
set relevant_bit1 to offset of "resident=" in VMemRS
set the_starting_point to relevant_bit1 + 9
set relevant_bit2 to offset of "swapped_out" in VMemRS
set the_ending_point to relevant_bit2 - 8
set RAM_usage1 to characters the_starting_point thru the_ending_point of VMemRS as string
set relevant_bit3 to offset of "_out=" in VMemRS
set the_starting_point to relevant_bit3 + 5
set relevant_bit4 to offset of "unallocated" in VMemRS
set the_ending_point to relevant_bit4 - 8
set RAM_usage2 to characters the_starting_point thru the_ending_point of VMemRS as string
set o to (offset of "." in RAM_usage2)
if ((o > 0) and (0.0 as text is "0,0")) then set RAM_usage2 to (text 1 thru (o - 1) of RAM_usage2 & "," & text (o + 1) thru -1 of RAM_usage2)
set RAM_usage2 to RAM_usage2 as number
if RAM_usage2 > 9 then
	set RAM_usage2 to 1
end if
set RAM_usage to RAM_usage1 + RAM_usage2
set theText to "Roon Server: using " & RAM_usage & " GB RAM"
display alert theText