Flex ModuleLoader组件导致内存泄漏.如何正确卸载模块? [英] Flex ModuleLoader component causing memory leak. How can I unload Modules properly?

查看:170
本文介绍了Flex ModuleLoader组件导致内存泄漏.如何正确卸载模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该应用程序太大,无法在此处描述,但是我可以告诉您,我可以随时使用多达20个或更多模块.而且,如果我一个接一个地进入加载屏幕,则我的应用程序可以占用500MB或更多空间.

The application is too big to describe here, but I can tell you I have up to 20 modules or more that the client can use at any time. And if I go on loading screen after screen, my application can ocuppy 500MB and more.

我用来加载和卸载模块的脚本是:

The script that I use to load and unload modules is:

public function createModule(modulo:String):void {
                if(moduleLoader != null){
                    moduleLoader.unloadModule();
                    //moduleLoader.url = null;
                    moduleLoader.url = "com/oss/facturable/components/" + modulo + "?version=" + model.configXML.versionApp;    
                    moduleLoader.loadModule();
                }       
            }
            private function errorHandler(e:ModuleEvent):void {
                Alert.show("No se ha podido cargar el modulo. Contacte al departamento técnico.");
            }

加载模块的容器:

<s:BorderContainer width="98%" height="98%" includeIn="mainState" styleName="bcModuleLoader" top="100">
                    <s:layout>
                        <s:HorizontalLayout paddingLeft="10" paddingRight="10" paddingTop="10" paddingBottom="10"/>
                    </s:layout>
                    <s:Scroller width="100%" height="100%">
                        <s:Group>
                            <mx:ModuleLoader id="moduleLoader" error="errorHandler(event)" width="100%" height="100%" horizontalAlign="center" verticalAlign="top" creationComplete="createModule('bandejaEntrada.swf')"/>              
                        </s:Group>
                    </s:Scroller>
 </s:BorderContainer>

每当我单击与此处无关的菜单选项时,都会调用createModule函数.总而言之,这就是我现在要使它正常工作和泄漏的一切:S

The createModule function is called whenever I click on a menu option that I think is irrelevant to post here. All in all, this is everything I have for now to make it work and leak :S

我认为这是官方的unloadModule函数,从外观看,我认为这应该很好地分配内存!它将删除所有eventListeners和子对象.为什么在卸载时我的内存保持不变,而在打开新模块时又在上面加载更多内存?啊!

This is the official unloadModule function, I thought, by the looks of it, that the memory should be well allocated with this thing, I mean, look! It removes all the eventListeners and child objects. How come my memory stays the same when unloading, and loads more memory on top of it when I open a new module?! Augh!

public function unloadModule():void
    {
        if (child)
        {
            removeChild(child);
            child = null;
        }

        if (module)
        {
            module.removeEventListener(ModuleEvent.PROGRESS,
                                       moduleProgressHandler);
            module.removeEventListener(ModuleEvent.SETUP, moduleSetupHandler);
            module.removeEventListener(ModuleEvent.READY, moduleReadyHandler);
            module.removeEventListener(ModuleEvent.ERROR, moduleErrorHandler);

            module.unload();
            module.removeEventListener(ModuleEvent.UNLOAD, moduleUnloadHandler);
            module = null;
        }
    }

推荐答案

来自这篇文章:

当将HierachicalDataAdvancedDataGrid一起使用时,该控件通过使用IHierarchicalCollectionView接口来跟踪分层数据和分组数据.接口具有openNodes属性,该属性包含一个对象数组,这些对象表示AdvancedDataGrid中当前打开的数据提供者的节点.

When using HierachicalData with an AdvancedDataGrid, the control keeps track of hierarchical and grouped data by using the IHierarchicalCollectionView interface. The interface has an openNodes property contains an Array of objects that represent the nodes of the data provider that are currently open in the AdvancedDataGrid.

如果用新的数据提供程序替换数据提供程序,则AdvancedDataGrid控件不会清除openNodes属性.因为openNodes属性包含对旧数据提供程序中对象的引用,所以无法通过垃圾回收将这些对象从内存中删除.为了确保可以从内存中删除旧的数据提供程序,请在替换数据提供程序时使用事件处理程序清除openNodes属性.

If you replace the data provider with a new data provider, the AdvancedDataGrid control does not clear the openNodes property. Because the openNodes property contains references to objects in the old data provider, the objects cannot be removed from memory by garbage collection. To ensure that the old data provider can be removed from memory, use an event handler to clear the openNodes property when you replace the data provider.

将事件侦听器添加到数据提供者的CollectionEvent.COLLECTION_CHANGE事件中,以清除openNodes属性.

Add an event listener to the data provider’s CollectionEvent.COLLECTION_CHANGE event to clear the openNodes property.

// Handle the colectionChange event.
private function removeOpenNodes(event:CollectionEvent):void 
{
  if(event.kind == CollectionEventKind.RESET)
    IHierarchicalCollectionView(adg.dataProvider).openNodes = {};
}

这篇关于Flex ModuleLoader组件导致内存泄漏.如何正确卸载模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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