Reputation: 17
I am working on how to reduce the memory usage in the code and got to know that removing the component also remove its children present inside it.If this happens the memory usage must decrease but it is increasing.
I have a titlewindow which contains of hboxes and those hboxes have canvases as children which contains images. Now if i use removeChild(titlewindow)
Does all the hboxes, canvases and images present in it gets removed or not?
If gets removed the memory usage is reduceed or not? How can i do that in flex?
Upvotes: 0
Views: 217
Reputation: 4750
Yeah, everything pretty much gets removed with it, as long as you then set the value of titleWindow
to null
and don't ever re-add those children. As for whether this clears out any memory or not, it basically will under two conditions:
The garbage collector runs afterwards. This can be expensive, and thus Adobe's designed it to not necessarily just keep happening over and over again at regular intervals. Instead it tends to happen when Flash Player or AIR is running out of memory in its current heap, at which point the garbage collector will check first to see if it can free up enough space within the current heap before anything more is grabbed from the operating system.
You don't have any non-orphaned references to these children anywhere else. By "non-orphaned", I mean that if the only places where you still have references to them are themselves without any references in the rest of your program, this condition is still met.
There is at least one exception to this rule, and that is that the garbage collector can single out multiple objects in your program as GCRoots
. A GCRoot
is never garbage-collected, period. So if you orphan off a GCRoot
(make it so that neither it nor any of its descendants have any references anywhere outside of themselves), the garbage collector basically just doesn't care. The GCRoot
will be left in there, and any references it has to any objects are thus considered live and active. Additionally there are certain occasions when the garbage collector will simply not be able to tell whether something in memory is a reference or not, so it'll just assume that it is and potentially fail to delete something. Usually this is not a problem, but if your program is big enough and is not doing a lot of object-pooling, I can tell you from experience that reacting specifically to this can on rare occasions be a necessity.
Upvotes: 1
Reputation: 1110
Try setting the titlewindow to null after removing them:
removeChild(titlewindow);
titlewindow = null;
The garbage collector will remove all your boxes from memory if there are no more references to them from your main code. It should be okay to ignore explicitly removing the children, as long as the only references to them are from the parent, i.e. titlewindow and its children are an isolated group of objects. But make sure you also remove any event listeners that anything might have registered to with removeEventListener()
.
Also, there is no guarantee when the garbage collector actually runs, so if it looks like your memory is increasing, it might just mean the GC hasn't had a chance to clear up the memory yet. Here's an SO question on how to force GC to run. (when debugging, System.gc()
usually works for me).
Upvotes: 0