0

I'm using AS3 and FlashDevelop, and I cannot seem to convince my FlashPlayer/AVM to GC it.

I tried the pretty standard options:

myboolean = null;
delete(myboolean);

But for the null it says "col: 14 Warning: null used where a Boolean value was expected." And delete I get "col: 11 Error: Attempt to delete the fixed property allDone. Only dynamically defined properties can be deleted."

And this is considering the Boolean is definited within a method as such:

var myBoolean:Boolean = false;

I appreciate that since it's within the method, when such has run it's course it should get garbage collected, but I like to be certain, and why can't I GC the Boolean when I've done the same for int, Array and Point within another method of the same class? Isn't Boolean also an object?

So if anyone knows how to GC the Boolean please let me know.

Jason Sturges
  • 15,855
  • 14
  • 59
  • 80

3 Answers3

2

Basically like Jonatan Hedborg here says you don't directly control what is garbage collected.

My guess is you're from a c/c++ background or the like where you are responsible for maintaining memory more strictly and directly, this isn't exactly the case with AS3 and Java; though memory management is still very important it's handled more at an Object level. Although Boolean extends from Object read here regarding primitive types:

Null data type The Null data type contains only one value, null . This is the default value for the String data type and all classes that define complex data types, including the Object class. None of the other primitive data types, such as Boolean, Number, int and uint, contain the value null . Flash Player and Adobe AIR will convert the value null to the appropriate default value if you attempt to assign null to variables of type Boolean, Number, int, or uint. You cannot use this data type as a type annotation.

http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f88.html#WS5b3ccc516d4fbf351e63e3d118a9b90204-7f82

In both cases you do have mechanisms such as nulling references to detach objects from the graph of all of the active objects which means they'll be garbage collected at the next scheduled run of the garbage collector (which can be forced but it isn't really recommended, the configuration for the JVM or AVM in this case will handle it based on the system it's running/executing in).

nulling an object will allow it to be garbage collected but you shouldn't really be concerned about individual primitive properties. There's a good article explaining some details on garbage collection in AS3 here (I would leave a an abstract but the whole page is pretty good, main points I suppose being garbage collection isn't necessarily 100% straight-forward but with some effort can be managed): http://tomgabob.blogspot.com/2009/11/as3-memory-management.html

Basically the way it works is the FlashPlayer or whatever virtual machine is running your (byte)code has a graph of all the objects that have been created and that there is a current reference to.

It also has a memory limit for what it can use based on the environment (config etc.) so the GC has algorithms setup to figure out when it should attempt to garbage collect. You should primarily be concerned with nulling references to objects you no longer need, and really this isn't too big of a deal if your application isn't fairly complex, or your hardware isn't extremely restrictive with regard to RAM.

So the concern shouldn't be making the GC run too little or too much, but creating references that are never removed (addingListeners and not removing from objects that should be collected, or simply having references to them within collections etc. after they are no longer needed). Again the article above explains this in a bit more depth.

shaunhusain
  • 19,630
  • 4
  • 38
  • 51
  • Lol surprisingly maybe shaun AS3 is my first proper coding experience, and I guess my background. I just worry that all the vars will bring the GC out unnecessarily and cause constant GC runs, rather than me just nulling em. I've checked some articles both here and elsewhere and I've got no intention of forcing the GC, but was hoping someone somewhere knew how you can GC Booleans, because no-one seems to! – ReaperOscuro Jun 23 '12 at 20:53
  • There's absolutely no need to worry about a single Boolean, if you're making a thousand of these objects you should just be concerned about managing that large number of bigger classes, rather than worrying about primitives – shaunhusain Jun 23 '12 at 21:01
  • Fair enough...I know I probably worry too much, but it just seemed bizarre that I can null all other var references, but Booleans cause me an error -and it is more than one, prob 50-100. Tbf the code just needs smoothing over anyway, but I'm glad to have confirmed that I shouldn't really worry too much about it, ta shaun :) – ReaperOscuro Jun 23 '12 at 21:02
0

Where are you using the boolean? The only way to make sure primitives are GC'ed is to ensure the class it's used in is collected.

Jonatan Hedborg
  • 4,382
  • 21
  • 30
0

There are 2 errors in your reasoning:

  • 'delete' can only be applied to dynamic objects, not class members or local variables; in these cases, just set the member/variable to null,
  • value types (boolean, number, string) don't need to be GC'd; in some cases Flash will create temporary 'boxing objects' which will be automatically collected so you don't have to worry about those.
Philippe
  • 2,621
  • 1
  • 19
  • 20