java 句柄泄露_如何检查Java程序的文件句柄泄漏?
由于没有已经给出,我加入我的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.con
由于没有已经给出,我加入我的
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
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)