Personal tools

Views

Flex 3:Feature Introductions: Performance and Memory Profiling

From Adobe Labs

Back to Feature Introductions

Table of contents
[edit]

Introduction

One of the exciting new features of Flex Builder 3 is the Profiler. In this write-up, I'll be explaining how to get started with the Profiler and how to accomplish some common tasks. Along the way, I will also be pointing out some gotchas and bugs to watch out for.

[edit]

Getting Started

To get started with profiling, look for the profiler button image:Flex3FeatureIntroductions-Profiler-profile_exc.gif in the Flex Development perspective's toolbar:


image:Flex3FeatureIntroductions-Profiler-simpleButtonFlexDevelopmentPerspective.png


Clicking on the profiler button image:Flex3FeatureIntroductions-Profiler-profile_exc.gif, should launch the application using the default browser. You can configure profiling to use IE, Firefox, or the standalone Flash Player.


After launching, the Profiler will pause the application:

image:Flex3FeatureIntroductions-Profiler-simpleButtonApp.png

Then the Profiler will display a "Profiler Agent" configuration dialog:

image:Flex3FeatureIntroductions-Profiler-configurationDialog.png


In the configuration dialog, you should see four check boxes. The first, "Enable memory profiling" will cause the Profiler Agent to collection information each time an object is created and garbage collected. This information is useful when trying to find potential memory leaks and when trying to find excessive object creation.


The second check box, "Watch live memory data", instructs Flex Builder to display the "Live Objects" panel. This panel shows what classes have been instantiated, how many have been created, how many are in the heap, how much memory they have taken up and how much memory the active objects are taking up.


The third check box, "Generate object allocation stacktraces", will cause a stack trace to be captured each time a new object is created. This can cause profiling to run slower and use more memory, so we recommend using it only as needed.


The last check box, "Enable performance profiling" will cause the Profiler to collect a sample, essentially a stack trace, at regular intervals. These samples can be used to determine where the bulk of the execution time in your application is spent. Sampling allows you to profile without noticeably slowing down the application.



For now, we'll just leave the defaults and click "Resume".

[edit]

Profile Panel

This panel shows each of the applications currently being profiled and the status, Running or Terminated. It's possible to analyze exiting memory and performance snapshots after the application has been terminated.


image:Flex3FeatureIntroductions-Profiler-profilePanel.png


Here are the Profile panel's toolbar buttons and what they are used for:

[edit]

Live Objects Panel

This panel shows the class name and package name of each object which has been instantiated since the application started up. The package name will be empty for top level classes, like SimpleButton, and built in classes, like String and Class. Also shown is the cumulative instances, currently live instances, cumulative memory use, and current memory use. As objects are garbage collected the number of instances and memory use should go down, but the cumulative instances and cumulative memory use should continue to go up.


image:Flex3FeatureIntroductions-Profiler-liveObjects.png


Use the image:Flex3FeatureIntroductions-Profiler-filter_co.gif button to bring up the Filters dialog:


image:Flex3FeatureIntroductions-Profiler-filters.png


By default all Flash classes (top level classes, like String, and classes in the flash.* package) and Flex classes (classes in the mx.* package) are filtered out. I often find it useful to remove these filters. The Profiler will remember your choices, so you won't have to modify the filters the next time you run the application.

[edit]

Memory Snapshot Panel

To take a memory snapshot, select an application in the Profile panel:

image:Flex3FeatureIntroductions-Profiler-applicationSelection.png


Then click the memory snapshot button image:Flex3FeatureIntroductions-Profiler-memleakantype_obj.gif in the toolbar. After that you should see something like this:


image:Flex3FeatureIntroductions-Profiler-profilePanelMemorySnapshot.png

After that, double click on the memory snapshot node. This should bring up a Memory Snapshot panel:

image:Flex3FeatureIntroductions-Profiler-memorySnapshotPanel.png

[edit]

Loitering Objects Panel

To use the "Loitering Objects" panel, you need to start by taking an initial memory snapshot, see "Memory Snapshot" panel. After that you will want perform some actions, which you suspect might be causing a memory leak, in the application. Next, you need to take another memory snapshot. Here is what that should look like:


image:Flex3FeatureIntroductions-Profiler-twoMemorySnapshots.png


Next select the two memory snapshots by selecting one memory snapshot, then hold down the Ctrl key and select the other memory snapshots:


image:Flex3FeatureIntroductions-Profiler-twoSelectedMemorySnapshots.png


Then click on the "Loitering Objects" button image:Flex3FeatureIntroductions-Profiler-classinst_statistic_co.gif, which should have become enabled after selecting two memory snapshots. This should bring up the Loitering Objects panel:


image:Flex3FeatureIntroductions-Profiler-loiteringObjectsPanel.png


Rows in the "Loitering Objects" panel represent potential memory leaks. It's likely that some of the objects, which were created between the memory snapshots and which were not garbage collected, are expected. Any that aren't expected, are memory leaks. You'll have to investigate your application to determine which are unexpected.


[edit]

Object References Panel

Doubling clicking on a row in "Loitering Objects" panel should bring up the "Object Referencs" panel:


image:Flex3FeatureIntroductions-Profiler-simpleButtonAppObjectReferences.png


This panel is use to walk backwards up the heap graph to find the object or objects, which are keeping the leaked object from being garbage collected. The "Allocation Trace" side panel will only show allocation traces if they were enabled, see the "Configuration Dialog".

[edit]

Allocation Trace Panel

Clicking on the allocation trace button image:Flex3FeatureIntroductions-Profiler-stckframe_obj.gif brings up the Allocation Trace panel, which shows memory use by function:


image:Flex3FeatureIntroductions-Profiler-simpleButtonAppAllocationTracePanel.png


This panel is useful in determining which functions use the most memory. These functions are the places to look if you want to reduce memory use or excessive object creation.


Note the "Generate object allocation stacktraces" check box must be checked for this panel to function. See <A HREF="#Getting Started">Getting Started</A>.

[edit]

Performance Snapshot Panel

To take a performance snapshot, select an application in the Profile Panel:


image:Flex3FeatureIntroductions-Profiler-applicationSelection.png


Then click the memory snapshot button image:Flex3FeatureIntroductions-Profiler-exectimeantype_obj.gif in the toolbar. After that you should see something like this:


image:Flex3FeatureIntroductions-Profiler-simpleButtonAppPerformanceSnapshot.png


After that, double click on the performance snapshot node. This should bring up a Performance Snapshot Panel:


image:Flex3FeatureIntroductions-Profiler-simpleButtonAppPerformanceSnapshotPanel.png

[edit]

Method Statistics Panel

To open the Method Statistics Panel, double click on a row in the <A HREF="#Performance Snapshot Panel"/>Performance Snapshot Panel</A>:


image:Flex3FeatureIntroductions-Profiler-simpleButtonAppMethodStatisticsPanel.png


You can drill down on a function by double clicking on a row in the Callers or Callees table:


image:Flex3FeatureIntroductions-Profiler-simpleButtonAppMethodStatisticsPanel2.png


[edit]

Launch Dialog

The launch dialog can be used to profile applications outside Flex Builder. To open the Launch Dialog click on the menu icon image:Flex3FeatureIntroductions-Profiler-view_menu.gif and click on the Launch Dialog icon image:Flex3FeatureIntroductions-Profiler-profile_exc.gif. It should look like this:


image:Flex3FeatureIntroductions-Profiler-launchDialog.png


To launch an application, select it and click the "Launch" button. Alternatively, click the "Launch the application manually outside Flex Builder" check box. Then open the application using a browser or the standalone Flash Player manually.

[edit]

Player Launching

When profiling, the default for Flex Builder is to launch your application using the default browser:


image:Flex3FeatureIntroductions-Profiler-webBrowserPreferences.png


You can explicitly specify the standalone player or browser using the Flex Player/Browser preferences:


image:Flex3FeatureIntroductions-Profiler-playerBrowserPreferences.png


[edit]

Saving and Loading Profiling Data

New for beta 2, profiling data can be saved and reloaded. To save a profiling session use the image:Flex3FeatureIntroductions-Profiler-save.gif "Save" option in the image:Flex3FeatureIntroductions-Profiler-view_menu.gif menu:


image:Flex3FeatureIntroductions-Profiler-saveMenu.png


This should prompt for a directory. The profiling data will be saved as a set of data files, which include a string table file, a samples file, an info file, one counts file per performance snapshot, and one members file per memory snapshot. These files are written using Java object serialization and are not expected to be human readable.


To load a saved profiling session, select the "Saved Profiling Data" panel and click image:Flex3FeatureIntroductions-Profiler-folder.gif "Open". This will prompt for a directory where a profiling session has previously been saved. After loading a saved profiling session, existing memory and performance snapshots can be viewed, but new snapshots cannot be created.

[edit]

FAQ

1. What are the bracketed functions, like [reap], [newclass], [mouseEvent], [enterFrameEvent], etc?
These are synthetic functions that the Flash Player inserts into stack traces to help explain what is going on when AS3 code is not being executed.

2. What is global$init?
This is the script init which is code the compiler generates to define a class and add it to the global object.

3. Can the Profiler be used with Flex 2.0 and Flex 2.0.1 applications?
Yes.

4. Can the Profiler be used with Flash Authoring AS3 based applications?
Yes.

5. Can the Profiler be used with AS2 applications?
No.

6. Is Adobe AIR profiling supported?
New for beta 2, yes!

7. Is profiling production SWF's supported?
No. Only debug SWF's work with the Profiler.

8. Is profiling increase time precision planned?

[edit]

Known Issues

Back to Feature Introductions

Retrieved from "http://labs.adobe.com/wiki/index.php/Flex_3:Feature_Introductions:_Performance_and_Memory_Profiling"