Java中的什么方法用于销毁您的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2486136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What method in Java is used to destroy your objects
提问by chandra
Can you give me one example for my question?
你能给我举一个例子来回答我的问题吗?
回答by mipadi
There is not a method per se. The finalize()
method, defined in the top-level Object
, maybe invoked when the object is deallocated by the garbage collector, but this is not a behavior you can rely upon.
本身没有方法。该finalize()
方法,在顶层定义Object
,可以被调用,当对象被垃圾收集器释放,但这不是你可以依靠一个行为。
回答by Andrew Hare
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.
Java 不支持确定性实例终结(即 C++ 的析构函数)。Java 有一个可由您调用的垃圾收集器(像这样Runtime.getRuntime().gc()
),但是当您单独进行内存清理时,垃圾收集运行时往往效果最佳。
回答by Rev316
Sorry, but there isn't really a "free" or "dispose" equivalent in Java.
抱歉,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
).
您能做的最好的事情就是将对象设置为 null(删除引用)。然后明确地告诉垃圾收集器你要去 rambo(它在 中的某个地方java.lang.Runtime
)。
回答by corprew
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/
这解释了垃圾收集(为什么您不必在 Java 中显式删除对象)的工作原理:http: //web.archive.org/web/20080205091525/http: //chaoticjava.com/posts/how-does-garbage -收集工作/
Check it out, ask a follow up question if it would help.
检查一下,询问后续问题是否有帮助。
回答by David Mason
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.
Java 自动处理其所有动态内存分配和释放。基本上任何不再被命名变量引用的内存都可以进行垃圾回收(没有匿名变量)。当所有引用该内存的变量都设置为其他内容或超出范围时,就会出现这种情况。
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 corprewgives a nice amount of detail in an intuitive format for anyone interested in learning more.
大多数人不需要知道更多,但是corprew之前发布的链接以直观的格式为任何有兴趣了解更多信息的人提供了大量详细信息。
回答by trashgod
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 listof such methods inlcudes these:
不再可访问的 Java 对象所占用的内存可能会被虚拟机的垃圾收集器回收。正如其他人所指出的,这是自动的。相反,程序的正常运行可能会分配某些必须显式释放的系统资源。本机屏幕资源就是一个例子。此类方法的部分列表包括:
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
回答by Manu
"object destory" is automatically done by jvm in java, when the object is eligible for garbage collector.
当对象符合垃圾收集器的条件时,“对象销毁”由 Java 中的 jvm 自动完成。
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;
}
}