Skip to main content

Regarding the dispose() method

2 replies [Last post]
jldominguez
Offline
Joined: 2008-01-02
Points: 0

Hello. I think I have read somewhere that the dispose() method should no be used. According to that, objects should be simply set to null, and then the garbage collector will do its job when the execution thread gets out of the (unlinked) object's context.

What do you think about this? Could the dispose() method be a source of problems? Is there a a good reason to use dispose() instead of "= null" or viceversa?

Regards,
Lucas

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Hinkmond Wong

phonemeadvanced@mobileandembedded.org wrote:
> Hello. I think I have read somewhere that the dispose() method should no be used. According to that, objects should be simply set to null, and then the garbage collector will do its job when the execution thread gets out of the (unlinked) object's context.
>
> What do you think about this? Could the dispose() method be a source of problems? Is there a a good reason to use dispose() instead of "= null" or viceversa?
>

Hi Lucas,

In general java.awt.Window.dispose() is used on a GUI component that is
a subclass of Window, in order for that specific GUI Window object (and
its children) to properly release and destroy native UI resources (such
as the screen). Garbage collection (and the eventual exiting of the
entire VM) may happen as a result of dispose(), but is not directly
connected with the dispose() call itself.

It is not the same as setting a pointer to an object to null, since that
will only possibly trigger garbage collection on that object and has
nothing to do with releasing and destroying the native UI resources that
object may have been using (ex. if that object is a non-GUI Java object).

So, without seeing your specific coding example (or just a snippet of
the code), in general you should be using dispose() to get rid of any
subclass of Window GUI object (so that it can possibly release any
native UI resources it holds), but use the setting of a pointer to null
on other type of non-GUI objects so that they can be properly reclaimed
by GC.

Hinkmond

---------------------------------------------------------------------
To unsubscribe, e-mail: advanced-unsubscribe@phoneme.dev.java.net
For additional commands, e-mail: advanced-help@phoneme.dev.java.net

jldominguez
Offline
Joined: 2008-01-02
Points: 0

Thanks,
Lucas