Debugging FLEX/AS3 memory leaks
Asked Answered
C

4

6

I have a pretty big Flex & Papervision3D application that creates and destroys objects continually. It also loads and unloads SWF resource files too. While it's running the SWF slowly consumes memory til about 2GB when it croaks the player. Obviously I am pretty sure I let go of reference to instances I no longer want with expectation the GC will do its job. But I am having a heck of a time figuring out where the problem lies.

I've tried using the profiler and its options for capturing memory snapshots, etc - but my problem remains evasive. I think there are known problems using debug Flash player also? But I get no joy using the release version either.

How do you go about tracking down memory leak problems using FLEX/AS3 ? What are some strategies, tricks, or tools you have used to locate consumption

Coze answered 20/3, 2009 at 5:41 Comment(0)
C
3

I stumbled across something explaining how to use Flex Profiler in Flex Builder and it was a HUGE help to me in debugging memory leaks. I would definitely suggest trying it out. It's very easy to use. Some things I found when profiling my applications:

Avoid using collections (at least LARGE collections) as properties of Value Objects. I had several types of Value Object Classes in my Cairngorm application, and each had a "children" property which was an ArrayCollection, and was used for filtering. When profiling, I found that these were one of my biggest memory eaters, so I changed my application to instead store the "parentId" as an int and use this for filtering. The memory used was cut drastically. Something like this:

Old way:

public class Owner1
{
    public var id:int;
    public var label:String;
    public var children:ArrayCollection; // Stores any number of Owner2 Objects
}

public class Owner2
{
    public var id:int;
    public var label:String;
    public var children:ArrayCollection; // Stores any number of Owner3 Objects
}

public class Owner3
{
    public var id:int;
    public var label:String;
}

New Way:

public class Owner1
{
    public var id:int;
    public var label:String;
}

public class Owner2
{
    public var id:int;
    public var label:String;
    public var parentId:int; // Refers to id of Owner1 Object
}

public class Owner3
{
    public var id:int;
    public var label:String;
    public var parentId:int; // Refers to id of Owner2 Object
}

I would also suggest removing event listeners when they are no longer needed.

Conscription answered 20/3, 2009 at 13:31 Comment(0)
C
4

I usually implement a cleanup method in every class I make (since AS doesn't have destructors). The main problem I've noticed with the GC is with event listeners. Additional to what dirkgently said, also try to avoid anonymous listener functions (as you can't explicitly remove them). Here are a few links you may find useful:

Chromo answered 20/3, 2009 at 7:21 Comment(1)
adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/…Determiner
C
3

I stumbled across something explaining how to use Flex Profiler in Flex Builder and it was a HUGE help to me in debugging memory leaks. I would definitely suggest trying it out. It's very easy to use. Some things I found when profiling my applications:

Avoid using collections (at least LARGE collections) as properties of Value Objects. I had several types of Value Object Classes in my Cairngorm application, and each had a "children" property which was an ArrayCollection, and was used for filtering. When profiling, I found that these were one of my biggest memory eaters, so I changed my application to instead store the "parentId" as an int and use this for filtering. The memory used was cut drastically. Something like this:

Old way:

public class Owner1
{
    public var id:int;
    public var label:String;
    public var children:ArrayCollection; // Stores any number of Owner2 Objects
}

public class Owner2
{
    public var id:int;
    public var label:String;
    public var children:ArrayCollection; // Stores any number of Owner3 Objects
}

public class Owner3
{
    public var id:int;
    public var label:String;
}

New Way:

public class Owner1
{
    public var id:int;
    public var label:String;
}

public class Owner2
{
    public var id:int;
    public var label:String;
    public var parentId:int; // Refers to id of Owner1 Object
}

public class Owner3
{
    public var id:int;
    public var label:String;
    public var parentId:int; // Refers to id of Owner2 Object
}

I would also suggest removing event listeners when they are no longer needed.

Conscription answered 20/3, 2009 at 13:31 Comment(0)
Y
3

because of issues like this I've developed a open source library that helps monitor all the events your running at any given time. its really easy to implement and i've re-factored projects in 10-15 minutes converting them to use the EventController i've developed.

basically for your scenario i would run through all the events and replace them from: obj.addEventListener(...);

to : EC.add(obj,...);

the rest is the same what that would do is register the event and make it crazy easy to see all your events at any point you want using the EC.log();

all the details and documentation are on my site i would love to know if this helps you and if you start working with it. if you have any feedback good or bad please feel free to post it up and i would look into it!

the site is: http://fla.as/ec/

Yellowhammer answered 18/3, 2010 at 19:28 Comment(0)
T
2

If your memory leak grows exponentially, it probably means GC is failing to do its job. Take a look at your code and see wherever you can decrease your objects' reference counts (by setting them to null). Make event-handlers weak. And re-profile.

Trillbee answered 20/3, 2009 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.