Our Flying Friends

Halle's latest posts

Low memory warning bombardment tool

25 April

This is an update on my old post here: Consider Yourself Warned.

I wanted to share a tool that I use in as part of checking an app for behaving correctly under low memory warnings, to the extent that is possible to do so using the Simulator. It’s an Applescript that just sends a low memory warning to the simulator every second or so, so you can go through every function of your app while debugging and see if a low memory warning ever causes a crash due to a required data structure not being available after a low memory warning. It will also smoke out some redraw bugs due to views being released, though for this it is less reliable than the device.


In order to use this tool, you need to turn go to Universal Access in the System Preferences and check “Enable access for assistive devices”, and if it doesn’t work immediately, you may need to open the script up in Applescript Editor (usually found in your Utilities folder) and make sure that the simulatorName variable actually corresponds to the name of your simulator app for the version of Xcode you use. You can also change the timer for how frequently simulated memory warnings are invoked.

This doesn’t tell you everything about how your app deals with memory on the device so it is not a substitute for functional device testing, but it will quickly show you if the app can be crashed on the simulator due to a missing data structure, which will often also happen on the device. It isn’t a panacea, just another tool to keep in the toolbox for catching some bugs easily.

If you are using a version of the simulator that has different titling in the menu (I don’t know if there are any around right now but that is likely in the future), you can change the strings in the script which refer to the menu element names.

When you quit the simulator, the script will quit. Since it is a loop that only exits when the simulator is quit, you may have to force quit it if you encounter some circumstance that I haven’t foreseen.

Here is the script uploaded as a zipped applet (you can still open it up in the script editor to edit it):

Politepix Memory Bombardment applet

If you don’t want to download a zipped applet, you can create your own Applescript applet from the following code:

# This requires going to Universal Access in the System Preferences and checking "Enable access for assistive devices".

# This will run until the iOS simulator is quit.

# Created by Halle Winkler, Politepix: https://www.politepix.com

#If you're using a different simulator version that is called something else, put its name here.
set simulatorName to "iOS Simulator"
# Change the .8 here to whatever interval you want to send simulated memory warnings at. These are seconds, so sleep 1 would mean that it repeats every second.
set repeatRate to "sleep .8"

# If the naming in the simulator menus is different for you, you can change them here

set menuName to "Hardware" # This is the main menu name
set menuItemName to "Simulate Memory Warning" # This is the name of the menu item

tell application simulatorName
end tell
	tell application "System Events"
		if application process simulatorName exists then
			tell application "System Events"
				tell process simulatorName
					tell menu bar 1
						tell menu bar item menuName
							tell menu menuName
								click menu item menuItemName
							end tell
						end tell
					end tell
				end tell
			end tell
			do shell script repeatRate
			exit repeat
		end if
	end tell
end repeat

Hope this is helpful to others.

Tags: , , ,