-1
在下面的最小代码中可以看到,我想在JAVA对象完成时,测试变量中分配的对象将被分配。我可以做些什么来取消分配这个对象? 我用null
和system.gc
,但它们不能解除分配对象。如何在java中释放分配的对象?
package chatclient;
/**
*
* @author root
*/
import java.io.IOException;
public class ChatClient {
private void run() throws IOException {
long test[]=new long[10000000];
test[10000]=100;
// Process all messages from server, according to the protocol.
test=null;
System.gc();
}
@Override
protected void finalize() throws Throwable {
super.finalize(); //To change body of generated methods, choose Tools | Templates.
}
/**
* Runs the client as an application with a closeable frame.
*/
public static void main(String[] args) throws Exception {
while(true){
ChatClient client = new ChatClient();
client.run();
}
}
}
你不能,明确。 –
为了澄清上述评论:[阅读此问题](http://stackoverflow.com/questions/66540/when-does-system-gc-do-anything)。基本上,'System.gc()'调用仅仅是JVM的一个暗示,也许,如果它觉得它可能会运行垃圾收集。虽然没有压力。 – Phylogenesis
请**编辑**问题以说明您的意思是“不工作”。你在看什么让你相信对象没有被“释放”?由于“免费”是一个C概念,你在这里意味着什么? – slim