附录 我有一个像以下一类的班级结构,在那里,我有一个临时儿童,被展示,后来被替换,因此不再需要。 然而,由于儿童以前接触过多种方法,我需要成为一类变量。
因此,在替代该儿童之后,我不再需要,希望允许垃圾收集再利用这一空间。 由于变量是类别变量,因此我不能使用<代码>delete tempChild。 通常,如果再没有提及停车场,则垃圾收集将失去记忆;因此,我的问题是,如果简单地将<代码>null转让给该变量,那么就足以启动该平台。
class Test extends Sprite
{
private var tempChild:Sprite;
public function Test ()
{
tempChild = new Sprite();
tempChild.graphics.drawRect( 0, 0, 100, 100 );
this.addChild( tempChild );
}
public function replace ( obj:DisplayObject ):void
{
this.removeChild( tempChild );
tempChild = null; // <-- here I want to free the variable
this.addChild( obj );
}
}
<<>strong>edit: 这方面的一个快速基准就是:
Test type A: Storing the Sprite reference as a private class variable
Test type B: Accessing it via child index (getChildAt
)
- Test 1: Accessing the sprite once -> B is about three times as slow as A
- Test 2: Deleting the sprite (removeChild vs. removeChildAt) -> nearly equal, no visible performance plus from using just the index (and skipping the search for the object)
- Test 3: Access the sprite multiple times -> A is about 20% faster than B
- Test 4: Same as 2, but with additional sprites that fill up the childList (so removeChild actually has to search) -> B is about 25% faster than A (as expected)
由于我只一次删除了这个内容,但不得不多次加以利用,我坚持选择A(基本上像上面的法典)。