Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force Garbage Collection in AS3?

Is it possible to programmatically force a full garbage collection run in ActionScript 3.0?

Let's say I've created a bunch of Display objects with eventListeners and some of the DO's have been removed, some of the eventListeners have been triggered and removed etc... Is there a way to force garbage collection to run and collect everything that is available to be collected?

like image 913
defmeta Avatar asked Oct 10 '08 17:10

defmeta


People also ask

Is it possible to force garbage collection in?

You really can't force Java GC. The Java garbage collection algos are non-deterministic, and while all of these methods can motivate the JVM to do GC, you can't actually force it.

Which method is used to force garbage collection in C#?

Collect() method. You can force garbage collection either to all the three generations or to a specific generation using the GC. Collect() method.

Can we force the garbage collector to run at any time?

Running the Garbage CollectorYou can ask the garbage collector to run at any time by calling System 's gc method: System. gc(); You might want to run the garbage collector to ensure that it runs at the best time for your program rather than when it's most convenient for the runtime system to run it.


2 Answers

Yes, it's possible, but it is generally a bad idea. The GC should have a better idea of when is a good time to run than you should, and except for a very specific case, like you just used 500MB of memory and you need to get it back ASAP, you shouldn't call the GC yourself.

In Flash 10, there is a System.gc() method you can call (but please don't, see above) - keep in mind System.gc() only works in the debugging version of Flash player 10+.

In Flash 9, there is an unsupported way to force it via an odd LocalConnection command, but it may not work in all versions. See this post by Grant Skinner.

like image 113
davr Avatar answered Sep 30 '22 17:09

davr


There is a new API for telling the GC that it might be a "relatively good moment" to collect.

See the Adobe API docs for System.pauseForGCIfCollectionImminent

And also this Adobe blog post from shortly after the method was introduced in Player version 11

The method takes an "imminence" argument; basically, you feed in a low number (near 0.0) if you really want the collector to run, even if there has not been much activity (currently measured by bytes-allocated) since the last collection, and you feed in a large number (near 1.0) if you only want the collection pause to happen if we were already near the point where a collection would happen anyway.

The motivation here is for situations in e.g. games where you want to shift the point where GC's happen by a small amount, e.g. do the GC during a change of level in the game, rather than two seconds after the player started exploring the level.

One very important detail: This new API is supported by both the Release and the Debugger Flash Runtimes. This makes it superior to calling System.gc().

like image 36
pnkfelix Avatar answered Sep 30 '22 17:09

pnkfelix