首页 \ 问答 \ AS3中对象的内存使用情况和垃圾回收(Memory usage and garbage collection of Objects in AS3)

AS3中对象的内存使用情况和垃圾回收(Memory usage and garbage collection of Objects in AS3)

我想知道关于Object类型,特别是当涉及Flash中的垃圾回收时。

我知道在这样的情况下物品将准备好进行垃圾回收:

// create
var ar:Array = [];

var mc:MovieClip = new MovieClip();
mc.addEventLisntener(blah, blah);

ar.push(mc);
addChild(mc);

// kill & gc
ar.splice(0, 1);
mc.removeEventListener(blah, blah);
removeChild(mc);

但是,如何/将在下面的情况下收集垃圾。

假设我在我的课程MartysMC中有一个函数,我通过以下方式解析Object

package
{
    import flash.display.MovieClip;

    public class MartysMC extends MovieClip
    {
        /**
         * Updates this
         * @param obj An object containing key/value pairs to represent new property values
         */
        public function update(obj:Object):void
        {
            var i:String;
            for(i in obj)
            {
                this[i] = obj[i];
            }
        }
    }
}

现在我利用这个功能:

var mmc:MartysMC = new MartysMC();

var dataObject:Object =
{
    x: 10,
    y: 34,
    alpha: 0.6
};

mmc.update(dataObject);

dataObject会发生什么? 这会从这里收集垃圾吗? 即使如此,这条线的目的是什么呢?

mmc.update({x:15,y:18,name:"marty"});

I want to know about the Object type specifically when it comes to garbage collection in Flash.

I know that items will be ready for garbage collection in situations like this:

// create
var ar:Array = [];

var mc:MovieClip = new MovieClip();
mc.addEventLisntener(blah, blah);

ar.push(mc);
addChild(mc);

// kill & gc
ar.splice(0, 1);
mc.removeEventListener(blah, blah);
removeChild(mc);

But how/will an Object get garbage collected in situations like below.

Say I have a function in my class MartysMC that I parse an Object through:

package
{
    import flash.display.MovieClip;

    public class MartysMC extends MovieClip
    {
        /**
         * Updates this
         * @param obj An object containing key/value pairs to represent new property values
         */
        public function update(obj:Object):void
        {
            var i:String;
            for(i in obj)
            {
                this[i] = obj[i];
            }
        }
    }
}

And now I make use of this function like so:

var mmc:MartysMC = new MartysMC();

var dataObject:Object =
{
    x: 10,
    y: 34,
    alpha: 0.6
};

mmc.update(dataObject);

What happens to dataObject? Will this get garbage collected from here? Even still, what about the object in this line:

mmc.update({x:15,y:18,name:"marty"});

原文:https://stackoverflow.com/questions/6197639
更新时间:2023-03-06 16:03

最满意答案

布局是惰性的,当您添加或删除组件时,不会立即验证容器(这将导致更新布局层次结构),这是出于性能原因而完成的。

因此,一旦您对UI已准备好(您已完成添加/删除内容)感到满意,您应该调用revalidaterepaint您修改的容器

不要将重量级(AWT)组件与轻量级(Swing)组件混合使用,这将导致无问题。 如果您不知道, ButtonCheckBox是重量级组件。 有关替代方法,请参见如何使用按钮,复选框和单选按钮


Layouts are lazy, when you add or remove components, the container is not immediately validated (which would cause the layout hierarchy to be updated), this is done for performance reasons.

So once you're satisfied that the UI is ready (you've finished adding/removing stuff), you should call revalidate and repaint on the container(s) you modified

Don't mix heavyweight (AWT) components with light weight (Swing) components, this is going to cause no end of issues. If you're not aware, Button and CheckBox are heavyweight components. See How to Use Buttons, Check Boxes, and Radio Buttons for alternatives

相关问答

更多
  • 我不确定,但你可以尝试这个: public synchronized void setText(String text){ typeField.append(text); } synchronized将同步两个线程。 您可以在这里阅读更多内容: Docs Oracle Okay the answer to this question turned out to be really simple. And it goes something like this: Since I'm declarin ...
  • 布局是惰性的,当您添加或删除组件时,不会立即验证容器(这将导致更新布局层次结构),这是出于性能原因而完成的。 因此,一旦您对UI已准备好(您已完成添加/删除内容)感到满意,您应该调用revalidate并repaint您修改的容器 不要将重量级(AWT)组件与轻量级(Swing)组件混合使用,这将导致无问题。 如果您不知道, Button和CheckBox是重量级组件。 有关替代方法,请参见如何使用按钮,复选框和单选按钮 Layouts are lazy, when you add or remove co ...
  • 首先,如前所述,您的GUI应该只在Event dispatch线程上运行。 在编写时,您的GUI类会做两件事:它是一个框架,一个是可运行的,两者都是完全独立使用的。 事实上,在GUI对象上调用“run”会创建另一个不相关的GUI对象。 这可能就是你什么也看不见的原因。 所以我建议你做主要的以下内容: ... main(...) { SwingUtilities.invokeLater(new Runnable() { public void run() { GUI gui ...
  • 构造函数不应该是void或返回任何东西,否则它将被视为方法而不是构造函数: public void NewClass() { // ^^-------------------------mistake 相反,你必须使用: public NewClass() { The constructor should not be void or return any thing, else it will be considered like a method and not a constructor ...
  • 我认为问题在于你在Main类中创建了一个SaveClass的实例,但是在Save方法中,你在SaveClass中创建了另一个实例,并且你从该实例中读取了文本。 所以你可能想要这样做Save()方法: delete the SaveClass sa = new SaveClass(); 接着: String text = this.getText1(); I think the problem is that you create an instance of SaveClass in the Main ...
  • 你正在遮蔽 txtSource变量,取而代之 JTextArea txtSource = new JTextArea(20, 80); 同 txtSource = new JTextArea(20, 80); You're shadowing the txtSource variable, replace JTextArea txtSource = new JTextArea(20, 80); with txtSource = new JTextArea(20, 80);
  • 设置JTextArea的lineWrap和wrapStyleWord属性 JTextArea MainText = new JTextArea(); MainText.setLineWrap(true); MainText.setWrapStyleWord(true); 看看如何使用文本区域了解更多细节 您可能还会发现通过Java编程语言的代码约定来读取某些用法 除非你真的不想,否则我还会建议将JTextArea添加到JScrollPane而不是直接将其添加到容器 panel.add(new JScrol ...
  • 您需要将DocumentListener添加到支持文本区域的Document 。 然后在侦听器的回调方法(insertUpdate(),removeUpdate(),changedUpdate())中,只需设置一个标记即某些内容已更改并在关闭应用程序之前测试该标志 public class MyPanel implements DocumentListener { private boolean changed; public MyPanel() { JTextArea textA ...
  • 如果我像这样创建JTextArea,它可以工作: JScrollPane使用自己的自定义布局管理器。 该滚动窗格包含以下区域: 水平/垂直滚动条 一个“行标题”和“列标题” 组件位于滚动窗格的顶部/右侧和顶部/左侧 用于包含要滚动的组件的“视口” 当您使用以下内容时: scrollableTextArea.add(text); 这会弄乱滚动窗格,因为组件直接添加到滚动窗格而不是滚动窗格的视口 当你使用: JScrollPane scrollableTextArea = new JScrollPane(ne ...
  • 正如这里和这里所建议的, null布局会引起麻烦。 因为您的显示以描述为中心,请使用JTextArea构造函数,该构造函数允许您按行和列指定大小。 当你pack()封闭的Window ,它将被调整大小以适应文本区域。 我添加了一些特别的文字来说明效果。 我还更新了代码以在调整框架大小时允许文本区域增长。 import java.awt.EventQueue; import java.awt.GridLayout; import javax.swing.BorderFactory; import javax. ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。