部署在tomcat上时什么时候调用finalize
我创建了一个简单的 Java 类,如下所示:
我将内容作为字节数组和文件名传递,并且该类在某处创建一个 TempFile。
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class TempFile {
private byte[] content;
private File file;
private String fileName;
public TempFile(byte[] content, String fileName)
throws IOException {
this.content = content;
this.fileName = fileName;
file = createTempFile();
}
private File createTempFile()
throws IOException {
String tmpDir = System.getProperty("java.io.tmpdir");
if(!tmpDir.endsWith("/") || !tmpDir.endsWith("\\"))
tmpDir += "/";
File tmpfile = new File(tmpDir + createUniqueName());
while(tmpfile.exists())
tmpfile = new File(tmpDir + createUniqueName());
tmpfile.createNewFile();
FileUtils.writeByteArrayToFile(tmpfile, content);
return tmpfile;
}
@Override
protected void finalize() throws Throwable {
try {
if(file.exists() && file.canRead() && file.canWrite())
file.delete();
} catch(Throwable t) {
t.printStackTrace();
} finally {
super.finalize();
}
}
我
想如果我在 Finalize 方法中实现清理,那么当 GC 处理对象时,临时文件将被自动删除。
我尝试对此进行调试,但似乎未调用 Finalize 方法。
原因是什么?这可能是因为我正在部署 Tomcat 服务器吗?
干杯
I have created a simple Java class as follows:
I pass content as a byte-array and a Filename and the class creates a TempFile somewhere.
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class TempFile {
private byte[] content;
private File file;
private String fileName;
public TempFile(byte[] content, String fileName)
throws IOException {
this.content = content;
this.fileName = fileName;
file = createTempFile();
}
private File createTempFile()
throws IOException {
String tmpDir = System.getProperty("java.io.tmpdir");
if(!tmpDir.endsWith("/") || !tmpDir.endsWith("\\"))
tmpDir += "/";
File tmpfile = new File(tmpDir + createUniqueName());
while(tmpfile.exists())
tmpfile = new File(tmpDir + createUniqueName());
tmpfile.createNewFile();
FileUtils.writeByteArrayToFile(tmpfile, content);
return tmpfile;
}
@Override
protected void finalize() throws Throwable {
try {
if(file.exists() && file.canRead() && file.canWrite())
file.delete();
} catch(Throwable t) {
t.printStackTrace();
} finally {
super.finalize();
}
}
}
I thought if I implemented the cleanup to happen in the finalize-method, the tempfiles would be automatically deleted when the GC disposes of the object.
I tried debugging this, but it seems the finalize method is not called.
What's the reason? Could this be because I am deploying this a Tomcat server?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
依靠
finalize
来清理资源是一个坏主意。您(通常)无法控制何时发生垃圾收集以及何时调用finalize
。事实上,JVM 甚至根本不保证finalize
会被调用,因此,如果由于某种原因没有被调用,您的资源根本不会被清理。Relying on
finalize
to clean up resources is a bad idea. You (normally) have no control over when garbage collection happens and whenfinalize
is called. In fact, the JVM does not even guarantee at all thatfinalize
will ever be called, so if for some reason it isn't, your resources won't be cleaned up at all.不要依赖终结器(这是《Effective Java》中的一项)。手动调用清理。
Do not rely on the finalizer (that's one item in Effective Java). Invoke the cleanup manually.