views:

91

answers:

3

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.

+1  A: 

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.

grapefrukt
Your very correct of course. Indeed I was just hoping there was some shortcut. I'm having trouble insuring everything is deleted.If I were to switch back to frame 1 (my preloader) is it possible I can unload everthing I loaded on frame 2? My preloader works by loading everything on frame 2.
Glycerine
+1  A: 

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.

fenomas
This is a really really great idea. Will this work with a dataModel in the background and Papervision ? - And With the GC, I find it tends to be a little wayward in when it collects and the app can bloat a little before getting GC'd
Glycerine
It all depends on internal references. When you remove the last reference to any given object, it gets released, but if there are two objects that reference each other, even if you don't keep any "external" references to the two of them, they still have incoming references. Those don't get released until a GC (mark-sweep) occurs, and the timing of when that happens depends on various Flash internals. So in general you should monitor your content's long-term memory use, and not necessarily worry if it doesn't jump down when you think it should.
fenomas
A: 

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.)

Matt D
That's how all garbage collected languages work, you must manage your references if you want memory to be collected.
fenomas
Thanks fenomas. I wasn't able to make that claim definitively because I am only familiar with Flash and Java, and was unsure if there were any other collectors which could be manually told to free up memory for something as you dereference it.Appreciate the helpful clarification/elaboration.
Matt D