Does Javascript do Garbage Collection?

79 views
Skip to first unread message

Elam

unread,
Jun 16, 2006, 4:52:12 PM6/16/06
to Google Web Toolkit
Hi,

I have a situation where a user presses a button and depending on the
class involved, a new FlexTable is constructed with various new
widgets. Since this Table is constructed a number of times during the
application, I'm wondering about resources.

Are the objects I create for the table garbage collected similar to
Java or am I going to find my app bogging down because the objects are
never destroyed when the new ones take their place?

Cheers,
-Elam

Eugine

unread,
Jun 16, 2006, 5:56:42 PM6/16/06
to Google Web Toolkit
JavaScript usually does garbage-collection, however IE screws it up in
a very specific case:
http://www.quirksmode.org/blog/archives/2005/02/javascript_memo.html

What I'm wondering is the following - does GWT recognize these cases
and take care of them appropriately

br...@google.com

unread,
Jun 17, 2006, 5:10:47 PM6/17/06
to Google Web Toolkit

Yes! The widget architecture is designed to avoid memory leaks in IE.
So, you can use widgets freely without worrying about leaks.

JSNI is somewhat riskier, depending on what you do. If you use JSNI to
set up event handlers, for example, you can accidentally re-introduce
leaks if you aren't careful. That's why we recommend using composites
of existing widgets as much as possible, since composites won't leak.
Keep in mind that writing JSNI is just like writing any raw
JavaScript...it's powerful but there's less GWT can do to help you
automatically.

See
<http://code.google.com/webtoolkit/documentation/com.google.gwt.doc.DeveloperGuide.UserInterface.CreatingCustomWidgets.html>
for details on building widgets using composites.

-- Bruce

Joel Webber

unread,
Jun 19, 2006, 12:00:38 PM6/19/06
to Google-We...@googlegroups.com
I want to add one very important distinction here -- if you use the event handling facilities (e.g., sinkEvents() and onBrowserEvent()) available through Java and the UIObject/Widget classes, you will *not* create memory leaks.  It is only by using JSNI that you can create potentially-leaking cycles.  So while it is usually preferred to create Composites (because it's easier and less error-prone), new Widgets in the style if Button, ListBox, Tree, etc. are not any likelier to cause leaks.

joel.

Eugine

unread,
Jun 19, 2006, 12:23:17 PM6/19/06
to Google Web Toolkit
Thank you, Bruce and Joel - I think you put all of us much more at ease
about using GWT.

Reply all
Reply to author
Forward
0 new messages