Robotlegs & Garbage Collection

Paul Cotterell's Avatar

Paul Cotterell

26 Jun, 2012 04:53 PM

Hello,

I have recently started a new project and decided to move over to using robotlegs instead of using Mate. This was due to the better handling of garbage collection in robotlegs.

I am however having a slight problem. When I switch states in my main component file, removing a now non-required component, the component still remains in memory.

Essentially, all I am doing is Injecting the view into the mediator, using the onRegister function to add an event listener to the view for a button click, firing that event to a command which changes the state of the main application to one where the non-required component is removed. This all happens successfully but the non-required component is still in memory.

Can anyone help me out with this?

Kind Regards
Paul Cotterell

  1. Support Staff 1 Posted by Shaun Smith on 26 Jun, 2012 05:06 PM

    Shaun Smith's Avatar

    Hello,

    Are you sure that GC has run? GC only runs when the Flash Player needs more memory than is currently allocated. You may find that the view component is destroyed quite some time after it is removed from stage.

    Robotlegs cleans up view and mediator reference automatically, so it's unlikely that the framework is holding on to your component. Are you using a profiler to observe the component reference count?

  2. 2 Posted by Paul Cotterell on 26 Jun, 2012 06:08 PM

    Paul Cotterell's Avatar

    Hi Shaun,

    Thanks for the reply.

    Yes, the GC has run because I am manually running it from the profiler.

    If I remove the mediator mapping from the context and then switch the state manually, it gets removed from memory when I run GC.

    It is very odd because there should be absolutely no references keeping it in memory. What I will do is make an extremely simple application and see if the issue is still happening with just a basic example. If it is, then I will post the project on here and see if other people experience it.

  3. Support Staff 3 Posted by Shaun Smith on 26 Jun, 2012 07:33 PM

    Shaun Smith's Avatar

    Howdy,

    Just double checking: are you running RL1 or RL2?

  4. 4 Posted by Paul Cotterell on 26 Jun, 2012 08:51 PM

    Paul Cotterell's Avatar

    I am running 1.5.2

  5. Support Staff 5 Posted by Ondina D.F. on 28 Jun, 2012 09:12 AM

    Ondina D.F.'s Avatar

    What I will do is make an extremely simple application and see if the issue is still happening with just a basic example. If it is, then I will post the project on here and see if other people experience it.

    Are you still having problems? If the answer is yes, you’d better attach your project or a simplified example reproducing the issue. I’ll take a look at it.
    If you solved the problem, posting your solution may help others struggling with similar problems :)

    As Shaun said: „GC only runs when the Flash Player needs more memory than is currently allocated.“
    Have you tried to force the gc: “Run Garbage Collector” in the profiler? Or inside your code, using System.gc() twice?

    There are a couple of things that could prevent your View from being gc-ed.
    In a way or another, something is holding a reference to your view or one of its subcomponents.

    A few questions for you:

    • Are you adding event listeners to the view or subcomponents directly instead of using the eventMap?

    • Are you setting useWeakReference to false when you map the events?

    • Inside your View, are you using one of the following?

    • setting buttonMode=true

    • setting focusEnabled=true

    • setting a styleName

    • using item renderers

    • Is it a Flex or a pure as3 project?

    • Is it a mobile application?

    Without seeing the code, it’s hard to say if the issue you’re encountering is caused by something listed above or if it is something entirely different, and I would just keep guessing.

    Ondina

  6. 6 Posted by Paul Cotterell on 29 Jun, 2012 09:59 AM

    Paul Cotterell's Avatar

    Hi,

    Sorry for the delay, I have attached a small example project that has been tested using the profiler and there is definitely no garbage collection. I ran the garbage collector manually and the memory didn't reduce at all.

    The application attached, when run, displays an image, click the image and it moves to a state where the image component is removed.

    Let me know how you get on.

    Kins Regards
    Paul

  7. Support Staff 7 Posted by Ondina D.F. on 29 Jun, 2012 11:49 AM

    Ondina D.F.'s Avatar

    Hi Paul,

    I looked at your code.
    In MainUI.mxml you have this:

    <views:MiloUI includeIn="base"/>

    This will keep your component alive.

    If you really want to destroy your component after changing states, you can either
    set itemDestructionPolicy to auto:

    <views:MiloUI includeIn="base" itemDestructionPolicy="auto"/>

    or
    you’ll have to add it through actionscript instead of mxml, and then remove it manually when the state changes.

    From Adobe’s documentation:
    Controlling caching of objects created in a view state
    „A change to a view state can cause Flex to create an object. By default, after Flex creates the object, the object is cached indefinitely, even after you switch to another view state. The item is cached even if the destination view state excludes the object.
    Use the itemDestructionPolicy attribute with the includeIn and excludeFrom attributes configure Flex to completely destroy the object when leaving a view state, including deleting it from the cache. Typically it is more efficient to allow items to be cached because it improves performance when switching back to the view state that created the object, or to a view state that uses the object. However your application might define a view state that is rarely used, and you do not want to allocate memory for caching the object.
    You can use the itemDestructionPolicy attribute on any object in MXML that supports the includeIn and excludeFrom attributes. The possible values for itemDestructionPolicy are never (default) and auto.
    The value of never specifies that the object is cached indefinitely. A value of auto means that the object is destroyed when leaving a view state where the object exists.“

    Please let us know if setting itemDestructionPolicy="auto" solved your problem.

    Cheers,
    Ondina

  8. 8 Posted by Paul Cotterell on 29 Jun, 2012 02:05 PM

    Paul Cotterell's Avatar

    Hi Ondina,

    That worked perfectly.

    Thanks very much for your help on this.

    Kind Regards
    Paul

  9. Support Staff 9 Posted by Ondina D.F. on 29 Jun, 2012 02:58 PM

    Ondina D.F.'s Avatar

    Glad to hear it worked out well. You’re more than welcome, Paul!

  10. Ondina D.F. closed this discussion on 29 Jun, 2012 02:58 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac