Skip to main content
Inspiring
May 31, 2012
Question

Best practice for adding and removing eventListeners?

  • May 31, 2012
  • 4 replies
  • 1017 views

Hi,

What is the best practice in regards to CPU usage and performance for dealing with eventListeners when adding and removing movieclips to the stage?

1. Add the eventListeners when the mc is instantiated and leave them be until exiting the app

or

2. Add and remove the eventListeners as the mc is added or removed from the stage (via an addedToStage and removedFromStage listener method)

I would appreciate any thoughts you could share with me. Thanks!

JP

This topic has been closed for replies.

4 replies

May 31, 2012

Hi JP,

Its usually a best practice to remove the event listeners when they are not needed anymore.

You may refer to the following article for optimizing your AIR applications:

http://www.adobe.com/devnet/air/articles/10_tips_building_on_air.html

One more tip is to set useWeakReference parameter of addEventListener method to true. (by default, its false).

This way, your event listeners can be GC'd when not in use anymore.

-Neha

gtrAuthor
Inspiring
May 31, 2012

neh@adobe wrote:

This way, your event listeners can be GC'd when not in use anymore.

Thanks for your reply.

These movieclip are frequently used "pages" in the app, so the eventListeners would be called often. Memory is not an issue with this app (typically peaking at 3-4 MBs before garbage collection occurs), so I'm primarily concerned with CPU implications on leaving in vs. constantly adding and removing a list of eventListeners each time the mc page is addedToStage.

Inspiring
May 31, 2012

I've been using weak reference and then calling System.gc(); when I remove the movieclip.  It's been working great for me.