Not sure what part of my code causes the issues exactly, but when I started pinpointing the issue, I noticed that open file count increases when I run the following code.
public synchronized String readStringSync(String basePath,String path){
if(useLegacy){
return readStringLegacy(basePath,path);
}
if(!basePath.endsWith("/"))
basePath+="/";
StringBuffer sb = new StringBuffer(420);
File f= new File(basePath+path);
if(!f.exists()){
return null;
}
Charset charset = Charset.forName("UTF-8");
try (BufferedReader reader = Files.newBufferedReader(Paths.get(f.getAbsolutePath()), charset)) {
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line+"\r\n");
}
String ret = sb.toString();
if(ret.trim().startsWith("deleted")||ret.trim().equalsIgnoreCase("dummy"))
return null;
return ret;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public static void main(String... args) throws Exception{
com.sun.management.UnixOperatingSystemMXBean mxb = (com.sun.management.UnixOperatingSystemMXBean)java.lang.management.ManagementFactory.getOperatingSystemMXBean();
System.out.println(mxb.getOpenFileDescriptorCount()); //11
System.out.println(readString("sometestfile"));
Thread.sleep(1000);
mxb = (com.sun.management.UnixOperatingSystemMXBean)java.lang.management.ManagementFactory.getOperatingSystemMXBean();
System.out.println(mxb.getOpenFileDescriptorCount()); //12 wtf
}
I already tried getting rid of File.exists and replacing it with Files.exists, but that did absolutely nothing.
As the stackoverflow is still complaining about the lack of details, here's the java version I am using:
java version "1.7.0_51" OpenJDK Runtime Environment (IcedTea 2.4.4) (7u51-2.4.4-0ubuntu0.13.04.2) OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)
useLegacy
? Problem might be insidereadStringLegacy()
. – iczafor
loop with like 1000 iterations, and inside that call the samereadStringSync()
to read the same file. Print what will be the open files count after that. – icza