0

Riddle me this: I have my MouseEvent.MOUSE_DOWN which calls mouseHandler. The latter looks something like this:

public function mouseHandler(evt:MouseEvent):void
{

    var p:Point = new Point(mouseX, mouseY);

    var objs:Array = new Array(getObjectsUnderPoint(p));

}

Now what I want to know is thusly: will the objs array and p point be overwritten every time, simply causing the previous objs array and p point to be wiped and a new one generated, or...does it just create a new array and point over and over and over? Of course if I trace(objs) it gives me the expected results, but am I chocking up the system in the background without realising? Your expertise would be appreciated.

EDIT: well after learning a fair bit from the answerers -thanks btw- that made me think of something else and, a quick search later, found a way to theoretically reliably remove the references:

var a = null;

Now I appreciate that this is probably not needed as they'll be GCed at the end of the function, but considering it takes two ticks to write, better safe than sorry, no?

2 Answers2

0

Since p is an object, not a primitive, the memory allocation for it will come from the Heap (and therefore need to be garbage collected) instead of the stack like local primitives (and are wiped once the function ends.

So new memory will be allocated every time this function is called, which will temporarily increase memory usage until they get GC'd, but the amount is probably insignificant (assuming that array isn't massive!).

VBCPP
  • 152
  • 1
  • 10
  • So...apologies for the ignorance, but I'm assuming the garbage collection happens automatically every time the code is run, right? Or you mean via removeMouseEventListener? And thankfully no, lol there aren't that many display objects under the pointer! – ReaperOscuro Jun 01 '12 at 11:48
  • The GC happens... whenever the flash player wants it to happen. It is out of your control and should be considered at best case random and at worst case during the least opportune moment. Whenever it does decide to run, it will notice your point and array objects are no longer accessible and will get rid of it for ya. – VBCPP Jun 01 '12 at 12:08
  • Lol of course, it's never that easy is it! I'm checking with Lukasz above if using removeMouseEventListener would cause the references to be GC -hope tells me yes... – ReaperOscuro Jun 01 '12 at 12:24
0

It works like this from what I understand - references for these variables will be lost after function will end. The values will be GC'ed then eventually.

If you will call the function second time, the new references will be created, and filled with new data, the values of previous references hovewer, may still exist in memory, and wait for Garbage Collector, but that may not necessarily happen! If they have attached listeners, the Flash Player will think that they are still useful, so they will exist in the memory to the end of the application run and listen, and even react to events - even if you theoretically cannot access them anymore.

Edit: In your case whats happening is that you are creating two references that will disappear at the end of the function. They are not related to event listener, the listener creates them, but is not attached to them so it won't stop GC from collecting them after function will end. You creates an array of references, but that are just references to other objects that values are referred in other places (like the display list), if the array alone is not referred anywhere outside of this function, it should be GCted.

Łukasz Zaroda
  • 869
  • 2
  • 19
  • 55
  • Hhmmm....then the fact that I regularly invoke the removeMouseEventListener so that unnecessary listeners are removed once utilised should thus GC the variables, right? – ReaperOscuro Jun 01 '12 at 12:06
  • If they have event listeners, they are connected to the whole "event system", thus the system have references to them. Similar situation is with adding object to display list (as it is also gluing it to something - in this case a display list) . If you will remove these dependencies and then remove references from code - their values should be available for GC. – Łukasz Zaroda Jun 01 '12 at 12:17
  • Thanks Lukasz -so the dependencies are removed, I get that, but what about the references: are these removed when the dependencies are, or does it need my manual intervention? – ReaperOscuro Jun 01 '12 at 12:23
  • Reference have its "range", it can be a function or bigger object where they are nested, if that object will end its life, the references will be destroyed. If you declared a variable inside of the function, the reference will disappear at the end of the function, if you declared a variable in the object (as private, public etc), the reference to the variable will exist as long as object have it's own references. It's like a tree. If the object have variables, and object is nulled, and these variables have no other references, the whole thing will be GC'ed. – Łukasz Zaroda Jun 01 '12 at 12:30
  • GC is just searching what to "cut" from the tree of dependencies. If the branch have no references and dependecies it will be GCed. – Łukasz Zaroda Jun 01 '12 at 12:32
  • So as you can see it's hard to tell if removing listeners will ensure GC, as it depends on the whole structure of references also. – Łukasz Zaroda Jun 01 '12 at 12:34
  • There is an exception to the rule. "Closures" They are very hard to get the GC to kick in on them. Mostly the only reason to use a Closure would be so it doesn't get GCed. If you don't know what one is I would suggest a google search. If at the least it will be some good reading for you. – The_asMan Jun 02 '12 at 04:26