PDA

View Full Version : Why does not GC collects?


kahuja
04-10-2008, 05:05 PM
If you look at the code below, I add three canvas's to a ViewStack, but then I remove 3 of them. I would expect the GC to collect and remove them. But it does not: try clicking on the "Trace Old" button.

I am totally new to GC in AS, so please bear with me.



<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:view="com.sapient.resultspace.view.*" creationComplete="init();" activate="">

<mx:Script>
<![CDATA[
import mx.core.UIComponent;
import com.adobe.cairngorm.control.CairngormEvent;
import com.adobe.cairngorm.control.CairngormEventDispatch er;
import mx.controls.Alert;
import mx.events.IndexChangedEvent;


private function next():void
{
if(vs.selectedIndex == 3)
{
vs.selectedIndex = 0;
}
else
{
vs.selectedIndex = vs.selectedIndex + 1;
}
}

private function removeAll():void
{
vs.removeChildAt(1);
vs.removeChildAt(1);
vs.removeChildAt(1);

moveBtn.visible = false;
removeBtn.visible = false;
}

private function traceOld():void
{
if(c2 != null)
{
Alert.show("GC not kicked in yet");
}
}

]]>
</mx:Script>

<mx:VBox>
<mx:Button id="move" label="Move next" click="next();"/>
<mx:Button id=" remove" label="Remove All" click="removeAll();"/>
<mx:Button label="Trace old" click="traceOld();"/>
</mx:VBox>


<mx:ViewStack id="vs" selectedIndex="0" width="100%" height="100%">
<view:MyCanvas id="c1" label1="Stack 1" />
<view:MyCanvas id="c2" label1="Stack 2" />
<view:MyCanvas id="c3" label1="Stack 3" />
<view:MyCanvas id="c4" label1="Stack 4" />
</mx:ViewStack>
</mx:Application>

kahuja
04-10-2008, 05:22 PM
It seems the GC will run based on many factors affecting the state the machine - and not immediately.

dr_zeus
04-10-2008, 06:04 PM
Correct, the GC will not always run immediately. I believe people have found that it only runs when your SWF needs to allocate more memory (creating a new object, for instance), and there are other factors that may affect it too.