Question on garbage collection in GWT

7 views
Skip to first unread message

danl...@gmail.com

unread,
Apr 23, 2007, 3:15:55 AM4/23/07
to Google Web Toolkit
Hi,

I have read some posts in the forum to describe the garbage collection
in GWT. But I am still confusing. Would someone explain the behaviour
of garbage collection in the situation below:

1) In creating a GWT application, I create a DockPanel object called
"mainPanel" and add it into RootPanel.

RootPanel --> mainPanel

2) I create a class "MyButton" that extends class "Button" and
contain a instance variable "myMainPanel" and public method
"setMyMainPanel(DockPanel panel)"


public class MyButton extends Button
{
private DockPanel myMainPanel;

public MyButton(String title)
{
super(title);
}

public void setMyMainPanel(DockPanel panel)
{
myMainPanel = panel;
}
}

3) I create a object "myButton" with class "MyButton" and add it into
mainPanel

RootPanel --> mainPanel --> myButton

4) I call the method myButton.setMyMainPanel(mainPanel). so,

RootPanel --> mainPanel <--> myButton

5) Then I remove the myButton from the mainPanel by calling
main.remove(myButton)

RootPanel --> mainPanel <-- myButton


So, in this case, myButton will have reference to mainPanel but not be
referenced by any other objects. Then, does garbage collector
"collect" the garbage "myButton" ? I know that there will be
difference between IE and FF. So, how do they work on this scenario ?

Regards,
Dan

Reinier Zwitserloot

unread,
Apr 23, 2007, 11:35:57 AM4/23/07
to Google Web Toolkit
Yes, if you don't use DOM.* methods or JSNI to attach/detach event
listeners, create keys on random DOM objects that link back to your
GWT code, or grab references to them you'll be fine. (In layman's
terms: Use only DOM.setStyleAttribute and stay the heck away from
anything else in there unless you know what you are doing; that is,
unless you know how Javascript's garbage collector works - so long as
you stay away from DOM.* (and JSNI methods that do similar things)
everything will get garbage collected that can be garbage collected).

By the way, While IE (either version, AFAIK, but for sure IE6) will
garbage collect everything, it's a pretty crappy garbage collector.
It's no longer true in java itself, but in GWT this rule still holds:
If you can avoid creating new objects by reusing old ones, you
probably should.

danl...@gmail.com

unread,
Apr 23, 2007, 3:19:57 PM4/23/07
to Google Web Toolkit
Hi Reinier,

Thanks for your reply. So, according to your post, if i have the
following scenario,

RootPanel --> mainPanel <--> myButton

Then, I remove the mainPanel from RootPanel

RootPanel mainPanel <--> myButton

Both mainPanel and myButton should get garbage collected, right ?

Regards,
Dan

Reinier Zwitserloot

unread,
Apr 23, 2007, 5:47:16 PM4/23/07
to Google Web Toolkit
Yes, they will get garbage collected on all browsers.

danl...@gmail.com

unread,
Apr 24, 2007, 3:28:44 AM4/24/07
to Google Web Toolkit
Thank you very much on your support.

khaled...@gmail.com

unread,
Sep 21, 2007, 6:56:23 AM9/21/07
to Google Web Toolkit
Hi,

In order to test memory link/garbage collector/big tree, I wrote an
application (gwt 1.4.6) that generates a tree (root --> 500 children)
when I click on a button.

Layout of my app:
RootPanel-->layoutPanel-->treePanel-->Tree

Here is the code:
// random tree
Button generateBigTree = new Button("Generate bigTree");
generateBigTree.addClickListener(
new ClickListener() {
public void onClick(Widget w) {
if (categoriesTree != null) {
treePanel.remove(categoriesTree);
}
// add Categories tree
categoriesTree = new
CategoriesTree(getMainPanel());
treePanel.add(categoriesTree);
expandAllButton.setVisible(false);
int size = 500;
// generate tree with a root and 500 children

AsyncCallBackManager.loadRootCategory(getMainPanel(), false, false,
size);
}
});

Here is my tests:

Scenario (with ie 6):
1. url = about:blank in order to "remove" all javascript object : Mem
usage = 40'000k
2. url = GWT app : Mem uage: 48'000k
3. clik on "Generate big tree": Mem usage: 63'000k
4. clik on "Generate big tree": Mem usage: 75'000k
5. clik on "Generate big tree": Mem usage: 85'000k
6. clik on "Generate big tree": Mem usage: 92'000k
7. clik on "Generate big tree": Mem usage: 99'000k
8. clik on "Generate big tree": Mem usage: 111'000k
...
9. several cliks on "Generate big tree": Mem usage: 182'600k

Scenario with firefox:
1. open firefox : Mem usage = 29'208k
2. url = GWT app : Mem uage: 30'796k
3. clik on "Generate big tree": Mem usage: 42'644k
4. clik on "Generate big tree": Mem usage: 46'936k
5. clik on "Generate big tree": Mem usage: 46'968k
6. clik on "Generate big tree": Mem usage: 46'748k
7. clik on "Generate big tree": Mem usage: 46'892k
8. clik on "Generate big tree": Mem usage: 46'856k
...
9. several cliks on "Generate big tree": Mem usage: 46'988k


With firefox, results are whst expected but with ie6, It looks that
mem usage is growing indefinitly. Is there a solution to avoid this
behaviour?

Regards,
KT.

On Apr 24, 9:28 am, "danle...@gmail.com" <danle...@gmail.com> wrote:
> Thank you very much on your support.
>
> On Apr 24, 5:47 am, Reinier Zwitserloot <reini...@gmail.com> wrote:
>

> > Yes, they will getgarbagecollected on all browsers.


>
> > On Apr 23, 9:19 pm, "danle...@gmail.com" <danle...@gmail.com> wrote:
>
> > > Hi Reinier,
>
> > > Thanks for your reply. So, according to your post, if i have the
> > > following scenario,
>
> > > RootPanel --> mainPanel <--> myButton
>
> > > Then, I remove the mainPanel from RootPanel
>
> > > RootPanel mainPanel <--> myButton
>

> > > Both mainPanel and myButton should getgarbagecollected, right ?


>
> > > Regards,
> > > Dan
>
> > > On Apr 23, 11:35 pm, Reinier Zwitserloot <reini...@gmail.com> wrote:
>
> > > > Yes, if you don't use DOM.* methods or JSNI to attach/detach event
> > > > listeners, create keys on random DOM objects that link back to your
> > > > GWT code, or grab references to them you'll be fine. (In layman's
> > > > terms: Use only DOM.setStyleAttribute and stay the heck away from
> > > > anything else in there unless you know what you are doing; that is,

> > > > unless you know how Javascript'sgarbagecollectorworks - so long as


> > > > you stay away from DOM.* (and JSNI methods that do similar things)
> > > > everything will getgarbagecollected that can begarbagecollected).
>

> > > > By the way, WhileIE(either version, AFAIK, but for sure IE6) will
> > > >garbagecollect everything, it's a pretty crappygarbagecollector.


> > > > It's no longer true in java itself, but in GWT this rule still holds:
> > > > If you can avoid creating new objects by reusing old ones, you
> > > > probably should.
>
> > > > On Apr 23, 9:15 am, "danle...@gmail.com" <danle...@gmail.com> wrote:
>
> > > > > Hi,
>
> > > > > I have read some posts in the forum to describe thegarbagecollection
> > > > > in GWT. But I am still confusing. Would someone explain the behaviour

> > > > > ofgarbagecollection in the situation below:


>
> > > > > 1) In creating a GWT application, I create a DockPanel object called
> > > > > "mainPanel" and add it into RootPanel.
>
> > > > > RootPanel --> mainPanel
>
> > > > > 2) I create a class "MyButton" that extends class "Button" and
> > > > > contain a instance variable "myMainPanel" and public method
> > > > > "setMyMainPanel(DockPanel panel)"
>
> > > > > public class MyButton extends Button
> > > > > {
> > > > > private DockPanel myMainPanel;
>
> > > > > public MyButton(String title)
> > > > > {
> > > > > super(title);
> > > > > }
>
> > > > > public void setMyMainPanel(DockPanel panel)
> > > > > {
> > > > > myMainPanel = panel;
> > > > > }
>
> > > > > }
>
> > > > > 3) I create a object "myButton" with class "MyButton" and add it into
> > > > > mainPanel
>
> >> > > RootPanel --> mainPanel --> myButton
>
> > > > > 4) I call the method myButton.setMyMainPanel(mainPanel). so,
>
> > > > > RootPanel --> mainPanel <--> myButton
>
> > > > > 5) Then I remove the myButton from the mainPanel by calling
> > > > > main.remove(myButton)
>
> > > > > RootPanel --> mainPanel <-- myButton
>
> > > > > So, in this case, myButton will have reference to mainPanel but not be
> > > > > referenced by any other objects. Then, doesgarbagecollector

> > > > > "collect" thegarbage"myButton" ? I know that there will be
> > > > > difference betweenIEand FF. So, how do they work on this scenario ?
>
> > > > > Regards,
> > > > > Dan

Reinier Zwitserloot

unread,
Sep 21, 2007, 11:13:39 AM9/21/07
to Google Web Toolkit
There's no 'garbage collect' function call like in java, so you don't
know if it's actually 'working' or not. I suggest you just keep
pressing that button until you're in the 6 GB range. If IE crashes or
your computer crashes because your virtual memory is enormous.

On Sep 21, 12:56 pm, "khaled.tl...@gmail.com" <khaled.tl...@gmail.com>
wrote:

thierryd

unread,
Sep 24, 2007, 8:31:55 AM9/24/07
to Google Web Toolkit
Your post confuses me Reinier.... Do you mean that if I start using
DOM.*, GWT will know about it and stop taking care of garbage
collecting?

I'm using quite a lot of "DOM.setElementAttribute(element, "id",
htmlid)" (not anything else) to set the id of widgets. Is GWT still
doing garbage collecting for me?

Thanks,
thierryd :)

Reply all
Reply to author
Forward
0 new messages