重新运行效果时,如何防止Flex中的内存泄漏?

时间:2012-03-09 19:43:27

标签: flex

这个应用程序是我想要完成的一个简化版本,通过使用复合动画在屏幕上进行一些运动的平滑,并在事情发生变化时重新运行它,并使用绑定来设置动画的某些值。动画定期停止,然后重新启动。 valueFrom未设置,因此属性值只是从它朝向新目标的位置继续。在这个例子中,我正在为一个Rect的左/上朝向鼠标的X / Y设置动画,使其追逐鼠标。

问题是如果你等一会儿并继续移动鼠标,这个应用程序的内存会继续增长。那么我该怎么做呢?不是吗?

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx"
    creationComplete="creationCompleteHandler(event)"
    mouseMove="mouseMoveHandler(event)"
    mouseOut="mouseOutHandler(event)"
    >
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            private static const RECT_HALF_SIZE:int = 5;
            private static const NO_CHASE:int = -1000;

            [Bindable]
            private var lastMouseX:int = NO_CHASE;

            [Bindable]
            private var lastMouseY:int = NO_CHASE;

            private var evalTimer:Timer = new Timer(100);

            protected function creationCompleteHandler(event:FlexEvent):void
            {
                evalTimer.addEventListener(TimerEvent.TIMER, function(event:TimerEvent):void { evalChase(); });
                evalTimer.start();
            }

            protected function mouseOutHandler(event:MouseEvent):void
            {
                lastMouseX = lastMouseY = NO_CHASE;
                evalChase();
            }

            protected function mouseMoveHandler(event:MouseEvent):void
            {
                const reStart:Boolean = (lastMouseX == NO_CHASE);

                lastMouseX = event.localX;
                lastMouseY = event.localY;

                if(reStart)
                    evalChase();
            }

            private function evalChase():void
            {
                doChase.stop();
                doChase.end();

                if(lastMouseX == NO_CHASE)
                    return;

                doChase.play();
            }

        ]]>
    </fx:Script>
    <fx:Declarations>
        <s:Linear id="linearEaser" />
        <s:Parallel
            id="doChase"
            duration="2000"
            >
            <s:Animate target="{chaser}" easer="{linearEaser}" >
                <s:SimpleMotionPath property="left" valueTo="{lastMouseX-RECT_HALF_SIZE}" />
            </s:Animate>
            <s:Animate  target="{chaser}" easer="{linearEaser}" >
                <s:SimpleMotionPath property="top" valueTo="{lastMouseY-RECT_HALF_SIZE}" />
            </s:Animate>
        </s:Parallel>
    </fx:Declarations>

    <s:Rect
        id="chaser"
        top="0" left="0"
        width="{2*RECT_HALF_SIZE}" height="{2*RECT_HALF_SIZE}"
        >
        <s:fill>
            <s:SolidColor color="red" />
        </s:fill>
    </s:Rect>

</s:WindowedApplication>

1 个答案:

答案 0 :(得分:0)

不要认为你在这段代码中实际上做错了什么,我复制到一个新的Flex 4.6项目并运行它,我可以看到你所说的关于ADL过程本身的内存消耗略有增加随着时间的推移,特别是在它停止运动后再开始,然而我尝试使用分析器在Flash Builder中运行它,看起来swf本身并没有真正泄漏任何内存,它对我来说大约保持在1.1 MB左右(最高可达1.4左右) ,但后来又倒退了)似乎它有相当数量的Vector。&lt; *&gt;内存中的对象虽然我不清楚这些对象中存储的是什么。