There's still plenty more to cover when it comes to profiling. Keep an eye out for more profiling tutorials coming soon.

There's still plenty more to cover when it comes to profiling. Keep an eye out for more profiling tutorials coming soon.

Latest revision as of 12:12, 2 June 2012

Profiling is an important part of software development. The FDT Profiler helps developers detect performance issues within a Flash application. If you're creating a simple image carousel, a profiler is something that you may not need. If you're building an AIR application that will work on a computer kiosk, running for hours on end, a profiler will likely be a life saver.

There two different approaches to profiling, Memory Profiling and Performance Profiling. We'll go over these two separate modes in future walkthroughs. For now, we're going to focus on becoming familier with launching the profiler, exploring the Profiling Perspective, and have a small introduction to Memory Profiling.

How The Example Project Works

With our project setup and our Main.as class and Circle.as class setup. We'll quickly cover the code. Circle is just a class that draws a circle of random size and color. It's also located in the graphics.shapes namespace (package). Main is a class that creates circles and adds them to the stage. The only bit of code to note is this:

privatefunction draw_circles(event : Event) : void{// with no referece this won't be garbage collected
addChild(create_circle());
// hold a referce in array so it can be removed from stage and garbage collected
circles.unshift(create_circle());
addChild(circles[0]);
}

Here we're creating two circles - one is placed directly on the stage and the other is having a reference (pointer) stored into an Array so we can retrieve it later:

On MOUSE_UP, we're stopping the creation of new circles and each Circle in the circles array is being taken out of the array and then removed from the stage. This will remove any references to the circle we've created so it can be easily deleted from memory (garbage collected).

Running The SWF

Before profiling, lets just run the .SWF to see what's going on. This demo already has a launch configuration included , and to access this we'll jump to the Run Configurations window , select it and hit Run. When the window pops up , press down on the stage (MouseEvent.MOUSE_DOWN) to begin drawing circles and then release the mouse (MouseEvent.MOUSE_UP) to have half of them disappear .

The circles that disappeared will be eligible for garbage collection and the ones on the stage will not.

Launching The Profiler

Now, let's get to the profiler. Again, we have multiple ways of launching the profiler, but to make sure we're using the same launch profile, let's go to the profile menu and choose our 'ProfileTest' launch. FDT will compile and and launch the .SWF then switch to the Profile perspective . For now let's move off the External SWF Viewer and focus on the Profiler Perspective.

note: In this example, the views have been moved around to make best use of the space available. You can set it up however you like.

note: By default a .SWFs background will become transparent when loaded onto another .SWF. The profiler is actually loading your .SWF into another .SWF. That is why the background in this example appears with the default Flex background color. The background color you are seeing is the background color of the Profiler .SWF. If you are using the Flex Framework you will see the profiler .SWF launch and then your Flex App load on top of it- and your 'background' color would then be the color as defined by the Flex Application.

The Profiler Perspective

Memory Graph

The top part is the Memory Graph. This is a visual representation of the .SWF's memory footprint displayed over time. This is where you'll be looking to compare Peak Memory (a total of all Objects created) and Current Memory (Objects that are currently active in memory).

Live Objects

The next view is the LiveObjects view . This provides a breakdown of the objects created and how much memory that type of object takes up. In this example we can see that the String type takes up .31 percent of memory in our app.

Profile Monitor

The Profile Monitor is where we control the profile process . Use this when you want to force garbage collection , terminate or suspend (pause) the profiler . When using these controls, make sure you have the currently running process selected, otherwise you won't be able to control the active profiling session .

Console

The Console view here is the same as it exists elsewhere in FDT . It simply provides an output of FDT's actions.

Profiler In Action

Bring focus back to the running .SWF and press and hold the mouse button. As you do this, circles will begin appearing on the stage. You'll see the Memory Graph begin to climb and the Live2Objects view being updated . When the mouse is released, half of the circles will disappear. The objects that disappeared are now eligible for garbage collection. For the standard player release, Flash's garbage collection is designed to be automated and not to be user initiated. Since we are using the debug player, FDT has access to an API that allows developers to initiate garbage collection as they wish. To initiate garbage collection, press the Run Garbage Collection button .

Instantly, you'll see a dip in the Current Memory within the Memory Graph view and the Instances column within the LiveObjects view will drop . This drop represents the deletion of those circles we removed from the stage.

Filters

To get a better idea of what is going on, let's pause the execution of the .SWF and profiler by pressing the Suspend button . With everything paused, let's take a closer look at the filters that are available. Here we have two important filters: Filter internal packages and Filter native objects. To a get a better idea of what garbage collection did for us, click on the Filter native objects filter. Here we can see the objects we created, Main and Circle. Notice that there are 305 circles active compared to 611 that have been generated in total. Our ENTER_FRAME event handler was creating two objects total - one we explicitly generate a reference for and one that we anonymously add. Our MOUSE_UP handler is then removing the circles we created a reference for and leaving the others behind. That is why when the garbage collector executed it removed exactly half of the circles, 305, of the total 610.

Memory Snapshots

Sometimes it doesn't make sense to pause your .SWF to inspect it. That is what Memory Snapshots are for. If your .SWF is not already executing, resume it now. After minimizing the Console view, prepare to take a snapshot by selecting the running process and press the Create Memory Snapshot button .

note: this example was restarted to clean up the UI a bit so it's not going to match exactly previous screens;however, the steps to this point are the same.

FDT will then create a memory snapshot of the running .SWF . Let's wait a little bit and then hit the garbage collection button and wait for the memory to be cleared . After the memory has been cleared take another snapshot .

With both snapshots created, we can now take a closer look at what was going on at that moment and even compare the two. Initiate the Memory Snapshot view by double clicking on one of the snapshots . To clear things up a bit, let's use the Filter native objects filter on the snapshot . This snapshot now provides a detailed report of the status of the application at a specific time. We can dive even deeper and investigate the instances associated with that type by expanding the Class type . Clicking an a particular instance will then activate the Back References associated with it. The Allocation Trace is also available to us and provides further information about the running .SWF .

Another useful features regarding Snapshots is the ability to compare Snapshots. Do this by selecting the two available Snapshots and pressing the Compare Snapshot button . A new Compared Snapshot will be created . Double click on this and the Compare Snapshot view will open . Here Missing Objects and Additional Objects can be investigated. In our case, the Missing Objects are those that were deleted when we initiated garbage collection and the Additional Objects are the various events being dispatched by the mouse events and ENTER_FRAME event.