I've been getting lots of questions about memory leaks lately. I finally found some time to put my techniques for analyzing memory leaks into writing.
Instead of using PowerPoint, I actually created a SWF so you can use the SWF to both view the presentation and learn how to use the Profiler. I cover the differences between profiler memory, System.totalMemory and process memory and show how I analyze the two most common memory leak scenarios.
As usual, caveats apply.
The SWF also demonstrates an approach to another topic I've heard about often, and that is how to do XML-driven or data-driven UI. The presentation in this SWF is controlled by an XML file. A single engine parses the XML and creates the UI widgets as dictated by the XML. To change the presentation, all I have to do is change the XML. I can also add new widgets easily. Source is available here:
The SWF also demonstrates a technique for improving startup time. Because our blog system is painful to use, I don't like uploading more than one or two files per blog post, but this presentation had more than a dozen images. So, instead of loading images externally, I embedded them into the SWF, but normally, that delays startup by the download time of all of those images. To get around that, I pack all the images into frame 3 of the SWF so that Flex can get up and running while the images download at the end of the SWF. I can get away with that because the images are not needed right away.
Hope this helps.

An excellent resource about memory profile, thanks Alex!!
You rock Alex! :) Thanks for providing such good info all the time!
I always wanted to learn more about the profiler, thank you Alex !
Maybe a little off topic but I would like to know where to get profiler version of FP 9.0.124 (it exists?) ? I actually have Socket Timeout issue with normal 9.0.124.
------------------------
Alex responds:
The debugger player for any FP should support the Profiler. I'm using .124 w/o problems
I'm working on a flex app that is using BlazeDS to call some EJBs on the backend. As such I really need my swf to run in the context of an application server so the EJB stuff works correctly.
Do you know if the profiler supports this? All I can figure out how to make it do is profile my swf in a standalone mode, which doesn't really let me do anything useful.
Your tutorial is great though, I like that it lets me click through at my own speed, all the other tutorials I found are text only w/ no pictures, or are long movies with very poor seeking control.
---------------------------
Alex responds:
"in context" as in the ability to profile the EJB's response to a request from the SWF and vice-versa? I'm not sure what data you are looking for?
Hello Alex!
I am afraid you are the only one on the earth who explained the difference between the memory shown in system.totalmemory and the numbers shown in windows task manager! Thanks so much!!
I have two questions not sure if you have any idea.
1) system.totalMemory shows very small values in IE7 as compared to firefox, is this a known bug?
2) Consistently, some of my swf can have 1.7GB shown in the system.totalMemory but only 58MB shown in the windows task manager. This is terrible, what might be the cause for this?
----------------------
Alex responds:
IE7 will show different numbers than FF, but it shouldn't vary that widely. If you have a simple way to reproduce the problem, feel free to file a bug.
One of my colleagues is no longer trusting Task Manager and so we're now using ProcessMonitor and private bytes. See what you get for numbers with ProcMon
Thanks for the great presentation!
i am still looking for some further details about garbage collection and the flashplayer internal actions ([keyboardEvent], [pre-render]) shown up in the profiler. are there any further online resources about these topics?
i know already these resources:
http://livedocs.adobe.com/flex/3/html/help.html?content=profiler_3.html
https://developer.mozilla.org/en/MMgc
one question to gc:
the gc uses besides mark and sweep the drc (deferred reference counter). so when i nullify an object which i dont need anymore, the reference counter should be updated and if it gets the value 0 the object is free for gc. so would it help for performance to nullify all unused objects (of course thats a boring piece of work)? i guess the mark and sweep will have less work as it should ignore objects already updated by reference counter to value 0.
-----------------
Alex responds:
I don't know of any official documentation on the names wrapped in [] in a profiler report. Note that the player is not written AS so these [] names are more like "phases" or "states" that it is in.
I also don't know which objects are guaranteed to be DRC objects so I don't normally write code that makes such assumptions and starts nulling things to try to get the refcount to zero sooner. So far, just treating everything like objects in a mark/sweep gc has worked for me.
Hi Team
When i start the profiler i get the below error
1.I am using FB 3.
and flash player 10.
Any pointers are highly appreciated.
ReferenceError: Error #1065: Variable ContextMenuClipboardItems is not
defined.
at flash.ui::ContextMenu/initLinkAndClipboardProperties()
at flash.ui::ContextMenu()
at
mx.core::Application/initContextMenu()[C:\autobuild\3.2.0\frameworks\pro\
jects\framework\src\mx\core\Application.as:1154]
at
mx.core::Application/initialize()[C:\autobuild\3.2.0\frameworks\projects\
\framework\src\mx\core\Application.as:844]
at
grip/initialize()[D:\eclipse\grip-app-client\grip\src\grip.mxml:0]
at
mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::c\
hildAdded()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\mana\
gers\SystemManager.as:2009]
at
mx.managers::SystemManager/initializeTopLevelWindow()[C:\autobuild\3.2.0\
\frameworks\projects\framework\src\mx\managers\SystemManager.as:3234]
at
mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::d\
ocFrameHandler()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\
\managers\SystemManager.as:3064]
at
mx.managers::SystemManager/docFrameListener()[C:\autobuild\3.2.0\framewo\
rks\projects\framework\src\mx\managers\SystemManager.as:2916]
Thanks
ilikeflex
-----------------------------
Alex responds:
You must use Flash Builder 4 to profile Flash Player 10 SWFs
Alex
thanks for this great information.
The sample code seems to be lacking a reference to Frame3Assets in ProfilerScenarios.mxml. Without it the images would not show.
I added
private var assets:Frame3Assets = new Frame3Assets();
and it works.
I'd like to understand why you inherited Frame3Assets from Sprite. I removed the base class and it still works. Any reason?
Thanks
Stefan
Hi Alex,
- The presentation was very informative. Why does not Adobe include information on memory management [including the info that you put up in one of your ppt's on memory in flash ] in flex/flash official guides/documentation ? I don't see much sense in Adobe leaving out all of this for developers to figure out while obviously they have no clue about the internals of the flash player !
- The whole exercise of taking snapshots and looking at loitering objects in the profiler seems a bit deceptive and here's why. When you take a memory snapshot what's actually going on is that the flex builder runs the gc before taking the snapshot. So in reality the loitering objects view shows those objects that weren't GCed when gc was 'forced' and not invoked by the flash player in a natural setting. So some of the objects that didn't show up in the loitering objects view since they were gced would actually persist in real time and those could be the major killers.
Club with this the fact that flex 3x has gc officially available. So is Adobe expecting developers to keep invoking gc at intervals ? Otherwise what profiler is doing seems misleading.
The article/source explains how to use compiler options to put the Frame3Assets in a third frame. It should not be directly referenced by the source code. It is a technique that improves download/startup time. Sprite or MovieClips are required as the root class of any frame.
If forced gc causes an object to go away, then it would eventually go away. This is generally not an issue unless you are creating so many objects that it takes a long time for gc to catch up and you end up with lots of fragmented memory. But the basic principle (try to re-use no re-create) will help in such a situation.
hey Alex it's really something. always wanted to know more about profiler. awesome presentation.
Thanks for the great presentation of flex builder.
but I'am looking for flash player internal action pre-render. can you advise?
[pre-render] in the profiler is part of the rendering process. It and [render] is the amount of time the player spent drawing all of the display objects. Essentially, [enterFrameEvent] causes your code to run then the player does a [pre-render] then a [render] then [enterFrameEvent] again unless there are mouse events or timer events to handle
If [pre-render] and/or [render] are big, then you have too many display objects and need to try to consolidate or have them do less fancy things or have fewer graphics instructions
Check this blog out for the socket timeout problem: http://wceii.blogspot.com/2009/12/flex-profiler-socket-timeout-error.html