ForumsProgramming ForumQuestions about garbage collector

3 2717
clipmaster3
offline
clipmaster3
104 posts
Nomad

In my flash game i have a little explosion animation that needs to be added to the screen every so often when a tower fires. I thought to myself "Ha! I'll be smart and in the last frame of the animation's timeline, I'll include the code parent.removeChild(this); so that i can create the animation with addChild(new explosion()); and never have to worry about removing it in the tower class's code!" What I found was that after it was removed from the stage, it continued to loop through the animation, would get back to the line parent.removeChild(this), and would fail and have Flash scream at me because parent was no longer defined. I solved the problem by adding stop(); after the code but it means that the garbage collector doesn't immediately pick it up after the references to it are lost. So now I have several questions:

1. When I dynamically add it to the screen, the garbage collector won't randomly run and pick it up because it will be contained in the children of stage (even though there's no variable referencing it), will it?
2. The garbage collector will eventually pick it up once it has been removed from the stage and has no other references to it, right?
3. Is it possible to force the garbage collector to run, and if so, would it be an acceptable idea to run it after the animation is removed, or does the processing required to run the collector outweigh the memory saved by not having the animation floating around?

Thanks in advance

  • 3 Replies
herofmb
offline
herofmb
30 posts
Nomad

Take a look at this topic:

http://armorgames.com/community/thread/3634656/help-with-garbage-colector

I think it answers all of your questions.

clipmaster3
offline
clipmaster3
104 posts
Nomad

Yes that answers the first two questions, thanks, but I'd still like to know about the third.

joeybetz
offline
joeybetz
107 posts
Shepherd

There is a way to run/force the garbage collector from the developer end if your worried about a memory leak. System.gc(); is the call for it. This is kind of a slow call so be wise about how you use it. This isn't the perfect way of doing things because if objects in memory are still being pointed to then they won't be removed.

Ideally you should look at the memory usage of your program first before making any adjustments with the System.totalMemory value. This will give you the amount of memory the flash player is currently using in bytes.

Showing 1-3 of 3