Performance Tip for IE browsers : Java garbage collection is NOT Javascript garbage Collection

843 views
Skip to first unread message

Rokesh

unread,
Sep 26, 2011, 6:29:15 AM9/26/11
to google-we...@googlegroups.com
Hi All,

I've been working on GWT for a while now and noticed something important and peculiar.
In GWT you code in Java and almost automatically assume Java garbage collection (because that's how you code).

you'll see that the callback is created like the code snippet below. I imagine that every GWT project will implement it like this.

The Use Case: Fill data in the new CellTable widget. The size of the data varied from 100 to 600 records.

If the onSuccess results in a lot of data and this event/callback is invoked lots of times (actually 3 to 5 times is enough) , on IE6, IE7,IE8 and IE9 there will be performance degradation. In other browsers
like Chrome, Firefox and Safari there is no performance loss noticed. 

After reading Joel Webber's note on Performance, I found out that it's not the component (like the CellTable) that causes this behavior,
but it was the callback creation. It seems that IE doesn't know how to garbage collection the "callback". So each time this "new AsyncCallback" is performed,
a memory leak occurred in IE browsers.

Since the Callback is stateless, we decided to create the callback only once  (static variable) and there was no performance loss anymore.
After clicking for a while, the performance was the same (even after inspecting the results of  some measurement tools).

This worked for us and even IE6 is blazingly fast. IE9 is now even faster that Chrome! No need for Chrome Frame Plugin (some organizations don't allow Plugins at all, think about that!)

Hopefully this tip works for you.

And Yes we have GWT in production. That company's browser policy is to use IE (now on 7, moving to 8). So this had to work or otherwise , it was a huge showstopper!

GWT applications do perform (even in older browsers), just make sure that critical parts of your code are carefully reviewed (especially on the "new").



 AsyncCallback callback = new AsyncCallback() {
   
public void onSuccess(Void result) {
     
// do some UI stuff to show success
   
}

   
public void onFailure(Throwable caught) {
     
// do some UI stuff to show failure
   
}
 
};

 
// (3) Make the call.......

Juan Pablo Gardella

unread,
Sep 26, 2011, 7:32:04 AM9/26/11
to google-we...@googlegroups.com
Thanks for share!!!

2011/9/26 Rokesh <rja...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/3ZelaqGUGTcJ.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Magno Machado

unread,
Sep 26, 2011, 9:10:11 AM9/26/11
to google-we...@googlegroups.com
interesting,

Would it be possible to implement this optimization at the compiler level, so that programmers don't have to care about this? I mean, only on the IE permutations, or for all permutations if it doesn't have negative effects on other browsers

Not something specific to callback, but for anonymous classes in general

ben fenster

unread,
Sep 26, 2011, 10:26:00 AM9/26/11
to Google Web Toolkit
so just to understand just replace anonymous in call callback with a
variable will fix the problem ?
instead of:
call(new callback(){});

use :
callback c = new callback(){};
call(c);

?



On Sep 26, 1:29 pm, Rokesh <rjan...@gmail.com> wrote:
> Hi All,
>
> I've been working on GWT for a while now and noticed something important and
> peculiar.
> In GWT you code in Java and almost automatically assume Java garbage
> collection (because that's how you code).
>
> In this code sample:http://code.google.com/webtoolkit/doc/latest/DevGuideServerCommunicat...
> you'll see that the callback is created like the code snippet below. I
> imagine that every GWT project will implement it like this.
>
> The Use Case: Fill data in the new CellTable widget. The size of the data
> varied from 100 to 600 records.
>
> If the onSuccess results in a lot of data and this event/callback is invoked
> lots of times (actually 3 to 5 times is enough) , on IE6, IE7,IE8 and IE9
> there will be performance degradation. In other browsers
> like Chrome, Firefox and Safari there is no performance loss noticed.
>
> After reading Joel Webber's note on Performance, I found out that it's not
> the component (like the CellTable) that causes this behavior,
> but it was the callback creation. It seems that IE doesn't know how to
> garbage collection the "callback". So each time this "new AsyncCallback" is
> performed,
> a memory leak occurred in IE browsers.
>
> Since the Callback is stateless, we decided to create the callback only once
>  (*static *variable) and there was no performance loss anymore.

Rokesh Jankie

unread,
Sep 26, 2011, 10:42:07 AM9/26/11
to google-we...@googlegroups.com
Almost there...


method doSomething (){


callback c = createCallback();


}


static AsyncCallback<?> callback = null;

AsyncCallback<?> createCallBack(){

   if (callback==null){
     callback = new AsyncCallback<Object>() {
               // implement onSuccess  
              //  implement onFailure
    }
       
    }
   return callback;

}


Since we are keeping the callbacks stateless, this really improved performance on IE!

Nicolas Antoniazzi

unread,
Sep 26, 2011, 11:26:52 AM9/26/11
to google-we...@googlegroups.com
Interresting. But in this case, is not it the same problem for all anonymous classes ?
Do not you have this problem with event handler ?

myButton.addClickHanler(new ClickHandler() {
  public void onClick(ClickEvent e) {...}
}

2011/9/26 Rokesh Jankie <rja...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.

Rokesh Jankie

unread,
Sep 26, 2011, 11:36:55 AM9/26/11
to google-we...@googlegroups.com
No definitely not: This is only with the callback Class (and in combination with a lot of data).
Somehow the callback is not garbage collected properly in IE.

The GWT team covered this part (of (new ClickHandler)...)


And the last post of Joel Webber was definitely worth the read!

So in short: Only the callback ( AsyncCallback()) class we changed to a static class variable and that was it...No other code changes...

Again: java garbage collection is (apparently) NOT the same as Javascript garbage collection.

Jens

unread,
Oct 12, 2012, 3:30:22 PM10/12/12
to google-we...@googlegroups.com
You have a static variable that is instantiated when its needed (lazy instantiation).


private static AsyncCallback<Person> findPersonCallback;

private AsyncCallback<Person> createFindPersonCallback() {
  if(findPersonCallback == null) {
     findPersonCallback = new AsyncCallback<Person>() { 
         //...... implement ..... 
     }
  }
  return findPersonCallback;
}

public void fetchByName(String name) {
   AsyncCallback<Person> callback = createFindPersonCallback();
   personService.findByName(name, callback);
}

-- J.

tskardal

unread,
Feb 8, 2013, 7:32:23 AM2/8/13
to google-we...@googlegroups.com
Thanks for sharing. Very interesting!

Is this issue still around with the latest version of GWT and IE?
Reply all
Reply to author
Forward
0 new messages