2/9/2010 2:39:04 PM
Title:
Memory Leak problem
I have got a memory leak problem in the example below(u can download the code from the link)
http://brandonmeyer.net/projects/SuperPanelDemo/SuperPanelDemo.html
Running in Profiler:-
What I am trying to do is creating new panels by selecting the Add new panel button.
I am selecting option allow Close (check box).
(After creating few panels and closing these panels i could find there is memory leak with the SuperPanel class)
So my problem is how to resolve this memory leak
2/9/2010 11:33:04 PM
Hi Ren ,
I think you are expecting to free up memory as soon as you close the panel.
Memory management is done in a different way in flash player. When you remove reference to some object, flash player marks the object for removal but the memory is not immediately freed up. Memory is recovered when Garbage collector runs automatically after some interval and that time is controlled by flash player itself. Not to waste system resources the garbage collector usually runs when memory is requested by some application and is not available. So you should practice best coding practices to free memory like removing reference of objects , variables etc by deleting them when not required. So that flash player knows what to remove when memory is required.
2/9/2010 11:57:46 PM
Hi, Thanx for the information. But I was unable to remove those instances and free up that memory, even after running GC.I tried with useWeakreferences=true in the addEventListener.
But that did not work. Can you tell me exactly where I have to make changes to free up those instances
Inder
Points: 2880
Posts:0
2/10/2010 1:19:09 AM
By setting weak references you can make the object available to garbage collector when no strong reference is detected by garbage collector. The object is not removed by garbage collector until the memory is required for some purpose or the object can be used for some other purpose.
In your you are using panel, since panel is a reusable component, using multiple panels wont consume memory again and again , there will be a slight raise in memory. Due to this when you will remove the panel the memory used by panel class may be retained by garbage collector.
In case of event listeners you can use removelistener or weakrefrence , and that's enough to make it available to garbage collector. But remember it may be or may not be removed by garbage collector.
2/10/2010 1:25:50 AM
BTW how did you run garbage collector, there is no way to run it in flash. You can use local connection hack but that is not the reliable one .
I smashed my head to garbage collection a lot. but flash player behavior is not predictable. Sometimes it will recover memory sometimes not. After study I found out that its a demand and supply thing. If system asks flash player for memory need then it runs garbage collector to free memory, else it keeps it for future use. So if you system is already having free ram then flash player will not run the process of garbage collection to preserve system resources. To me it seems to be a quite sensible approach.
2/10/2010 1:30:36 AM
Hi redni, Thank you for the reply.
I tried to specify usewekrefernce=true and then running GC. These changes din't work.Can you tell me where to make changes in my code, in order to free the memory
Inder
Points: 2880
Posts:0
2/11/2010 12:31:28 AM
Ren, Garbage collection is entirely a function that is dependent on the flash player. What you can do is just set reference weak or delete an object. If still memory does not free up then it means that GC has decided not to free it up due to some internal reason.
As a good programming practice what you did is sufficient.
2/11/2010 1:29:53 AM
I was able to find where the problem is and tried to fix it. Memory leak may be due to BindingUtils.bindProperty . Here is the modified code. Just replace that method with this.
My problem is at anytime even after closing all instances there is one instance still occupying the memory. Can someone tell me whats wrong with this code?
private function addNewPanel():void
{
var watchers:Array=[];
var panel:SuperPanel = new SuperPanel();
panel.width = 300;
panel.height = 200;
panel.minWidth = 200;
panel.minHeight = 100;
panel.title = "My Panel " + (panelContainer.numChildren + 1);
panel.addEventListener(CloseEvent.CLOSE, function(event:CloseEvent):void{
for each(var watcher :ChangeWatcher in watchers) {
watcher.unwatch();
}
event.target.parent.removeChild(event.target);
},false,0,true);
watchers.push(BindingUtils.bindProperty(panel, "allowDrag", allowDragCheck, "selected"));
watchers.push(BindingUtils.bindProperty(panel, "allowResize", allowResizeCheck, "selected"));
watchers.push(BindingUtils.bindProperty(panel, "allowClose", allowCloseCheck, "selected"));
watchers.push(BindingUtils.bindProperty(panel, "allowMaximize", allowMaxCheck, "selected"));
watchers.push(BindingUtils.bindProperty(panel, "allowMinimize", allowMinCheck, "selected"));
panelContainer.addChild(panel);
}