0
我的java代码似乎有句柄泄漏,哪个调试工具适合检查?如何检查Java程序的文件句柄泄漏?
我的java代码似乎有句柄泄漏,哪个调试工具适合检查?如何检查Java程序的文件句柄泄漏?
lsof命令将列出与程序关联的所有文件。
由于没有已经给出,我加入我的
package org.gradle;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class FileDescriptorDemoOne {
static int index_count;
public static void main(String[] args) throws IOException {
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(new Runnable() {
public void run() {
index_count++;
// do stuff
File file = new File("/tmp/helloworld.txt");
FileDescriptor fd;
FileOutputStream fos1;
try {
fos1 = new FileOutputStream(file);
fd = fos1.getFD();
//passing FileDescriptor to another FileOutputStream
FileOutputStream fos2 = new FileOutputStream(fd);
fos2.write(index_count++);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 0, 5, TimeUnit.SECONDS);
}
}
显然上面的代码会泄漏文件描述符每隔几秒钟。
为了赶上这个运用
lsof的| grep的HelloWorld的
java 6015 vic 8w REG 1,4 12 86076888 /private/tmp/helloworld.txt
java 6015 vic 9w REG 1,4 12 86076888 /private/tmp/helloworld.txt
java 6015 vic 10w REG 1,4 12 86076888 /private/tmp/helloworld.txt
分享你的代码,以便其他人可以看到你在说什么。 – 2012-07-31 23:07:45
让我猜:“FileNotFoundException:太多打开的文件”? 如果在java调试器*中有更多的方法来跟踪打开的文件句柄,那将会很不错。大多数时候单独使用“lsof”是不足以真正追踪这样的错误的。 + 1'd – 2012-08-01 05:52:48