【发布时间】:2013-11-18 11:46:23
【问题描述】:
我使用 Flex Builder 在 Adobe Flex 中构建的应用程序遇到内存泄漏问题。使用 30-40 分钟后,它开始变得越来越慢。
应用程序将图像显示为目录,但当我推送和弹出视图时,我的记忆力会大大增加。
我的想法是,如果我将所有对象设置为 null 并处置我使用的所有位图数据,我可以释放足够的内存来继续使用应用程序而没有问题,但似乎问题不存在。
我的应用 menuView.mxml、categoriesView.mxml 和 productsView.mxml 中有 3 个视图。
我的应用程序在我的电脑(不是平板电脑)中启动,总内存:47Mb 和私有内存:88 MB
在推送和弹出视图 5 次后,我得到 TotalMemory: 61Mb 和 Private Memory : 101 MB
想象一下,如果我这样做几次。该应用程序在我的 Ipad 或三星 Galaxy Tab 中开始运行缓慢。
为什么会这样?我不知道如何解决这个问题。
请帮忙!非常感谢!!
我在下面放了一些代码。
当我在 menuView 中时,我使用以下代码将视图从 menuView 推送到 categoriesView
protected function button3_clickHandler(event:MouseEvent):void
{
if((FlexGlobals.topLevelApplication.getIdClienteServidorCompraActual( )!=null)&& (FlexGlobals.topLevelApplication.getIdClienteServidorCompraA ctual()>0))
{
navigator.pushView(categoriesView);
}
}
当我在 categoriesView 中时,我使用以下代码将视图从 categoriesView 推送到 productsView。在这个视图中,每个类别都有 3 个按钮。
protected function buttonC1_clickHandler(event:MouseEvent):void
{
//Categoria general con todos
var ab:ArrayCollection = getIdAmbienteServidor();
cleanMemory();
navigator.pushView(productsView, null);
}
private function cleanMemory():void
{
result.splice(0);
result = null;
System.gc();
}
当我在 productsView 中时,我使用以下代码将视图从 productsView 弹出(我使用 PUSH INSTEAD OF POP,因为我有不同的选项)到 categoriesView 。
protected function button1_clickHandler(event:MouseEvent):void
{
cleanMemory();
navigator.pushView(menuView);
}
private function cleanMemory():void
{
if(image1 != null)
{
image1.source = "";
if(image1.bitmapData != null)
{
image1.bitmapData.dispose();
}
}
if(image2 != null)
{
image2.source = "";
if(image2.bitmapData != null)
{
image2.bitmapData.dispose();
}
}
if(result != null)
{
result.splice(0);
result = null;
}
if(result1 != null)
{
result1.splice(0);
result1 = null;
}
if(result2 != null)
{
result2.splice(0);
result2 = null;
}
dbConnection = null;
object1 = null;
object2 = null;
dataToSave = null;
cGreyImageSmallAsset = null;
cRedImageSmallAsset.bitmapData.dispose();
cRedImageSmallAsset = null;
cOrangeImageAsset.bitmapData.dispose();
cOrangeImageAsset = null;
cGreenImageAsset.bitmapData.dispose();
cGreenImageAsset = null;
cPinkImageAsset.bitmapData.dispose();
cPinkImageAsset = null;
cBlueImageAsset.bitmapData.dispose();
cBlueImageAsset = null;
cGreyImageAsset.bitmapData.dispose();
cGreyImageAsset = null;
cRedImageAsset.bitmapData.dispose();
cRedImageAsset = null;
cGreenImageSmall = null;
cOrangeImageSmall = null;
cPinkImageSmall = null;
cBlueImageSmall = null;
cGreyImageSmall = null;
cRedImageSmall= null;
cGreenImage = null;
cPinkImage = null;
cBlueImage= null;
cGreyImage = null;
cRedImage= null;
cOrangeImage= null;
System.gc();
}
我加载图像。
private function setImages():void
{
if(object1!=null)
{
panelLeft.visible = true;
buttonLeftMore.visible = true;
image1.source = "file://" + File.applicationStorageDirectory.nativePath + "/b"+object1.idArchivo+"_500.jpg";
setObject1MainTexts();
}
else
{
image1.source = "";
panelLeft.visible = false;
buttonLeftMore.visible = false;
}
if(object2!=null)
{
panelRight.visible = true;
buttonRightMore.visible = true;
image2.source = "file://" + File.applicationStorageDirectory.nativePath + "/b"+object2.idArchivo+"_500.jpg";
setObject2MainTexts();
}
else
{
image2.source = "";
panelRight.visible = false;
buttonRightMore.visible = false;
}
}
当我在 categoriesView 中时,我使用以下代码将视图从 categoriesView 弹出(我使用 PUSH INSTEAD OF POP,因为我有不同的选项)到 menuView。
protected function button1_clickHandler(event:MouseEvent):void
{
cleanMemory();
navigator.pushView(menuPrincipalBelda);
}
private function cleanMemory():void
{
result.splice(0);
result = null;
System.gc();
}
提前致谢!
【问题讨论】:
-
您永远不必手动运行 GC,也很少必须在 DisplayObject 上运行“清理”例程。只需将其从显示列表中删除并取消对它的任何引用就足够了。这听起来更像是您在某处泄漏了对您的视图的引用,从而阻止了它在 GC 中被清理。您是否检查了分析器中的泄漏?
-
我们在使用 Flash Builder 开发移动应用程序时也遇到了这个问题。感谢这个帖子,我会试试这个建议。
标签: apache-flex memory memory-leaks air adobe