Questions related to Disposable class

52 views
Skip to first unread message

Chamnap

unread,
Nov 4, 2010, 4:12:59 AM11/4/10
to Closure Library Discuss
I have read a book, closure definitive guide. However, I'm not clear
when to use this class. What's the benefit of using this class? When
to inherit from this class? Must I always call dispose() method?

Thanks
Chamnap

Shawn Brenneman

unread,
Nov 4, 2010, 4:47:12 PM11/4/10
to closure-lib...@googlegroups.com
Hi Chamnap,

It's a good idea to inherit from Disposable whenever a class holds
onto browser resources like DOM elements or XHRs, or if you need to do
specific cleanup whenever an object is no longer necessary.

If you have an object with handles to DOM elements that you no longer
use, those elements may remain resident in memory because the instance
maintains pointers to them and the browser's garbage collector doesn't
know they can be removed. A dispose method lets you delete or null out
those references so the memory can be freed.

In a small, short-running application, this may never be a concern. In
a long-running application, it helps prevent memory leaks. Do you
always need to call dispose()? Strictly, no. Browsers do a reasonable
job of cleaning up memory at page destruction these days. But if you
create and remove many UI Components or network requests, disposing
them when you're through will keep your app's memory footprint down.

Shawn

Reply all
Reply to author
Forward
0 new messages