在 AS3 中删除父级:这是否会释放所有子级使用的内存?

发布于 2024-09-19 19:28:33 字数 358 浏览 12 评论 0原文

我正在制作一个相当大的闪存项目,因此我担心内存使用情况。在应用程序的每个部分的末尾,我删除了保存内容的总体父元素。虽然这会删除父级,但这是否也会释放其中包含的每个子级的内存,或者我应该在删除父级之前运行迭代来删除这些子级?

如果我没有表达我想要的内容,我会给出更多解释:

addChild(movie1);
movie1.addChild(movie2);
movie1.addChild(movie3);

通过使用此代码:

removeChild(movie1);

它是否从内存中删除 movie2 和 movie3 或者它们仍然存储,只是未链接?

I'm making a rather large flash project and so I'm concerned about memory usage. At the end of each section of the application I remove the overarching parent element that holds the content. Although this remove the parent, does this also free up the memory for each of the children contained within this, or should I run an iteration to remove those prior to removing the parent?

I'll give a little more explanation in-case I'm not expressing what I want:

addChild(movie1);
movie1.addChild(movie2);
movie1.addChild(movie3);

By using this code:

removeChild(movie1);

Does it remove movie2 and movie3 from memory or are they still stored, just unlinked?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七七 2024-09-26 19:28:33

如果 movie2movie3 不再被另一个对象引用,则应该对它们进行垃圾回收。这同样适用于movie1

来自在 ActionScript 中创建可视化 Flex 组件

要以编程方式删除控件,
您可以使用removeChild()或
removeChildAt() 方法。您还可以
使用removeAllChildren()方法
从 a 中删除所有子控件
容器。调用这些方法确实
并没有真正删除对象。如果
你没有任何其他参考资料
对于孩子,Flash Player 将其包含在内
在未来的垃圾收集中
观点。但如果你存储了一个引用
到那个孩子在某个物体上,
孩子并没有从记忆中删除。

If movie2 and movie3 aren't referenced anymore by another object, they should be garbage collected. The same applies for movie1.

From Creating visual Flex components in ActionScript :

To programmatically remove a control,
you can use the removeChild() or
removeChildAt() methods. You can also
use the removeAllChildren() method to
remove all child controls from a
container. Calling these methods does
not actually delete the objects. If
you do not have any other references
to the child, Flash Player includes it
in garbage collection at some future
point. But if you stored a reference
to that child on some object, the
child is not removed from memory.

旧街凉风 2024-09-26 19:28:33

好吧,到目前为止,没有任何内容从内存中删除,这实际上取决于 movie1 的引用方式。假设稍后在应用程序中您想要再次添加 movie1,您应该发现 movie1 不仅还在这里,而且还包含 movie2 和 movie1。电影3.

var movie1:MovieClip;

whatever();
anotherMethod();

function whatever():void
{
    movie1 = new MovieClip();
    var movie2:MovieClip = new MovieClip();
    var movie3:MovieClip = new MovieClip();
    movie1.addChild(movie2);
    movie1.addChild(movie3);
    addChild(movie1);

    whateverElse();
}

function whateverElse():void
{ 
    //here, nothing is garbage collected, movie1 exists outside
    //the scope of this function
    removeChild(movie1);

    //now you can
    tryThis();

    //but if you do this you're effectively killing movie2 & movie3 , since
    //they're not referenced anywhere else.
    removeChild(movie1);
    movie1 = null;

    //this is going to throw an error , movie1's gone!
    tryThis();
}

function tryThis():void
{
    //yes ,it's still here with all its content
    addChild(movie1);
}

function anotherMethod():void
{
    var movieN:MovieClip = new MovieClip();
    var movie2:MovieClip = new MovieClip();
    var movie3:MovieClip = new MovieClip();
    movieN.addChild(movie2);
    movieN.addChild(movie3);

    // do all you need to do... 
    // then
    removeChild( movieN);
    getOut();
}

function getOut():void
{
   //life goes on without movieN, movie2 & movie3
   //waiting to be garbage collected
}

Well, nothing is removed from memory so far , it really depends on how movie1 is referenced. Let's say that later in the application you want to add movie1 again, you should find that movie1 is not only still here but it also contains movie2 & movie3.

var movie1:MovieClip;

whatever();
anotherMethod();

function whatever():void
{
    movie1 = new MovieClip();
    var movie2:MovieClip = new MovieClip();
    var movie3:MovieClip = new MovieClip();
    movie1.addChild(movie2);
    movie1.addChild(movie3);
    addChild(movie1);

    whateverElse();
}

function whateverElse():void
{ 
    //here, nothing is garbage collected, movie1 exists outside
    //the scope of this function
    removeChild(movie1);

    //now you can
    tryThis();

    //but if you do this you're effectively killing movie2 & movie3 , since
    //they're not referenced anywhere else.
    removeChild(movie1);
    movie1 = null;

    //this is going to throw an error , movie1's gone!
    tryThis();
}

function tryThis():void
{
    //yes ,it's still here with all its content
    addChild(movie1);
}

function anotherMethod():void
{
    var movieN:MovieClip = new MovieClip();
    var movie2:MovieClip = new MovieClip();
    var movie3:MovieClip = new MovieClip();
    movieN.addChild(movie2);
    movieN.addChild(movie3);

    // do all you need to do... 
    // then
    removeChild( movieN);
    getOut();
}

function getOut():void
{
   //life goes on without movieN, movie2 & movie3
   //waiting to be garbage collected
}
冬天的雪花 2024-09-26 19:28:33

当然,它会删除所有 3。

子电影 1 有两个子电影,电影 2 + 3。
如果他因某种个体死亡而死亡,孩子们也可能会死亡。

也许我错了,但你也可以尝试:

trace(movie2);

Ofcourse it removes all of the 3.

Child movie 1 has two children, movie2 + 3.
If he dies because of some Instance decease the children will possibily die too.

Maybe I'm wrong but you can also try:

trace(movie2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文