令人困惑的垃圾收集问题 [英] perplexing garbage collection issue

查看:91
本文介绍了令人困惑的垃圾收集问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1) var x1:X = new X();
2) var x2:X = new X();
...
3) x1.z = new SWFLoader(...);
...
4) x2.z = x1.z;
5) x1.z = null
6) x1 = null;

最后一个语句是无用的,因为语句4保证x1和别的它包含永远不会被垃圾收集只要x2.z存在。没有任何人认为这是奇怪的?这是一个重大的冲击和缺点我的东西,我需要做的。有没有什么解决办法呢?

The last statement is useless because statement 4 guarantees that x1 and anything else it contains will never ever be garbage collected as long as x2.z exists. Does anyone else think this is bizarre? This was a major shock and drawback to me for something that I needed to do. Is there any workaround at all?

的唯一原因,这将是有意义的是,如果一切X1被存储在连续内存什么的,但是当你说x1.z =新的......在大多数语言暗示其准备和分配的新块内存别的地方,并返回一个指向它的指针(即后来分配到x2.z以及一个指针。)我总是跨preting参考,在动作的指针。

The only reason this would make sense is if everything in x1 was stored in contiguous memory or something, but when you say "x1.z = new ..." in most languages that implies its going and allocating a new block of memory somewhere else and returning a pointer to it (a pointer that is subsequently assigned to x2.z as well.) I was always interpreting "reference" in actionscript as "pointer".

当然,有人可能会说,好吧,你仍然可以删除个别一切X1。但是,如果不是为了说明4以上的,记6将标志着一切X1为删除。

Of course some might say, well you could still delete everything in x1 individually. But if not for statement 4 above, statement 6 would mark everything in x1 for deletion.

(注:我把语句5中的唯一理由是要告诉flash播放器:我真的不关心x1.z了,但它并没有区别。)

(Note: the only reason I put statement 5 in was to tell flash player "I really don't care about x1.z anymore", but it made no difference.)

推荐答案

,你是说下面的code漏液X1。但事实并非如此。你是如何测试一个X1不符合集合?你可以张贴一些工作code能重现问题?

If I understood your question correctly, you are saying that the following code should leak x1. But it doesn't. How are you testing that x1 is not eligible for collection? Can you post some working code that reproduces the problem?

package {
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.net.URLLoader;
    import flash.system.System;
    import flash.utils.Dictionary;
    import flash.utils.setInterval;

    public class test extends Sprite
    {

        private var _dict:Dictionary = new Dictionary(true);

        private var x1:Foo;
        private var x2:Foo;

        public function test()
        {
            testGC();
            setInterval(function():void { 
                traceCount();
            },100);         
        }

        private function testGC():void {
            x1 = new Foo();
            x2 = new Foo();

            _dict[x1] = true;
            _dict[x2] = true;

            x1.z = new URLLoader();
            x2.z = x1.z;

            x1 = null;


        }

        private function traceCount():void {
            var count:int = 0;
            for each(var i:* in _dict) {
                count++;
            }
            trace(count);
            System.gc();
        }

    }
}

import flash.net.URLLoader;

class Foo {

    public var z:URLLoader;
}

这篇关于令人困惑的垃圾收集问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆