以前我们最熟悉的强制内存回收的方法是:
function GC():void
{
try
{
(new LocalConnection).connect("foo");
(new LocalConnection).connect("foo");
}
catch (e)
{
trace(System.totalMemory);
}
}
{
try
{
(new LocalConnection).connect("foo");
(new LocalConnection).connect("foo");
}
catch (e)
{
trace(System.totalMemory);
}
}
//由于上次试验弱引用的时候,发现当轮询本地sharedobject对象的时候,导致弱引用的内容进行了回收,于是联想用来进行强制内存回收..代码如下:
代码
package {
import flash.net.SharedObject;
import flash.system.System;
import flash.utils.clearInterval;
import flash.utils.setInterval;
public class GCPlus {
public static function clear(isTraceTM : Boolean = false) : void {
var time : int = 2;
var interval : int = setInterval(loop, 50);
function loop() : void {
if(!(time--)) {
isTraceTM && trace(System.totalMemory);
clearInterval(interval);
return;
}
SharedObject.getLocal("cypl", "/");
}
}
}
}
import flash.net.SharedObject;
import flash.system.System;
import flash.utils.clearInterval;
import flash.utils.setInterval;
public class GCPlus {
public static function clear(isTraceTM : Boolean = false) : void {
var time : int = 2;
var interval : int = setInterval(loop, 50);
function loop() : void {
if(!(time--)) {
isTraceTM && trace(System.totalMemory);
clearInterval(interval);
return;
}
SharedObject.getLocal("cypl", "/");
}
}
}
}
//测试文档类:
代码
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.system.System;
public class GC_Test extends Sprite {
private static const NUM : int = 1000;
private var num : int;
public function GC_Test() {
init();
}
private function init() : void {
num = NUM;
stage.addEventListener(MouseEvent.CLICK, clickHandler);
while (num--) {
var mc : MovieClip = new MovieClip;
mc.graphics.beginFill(0);
mc.graphics.drawRect(0, 0, 100, 100);
mc.x = Math.random() * 500;
mc.y = Math.random() * 400;
addChild(mc);
}
trace(System.totalMemory);
}
private function clickHandler(e : MouseEvent) : void {
clear();
}
private function clear() : void {
while (numChildren) {
removeChildAt(0);
}
GCPlus.clear(true);
}
/*function GC():void{//这是我们熟悉的方法
try{
(new LocalConnection).connect("foo");
(new LocalConnection).connect("foo");}catch(e){
trace(System.totalMemory);
}
}*/
}
}
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.system.System;
public class GC_Test extends Sprite {
private static const NUM : int = 1000;
private var num : int;
public function GC_Test() {
init();
}
private function init() : void {
num = NUM;
stage.addEventListener(MouseEvent.CLICK, clickHandler);
while (num--) {
var mc : MovieClip = new MovieClip;
mc.graphics.beginFill(0);
mc.graphics.drawRect(0, 0, 100, 100);
mc.x = Math.random() * 500;
mc.y = Math.random() * 400;
addChild(mc);
}
trace(System.totalMemory);
}
private function clickHandler(e : MouseEvent) : void {
clear();
}
private function clear() : void {
while (numChildren) {
removeChildAt(0);
}
GCPlus.clear(true);
}
/*function GC():void{//这是我们熟悉的方法
try{
(new LocalConnection).connect("foo");
(new LocalConnection).connect("foo");}catch(e){
trace(System.totalMemory);
}
}*/
}
}
通过System.totalMemory的结果可以清楚的看到内存得以释放,window任务管理器也能够看到有变化哦,原理也可能和异常有点关系.
补充下:也许大家试验其他的异常方法,在IDE调试下也能看到内存释放,但是独立的swf测试的时候,会看到任务管理器中内存并没有得到释放. 目前也只发现传统的LC方法和我上面提到的SO的方法能独立测试也能够达到释放的效果,
// 发觉强制回收有一个特点,都是在用链接到本地的未知的东西抛出异常。