Flash Builder 4 Profiler:如何识别哪些对象正在导致已知的内存增加? [英] Flash Builder 4 Profiler: how to spot what objects are causing a known memory increase?

查看:183
本文介绍了Flash Builder 4 Profiler:如何识别哪些对象正在导致已知的内存增加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我知道在下面的代码中(取自约书亚的问题 >),无数的 circle 对象实例被添加到 hostComponent 中。这显然会导致应用程序的逐渐放缓。



我的问题是,当我运行Flash Builder Profiler时,究竟在哪里看到问题出在哪里?



运行应用程序示例 a>



试试看,创建一个新的Flex 4项目,并粘贴下面的代码:

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

private var hostComponent:UIComponent;

private函数onInit():void {

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

/ *将circle UIComponent对象添加到hostComponent。
移动和淡化圆形对象* /
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);


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

}

]]>
< / fx:Script>

< fx:声明>
<! - 在这里放置非可视元素(例如服务,值对象) - >
< / fx:声明>

< / s:Application>


解决方案

首先,我会查看Memory Usage面板玩一下应用程序:





注意内存越来越多。
有一个强制GC的运行垃圾收集器按钮。但是,单击它时,内存不会减少。

下一步是识别罪魁祸首。为此,您使用Live Objects面板:





看起来像,appart一些Vector实例,一切看起来都不错。
默认情况下,会从活动对象datagrid中过滤很多类。幸运的是,可以指定哪些类将被显示和隐藏。
默认情况下,flash.x.x软件包中的所有类都是隐藏的。从过滤列表中删除它们会使表格变得有趣:



注意图形行:已经创建了871个实例,它们全部仍在内存中!有了这些信息,你可以假设图形实例负责应用程序的减速。如果您也过滤掉mx。*类,您将看到有871个UIComponents实例。每次创建UIComponent时,都会创建一个Graphics对象。



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


I know profiler questions can be quite general, but here I have a very specific question and example.

I know that in the following code (taken from Joshua's question), that an infinite number of circle object instances are being added to the hostComponent. This obviously causes gradual slowing down of the app.

My question is, when I run the Flash Builder Profiler, where exactly do I see where the problem lies?

Running example of the app

To try it out, create a new Flex 4 project, and paste in this code:

<?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>

解决方案

First, I would look at the Memory Usage panel after playing a bit with the application :

Notice the memory increases more and more. There is a "Run Garbage Collector" button that forces the GC. However, when you click on it, the memory doesn't decrease.

The next step is to identify the culprits. For that, you use the Live Objects panel :

It looks like that, appart some Vector instances, everything looks allright. By default, a lot of classes are filtered from the live objects datagrid. Fortunately, one can specify which classes will be displayed and hidden. All classes from the flash.x.x packages are hidden by default. Removing them from the filtered list bring somthing interesting to the table :

Notice the Graphics row : 871 instances have been created and they are all still in memory! With that info, you can suppose the Graphics instances are responsible of the slow down of the application. If you also filter out the mx.* classes, you will see there are 871 UIComponents instances. Everytime a UIComponent is created, there is a Graphics object also instancied.

Final step is to remove each UIComponent once it's no more needed on screen and look if there is any performance improvement.

这篇关于Flash Builder 4 Profiler:如何识别哪些对象正在导致已知的内存增加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆