Glycerine
Glycerine

Reputation: 7347

Garbage collection - manually wiping EVERYTHING!

I have been building a game for a while (nearly done) - But the game needs a replay button and its a big task.

I know the GC is dreadful in flash, but I wanted to know if there is a way to wipe EVERYTHING as if the flash app has just begun. Clearing memory, game data - I haven't to worry about game loading as its not really heavy on data.

Everything pretty much lives in a DataModel - but I fear if I just clear the all variables, I'll have pockets of orphaned memory.

Any forwarding idea would be great.

cheers guys.

Upvotes: 2

Views: 312

Answers (3)

Matt D
Matt D

Reputation: 1506

The problem with Flash's garbage collector is that as far as I know you cannot FORCE it to "collect". I had this issue with a program in which I would load in various external SWF's, and occasionally Flash would simply not load them. What you may need to do is make a function to set every major variable in your code to null at the end of a game, if you want a true reset.

There is still no guarantee, but unfortunately I'm fairly certain there is no shortcut to force a memory release.

(For me this meant also making sure my event handlers were cleared properly, and my loaders would "unload()" after passing their content off, in the case of my external SWF loader.)

Upvotes: 0

fenomas
fenomas

Reputation: 11139

I would do this:

  1. make a class that encapsulates your entire game, called GameContainer or whatever.
  2. Do a search on all your source code, and make sure that in every call to addEventListener, you are passing true for the "use weak references" argument.
  3. In your document class (or frame script), make a single instance of GameContainer and add it to the stage, and do nothing else.

Now when you want to entirely clear your game from memory, remove GameContainer from the stage and null the reference. Memory will not immediately be released, but everything in your game will now be eligible for release. If Flash thinks it needs more memory it will trigger a GC, and the large orphaned GameContainer will be nuked. (Step 2 above will keep your event listeners from counting as references to your objects, and make sure that all self-contained objects are eligible for disposal.

Not sure what you mean about Flash's GC being dreadful though. I can't recall having heard of any bugs in it. It won't nuke your objects unless you are careful with your references, but that's true of all garbage collection.

Upvotes: 2

grapefrukt
grapefrukt

Reputation: 27045

Not short of refreshing the page. There might be some hack you could do by loading a separate swf and then unloading it, but that would be just as error prone as doing it the proper way.

My advice would be to just buckle down and write your reset function then get something to monitor memory and make sure it works by reiniting/resetting a bunch of times.

Upvotes: 1

Related Questions