开发者

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

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_StackOverflow中文版 Closed 10 years ago.

Can you give me one example for my question?


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).


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


There is not a method per se. The finalize() method, defined in the top-level Object, may be invoked when the object is deallocated by the garbage collector, but this is not a behavior you can rely upon.


Java does not support deterministic instance finalization (i.e. C++'s destructors). Java has a garbage collector that is invokable by you (like this Runtime.getRuntime().gc()) but garbage-collected runtimes tend to work best when you leave memory cleanup alone.


This explains how garbage collection (why you don't have to delete objects explicitly in Java) works: http://web.archive.org/web/20080205091525/http://chaoticjava.com/posts/how-does-garbage-collection-work/

Check it out, ask a follow up question if it would help.


Java automatically handles all its dynamic memory allocation and deallocation. Essentially any memory that is no longer referenced by a named variable becomes eligible for garbage collection (there are no anonymous variables). This will be the case when all variables referring to that memory are set to something else or go out of scope.

Garbage collection will happen automatically, but there is no specific time at which any piece of memory will actually be deallocated.

Most people won't need to know more than that, but the link previously posted by corprew gives a nice amount of detail in an intuitive format for anyone interested in learning more.


"object destory" is automatically done by jvm in java, when the object is eligible for garbage collector.

for example..

public class gc{
  public static void main(String []s){
     gc obj=new gc();
     gc obj1=new gc();
     // if u made manually obj1=null. its eligible for garbage collection else  jvm done automatically when this obj1 not in use..

       obj1=null;
 }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜