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 anObject
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
最满意答案
布局是惰性的,当您添加或删除组件时,不会立即验证容器(这将导致更新布局层次结构),这是出于性能原因而完成的。
因此,一旦您对UI已准备好(您已完成添加/删除内容)感到满意,您应该调用
revalidate
并repaint
您修改的容器不要将重量级(AWT)组件与轻量级(Swing)组件混合使用,这将导致无问题。 如果您不知道,
Button
和CheckBox
是重量级组件。 有关替代方法,请参见如何使用按钮,复选框和单选按钮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
andrepaint
on the container(s) you modifiedDon'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
andCheckBox
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 ...
-
JTextArea麻烦(JTextArea trouble)[2023-10-17]
布局是惰性的,当您添加或删除组件时,不会立即验证容器(这将导致更新布局层次结构),这是出于性能原因而完成的。 因此,一旦您对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 ...
-
JTextArea不可见(JTextArea not visible)[2022-12-07]
构造函数不应该是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 ...
-
JTextArea总是空?(JTextArea always null?)[2023-09-21]
你正在遮蔽 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问题(JTextArea issue)[2022-09-05]
设置JTextArea的lineWrap和wrapStyleWord属性 JTextArea MainText = new JTextArea(); MainText.setLineWrap(true); MainText.setWrapStyleWord(true); 看看如何使用文本区域了解更多细节 您可能还会发现通过Java编程语言的代码约定来读取某些用法 除非你真的不想,否则我还会建议将JTextArea添加到JScrollPane而不是直接将其添加到容器 panel.add(new JScrol ... -
如何知道jtextarea是否有任何变化?(How to know whether any changes in the jtextarea have been made or not?)[2024-02-10]
您需要将DocumentListener添加到支持文本区域的Document 。 然后在侦听器的回调方法(insertUpdate(),removeUpdate(),changedUpdate())中,只需设置一个标记即某些内容已更改并在关闭应用程序之前测试该标志 public class MyPanel implements DocumentListener { private boolean changed; public MyPanel() { JTextArea textA ... -
JTextArea不可编辑(JTextArea not editable)[2022-08-18]
如果我像这样创建JTextArea,它可以工作: JScrollPane使用自己的自定义布局管理器。 该滚动窗格包含以下区域: 水平/垂直滚动条 一个“行标题”和“列标题” 组件位于滚动窗格的顶部/右侧和顶部/左侧 用于包含要滚动的组件的“视口” 当您使用以下内容时: scrollableTextArea.add(text); 这会弄乱滚动窗格,因为组件直接添加到滚动窗格而不是滚动窗格的视口 当你使用: JScrollPane scrollableTextArea = new JScrollPane(ne ... -
JTextArea没有显示(JTextArea not showing)[2022-10-23]
正如这里和这里所建议的, null布局会引起麻烦。 因为您的显示以描述为中心,请使用JTextArea构造函数,该构造函数允许您按行和列指定大小。 当你pack()封闭的Window ,它将被调整大小以适应文本区域。 我添加了一些特别的文字来说明效果。 我还更新了代码以在调整框架大小时允许文本区域增长。 import java.awt.EventQueue; import java.awt.GridLayout; import javax.swing.BorderFactory; import javax. ...