Component does not receive garbage collection

I just noticed strange behavior while looking at my application in Flash Profiler. When I click on a button in TitleWindow, then TitleWindow does not receive garbage collected after its removal. I have no idea why this is happening.

I created a small sample application, so you can try:

Main.mxml

<?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" pageTitle="Memory Leak (Spark)">

    <fx:Script>
        <![CDATA[
            protected function openWindowBtn_clickHandler():void
            {
                removeAllElements();
                addElement(new ExampleView());
            }
        ]]>
    </fx:Script>

    <s:controlBarContent>
        <s:Button label="Open Window" id="openWindowBtn" click="openWindowBtn_clickHandler()"/>
    </s:controlBarContent>
</s:Application>

ExampleView.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%" title="Example View" close="closeHandler()">

    <fx:Script>
        <![CDATA[
            import mx.core.IVisualElementContainer;

            protected function closeHandler():void
            {
                var visualElementParent:IVisualElementContainer = parent as IVisualElementContainer;

                if (visualElementParent)
                    visualElementParent.removeElement(this);
                else
                    parent.removeChild(this);
            }
        ]]>
    </fx:Script>

    <s:layout>
        <s:VerticalLayout verticalAlign="middle" horizontalAlign="center"/>
    </s:layout>

    <s:Button id="doSomethingBtn" label="Click me!"/>
</s:TitleWindow>

When you click "Open Window" and close ExampleView without clicking "Click me!" in this case, the GC starts and deletes the ExampleView. However, when you click "Click me!" and then close the ExampleView, the ExampleView will remain in memory forever.

I could not find the links in Profiler that cause this behavior. I hope someone knows about this, otherwise Flex will create a lot of memory leaks.

+3
3

, , , , , , . GC , - , , . :

: -


<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" pageTitle="Memory Leak (Spark)">

<fx:Script>
    <![CDATA[
        protected function openWindowBtn_clickHandler():void
        {
            removeAllElements();
            addElement(new ExampleView());
        }

        protected function button1_clickHandler(event:MouseEvent):void
        {
            var o:Object = new Object();
            System.gc();
        }

    ]]>
</fx:Script>

<s:controlBarContent>
    <s:Button label="Open Window" id="openWindowBtn" click="openWindowBtn_clickHandler()"/>
    <s:Button label="Force GC"  click="button1_clickHandler(event)"/>
</s:controlBarContent>
</s:Application>

. "Force GC" , ExampleWindow. , - , System.gc() ( , , ), , , " , , , Flash Player , .

+1

, , iirc EventListeners, MXML, , Button GC'ed.

EventListener , ? EventListeners Debugger, - WeakMethodClosure, .

+2

ExampleView doesn't seem to be collecting garbage because EventListener is added in some way when "Click Me" is clicked. The best way to avoid this is 1. Add the Event Listener manually to the createComplete event 2. Remove the EventListener in closeHandler 3. Remove the button from the container and set its value

ExampleView will now be combined with garbage

0
source

Source: https://habr.com/ru/post/1770062/


All Articles