The Amethyst 2 Profiler
Profiling a Flash application allows you to see which functions are consuming most CPU and, in addition, detect memory leaks. Memory leaks can be quite insidious in Flash as it’s quite easy to allocate, say, an event handler and forget about it
In Amethyst 2, we’ve added a powerful profiling tool that not only allows you to see the amount of memory being used but to track down memory leaks easily. So, to demonstrate this, I’ve created some code that clearly has a memory leak. In the code below, every time a new C4 object is created, a 4MB chunk of memory is allocated and never freed.
Every time a button is clicked a new C4 object is created:
To demonstrate this, I just start the profiler from the Visual Studio Amethyst menu:
The project is built (if required) and launched. After clicking the button a few times, the CPU table looks this this:
And you can see that the C4 constructor and the writeInt method are high on the list. Next, let’s have a look at the Objects table. This gives a view of how many objects of a given class were allocated and how much memory was allocated. Here, you can see that the ByteArray class is the main culprit.
After a few dozen clicks, you can clearly see that we’ve got a problem as shown by the Memory Graph:
But to track this down, we need another tool – the Data Snapshot. A ‘snapshot’ grabs data on all the objects allocated in the Player at a given time. And I do mean all – which makes a snapshot by itself difficult to work with. There’s simply too much data. You can filter out some of the data using the Amethyst profiler ‘filter’ (of which more later). But a much better way to track down memory leaks is to take another snapshot and difference them after some action, such as clicking a button.
Here’s the result of taking another snapshot after two button clicks, and then differencing the two snapshots. You can see the two objects that are new in the second snapshot in the Delta snapshot. Clicking on one of these objects in the tree displays the call stack, and this can then be used to navigate to the source code via a context menu as show here.
The data can be controlled by using a set of ‘filters’. Using these, you can include, for example, all objects starting with flash.utils and exclude everything starting with spark. If you update a filter, it is immediately applied to the incoming data from the target SWF being profiled – you don’t need to restart the profiler.
The filters and other Profiler parameters, such as the memory sampling interval for the graph are accessed and set via a standard Visual Studio property page. These are persisted in the project file for use next time.
This is a preview of the forthcoming version of Amethyst 2 – our ActionScript/Flash IDE for Visual Studio 2012 and 2010. If you buy Amethyst 1.0 for Visual Studio 2010 now you will be entitled to a free update to Amethyst 2 when it is released. See the Amethyst 2012 announcement for more information.