apache-flex - Flash Builder 4 分析器 : how to spot what objects are causing a known memory increase?

标签 apache-flex flexbuilder flash-builder profiler

我知道探查器问题可能非常笼统,但这里我有一个非常具体的问题和示例。

我知道在下面的代码(取自 Joshua's question )中,无限数量的circle 对象实例被添加到hostComponent 中。这显然会导致应用程序逐渐变慢。

我的问题是,当我运行 Flash Builder Profiler 时,我到底在哪里看到问题所在?

Running example of the app

要尝试一下,请创建一个新的 Flex 4 项目,然后粘贴以下代码:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
               initialize="onInit()" viewSourceURL="srcview/index.html">
    <fx:Script>
        <![CDATA[
            import mx.core.UIComponent;
            import mx.effects.Fade;         
            import spark.effects.Move;

            private var hostComponent:UIComponent;

            private function onInit():void{

                hostComponent = new UIComponent();
                hostComponent.id = "circleHostComponent";
            }

            /* Add circle UIComponent objects to the hostComponent.
                Move and Fade the circle objects */
            private function onTimerEvent(event:TimerEvent):void{  

                var yPos:Number = Math.ceil(Math.random()*100);
                var radius:Number = Math.ceil(Math.random()*5); //1-12
                var effectAlpha:Number = Math.random()*0.5 + 0.2 // 0-1
                var effectDuration:Number = Math.ceil(Math.random()*3000) + 1000;

                var circle:UIComponent = new UIComponent();
                circle.graphics.beginFill(0x1C75BC, effectAlpha);
                circle.graphics.drawCircle(90, yPos, radius);
                circle.graphics.endFill();

                hostComponent.addChild(circle);

                var moveEffect:Move= new Move(circle);
                moveEffect.xBy = 300;
                moveEffect.duration = effectDuration;

                moveEffect.play(); 

                var fadeEffect:Fade = new Fade(circle);
                fadeEffect.alphaFrom = 1;
                fadeEffect.alphaTo = 0;
                fadeEffect.duration = effectDuration;

                fadeEffect.play();

                this.addElement(hostComponent);

            }

            private function onClick():void{
                startButton.enabled = false;
                var t:Timer = new Timer(100, 0);
                t.start();
                t.addEventListener(TimerEvent.TIMER, onTimerEvent);

            }       

        ]]>
    </fx:Script>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <s:Button id="startButton" label="Click to Start" click="onClick()" />
</s:Application>

最佳答案

首先,我会在使用该应用程序一段时间后查看“内存使用情况”面板:

enter image description here

注意内存越来越多。 有一个“运行垃圾收集器”按钮可以强制执行 GC。然而,当你点击它时,内存并不会减少。

下一步是找出罪魁祸首。为此,您可以使用“事件对象”面板:

enter image description here

看起来是这样的,除了一些 Vector 实例之外,一切看起来都很好。 默认情况下,许多类是从事件对象数据网格中过滤出来的。幸运的是,我们可以指定要显示和隐藏哪些类。 默认情况下,flash.x.x 包中的所有类都是隐藏的。从过滤列表中删除它们会给表带来一些有趣的东西:

enter image description here

注意 Graphics 行:已经创建了 871 个实例,并且它们仍然在内存中!有了这些信息,您就可以假设 Graphics 实例是导致应用程序速度减慢的原因。如果您还过滤掉 mx.* 类,您将看到有 871 个 UIComponents 实例。每次创建 UIComponent 时,都会实例化一个 Graphics 对象。

最后一步是在屏幕上不再需要每个 UIComponent 后将其删除,并查看是否有任何性能改进。

关于apache-flex - Flash Builder 4 分析器 : how to spot what objects are causing a known memory increase?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4958795/

相关文章:

javascript - 如何在浏览器的子窗口中加载我的Flex应用程序?

php - 应采用什么设计来保存文件信息

xml - 如何计算 flex 中的根 xml 标记中有多少个子节点?

actionscript-3 - Flex 4透明背景问题?

android - Flash Builder,为移动设备加载资源

javascript - 从浏览器调整 Flash 对象的大小而不闪烁

apache-flex - 我需要 Flex Builder 吗?

apache-flex - 服务器和手机之间发送数据

mobile - 可以替代缓慢且不兼容 HTML5 的 Adob​​e Air WebKit v6531.9 (Webview) 吗?

android - Flash Builder 像 Flash IDE 一样将文件嵌入到应用程序中?