java释放文件,释放java文件句柄

We have a rather large and complex application written in Java which is running on top of the Gridgain package. The problem I am having is that this application will sit there processing requests for approximately a day before every request starts resulting in an exception of type java.nio.channels.ClosedByInterruptException.

My supposition is that the application is not releasing file handles and after a day of continuous usage it runs out and can no longer continue to process requests (each request requires the reading of multiple files from each grid node). We have wrapped most of our file IO operations in classes such as this one

package com.vlc.edge;

import com.vlc.common.VlcRuntimeException;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.io.Reader;

public final class BufferedReaderImpl implements BufferedReader {

private java.io.BufferedReader reader;

public BufferedReaderImpl(final String source) {

this(new File(source));

}

public BufferedReaderImpl(final File source) {

try {

reader = new java.io.BufferedReader(new FileReader(source));

} catch (FileNotFoundException e) {

throw new VlcRuntimeException(e);

}

}

public BufferedReaderImpl(final Reader reader) {

this.reader = new java.io.BufferedReader(reader);

}

public String readLine() {

try {

return reader.readLine();

} catch (IOException e) {

throw new VlcRuntimeException(e);

}

}

public void close() {

try {

reader.close();

} catch (IOException e) {

throw new VlcRuntimeException(e);

}

}

}

I think the problem is that this design doesn't explicitly release the file handle, my proposed solution is to add a finalize method like this

protected void finalize() throws Throwable

{

reader.close();

super.finalize();

}

which will do this explicitly. The question (finally) is whether or not this is likely to have any effect. Do classes such as java.io.BufferedReader already have some mechanism for dealing with this type of problem?

EDIT: Also greatly appreciated here would be ways of checking whether this is actually the problem... ie is there a way to query a running JVM and ask about it's file handle allocations?

解决方案

There is little point in overriding finalize(). If the handle is getting garbage collected and finalized, then so is the instance of java.io.BufferedReader and it will get closed.

It is possible (according to the spec) that the handle is being garbage collected but not finalized, but this is not very likely.

You could try using PhantomReferences to clean up unused file handles, but my guess is that your instances of BufferedReaderImpl are still referenced from somewhere (e.g. values in a Map from filenames to open handles) and that is what is preventing them from being closed (in which case finalizers will not help.)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值