how to disable garbage collector as3 [duplicate]

纵然是瞬间 提交于 2020-01-05 04:21:11

问题


so, I am making really enormous project with as3, and I`d like to know how to disable garbage collector, as user might be on for long and I dont want him or her to just be cut off because garbage collector removed some listener somewhere that should be now needed.

is there easier way to disable garbage collector than go to every single listener and add "false, 0, true" extension after naming the listener?


回答1:


No it's not possible, but what you're afraid of isn't really an issue.

Also, making event listeners weak doesn't disable the garbage collector, in fact it makes whatever object they are set to eligible for garbage collection if that is the only reference left to them.




回答2:


That is possible actually, but it needs discipline...

When an as3 object looses all its references to others, it becomes a candidate for garbage collection. To avoid this you can use the following method:

When you need an object to persist in memory, bind it to somewhere accessible.

Here I supplied an example code.

First create the class below:

package your.package.path;

public class noGc {

protected static var vault:Array = [];

// In case we forget that noGc should stay static....   
public function noGc(){throw new Exception('Error: Instance from STATIC noGc');}

public static function hold(o: *): void {
   if(vault.indexOf(o)==-1)vault.push(o); // no multiple references in vault 
}

public static function release(o: *): Boolean {
var i: int = vault.indexOf(o);
    if(i == -1)return(false);            // return value is for information only
    vault.splice(i,1);                   // remove object from vault
    return(true);
}

public static function releaseAll(): void {
    vault.length = 0;
}

} // end class noGc

To avoid gc on "yourObject"

noGc.hold(yourObject);

To allow gc on "yourObject" back

noGc.release(yourObject);

For normal code flow start holding the objects right after creation of them. Then you should release them at the end of their use.

Also you have to keep an eye on exceptions since exceptions break the normal flow you should handle them and release the objects becoming irrelevant after the exception.

Forgetting an object held means use of unnecessary memory, a.k.a. a memory leak.

As I told before, It needs discipline.

Finally, when you need to wipe all objects that are held use,

noGc.releaseAll();

Hope this helps.



来源:https://stackoverflow.com/questions/15198931/how-to-disable-garbage-collector-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!