Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What method in Java is used to destroy your objects [closed]

Tags:

java

Can you give me one example for my question?

like image 227
chandra Avatar asked Mar 21 '10 06:03

chandra


2 Answers

Sorry, but there isn't really a "free" or "dispose" equivalent in Java.

The best you can do is just set the object to null (removes the reference). Then explicitly tell the garbage collector you're going rambo (its somewhere in java.lang.Runtime).

like image 177
Rev316 Avatar answered Oct 29 '22 02:10

Rev316


The memory occupied by Java objects that are no longer accessible may be reclaimed by the virtual machine's garbage collector. As other have noted, this is automatic. In contrast, the normal operation of a program may allocate certain system resources that must be freed explicitly. Native screen resources are an example. A partial list of such methods inlcudes these:

java.awt.Component.BltBufferStrategy#dispose() 
java.awt.Component.FlipBufferStrategy#dispose() 
java.awt.CompositeContext#dispose() 
java.awt.Graphics#dispose() 
java.awt.im.InputContext#dispose() 
java.awt.im.spi.InputMethod#dispose() 
java.awt.image.BufferStrategy#dispose() 
java.awt.Image#flush() 
java.awt.PaintContext#dispose() 
java.awt.Window#dispose() 
java.io.InputStream#close()* 
java.io.OutputStream#close()* 
java.sql.Connection#close() 
java.util.Timer#cancel() 
javax.imageio.ImageReader#dispose() 
javax.imageio.ImageWriter#dispose() 
javax.print.StreamPrintService#dispose() 
javax.security.sasl.SaslClient#dispose() 
javax.security.sasl.SaslServer#dispose() 
javax.swing.DebugGraphics#dispose() 
javax.swing.JInternalFrame#dispose() 
org.ietf.jgss.GSSContext#dispose() 
org.ietf.jgss.GSSCredential#dispose() 
* Includes subclasses
like image 36
trashgod Avatar answered Oct 29 '22 01:10

trashgod