Skip to main content
Anonymous507
Known Participant
August 8, 2014
Answered

More use of objects leads to performance issue?

  • August 8, 2014
  • 2 replies
  • 354 views

hi

I am developing a game using MVC. In which i need to send mulitple parameter to the addEventListener. So i have created a customEvent class with a parameter as object type.

Below is code flow

my custom Event class:

public function CustomEvent(type:String, customData:Object = null,bubbles:Boolean=false,cancelable:Boolean=false) {

     //constructor

     super(type,bubbles,cancelable);

     this.customData = customData;

}

Adding listener

_myController.addEventListener(CustomEvent.UPDATE_MY_METHOD, doSomething);

function doSomething(myObj:Object){

     var param1 = myObj.param1;

     var param2 = myObj.param2 ;

     var param3 = myObj.param2 ;

}

To call my custom event.

var dispObj = new Object();

dispObj.param1 = "ONE";

dispObj.param2 = 100;

dispObj.param3 = "PLAYER";

dispatchEvent(new CustomEvent(CustomEvent.UPDATE_MY_METHOD, dispObj);

This idea makes me to use more objects in my game.


Now my doubt is, whether this will leads to memory leakage or any other performance issue? If it is can I replace dictionary object or any other best option.

Any suggestions? Please.

Thanks in advance

This topic has been closed for replies.
Correct answer kglad

there won't be a memory issue because those customevent's will get gc'd.  but there is more work for the flash player because there's more to gc.

on the other hand, you have to use something to communicate needed parameters so i don't see a benefit to using getters or other public functions that allow parameter values to be retrieved.

2 replies

Amy Blankenship
Legend
August 8, 2014

You actually can get a performance issue because of creating so many objects that subsequently need to be garbage collected. However, you'd need to be creating huge numbers of them. Here are some alternatives:

  • Create an object pool of the Objects you are using, and reuse them. This has a couple of issues, namely that your objects may wind up with old data sticking out here and there. Another problem is that providing a reference to the object pool to listening objects that might want to return the objects to the ppol may well defeat the MVC design. Note that you can't reuse Events, so don't bother trying to pool them.
  • You can check to see if anyone is listening before creating the object and dipatching the event. This won't work for events dispatched on the display list, because someone could be listening to a parent or using useCapture.
  • Switch to using callbacks. This tends to increase coupling, but you can try using As3Signals, which some people feel reduces coupling while also getting the benefits of callbacks.
kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
August 8, 2014

there won't be a memory issue because those customevent's will get gc'd.  but there is more work for the flash player because there's more to gc.

on the other hand, you have to use something to communicate needed parameters so i don't see a benefit to using getters or other public functions that allow parameter values to be retrieved.