部署在tomcat上时什么时候调用finalize

发布于 2024-11-16 23:21:22 字数 1325 浏览 2 评论 0原文

我创建了一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

澉约 2024-11-23 23:21:22

依靠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 when finalize is called. In fact, the JVM does not even guarantee at all that finalize will ever be called, so if for some reason it isn't, your resources won't be cleaned up at all.

半边脸i 2024-11-23 23:21:22

不要依赖终结器(这是《Effective Java》中的一项)。手动调用清理。

Do not rely on the finalizer (that's one item in Effective Java). Invoke the cleanup manually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文