0
votes

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)

2
What is the value of useLegacy? Problem might be inside readStringLegacy().icza
tried adding if(true)return null; just before the try block => file count stays at 11, so it's def about something that the try block doesSeppo420
Write a for loop with like 1000 iterations, and inside that call the same readStringSync() to read the same file. Print what will be the open files count after that.icza
tried making the wait period 10 seconds + prefacing it with System.gc(), no effectSeppo420
icza, still 12 open filesSeppo420

2 Answers

1
votes

The try-with-resources statement properly closes the stream/reader, you should not worry about that.

If you experience that files are kept open, it is not due to a bug in your code. If you have relatively low open files system-wide, your OS or even JVM might keep them open for a while for performance reasons, and close them if this becomes an issue. From your side, you can't do more, you properly close it by using the try-with-resources statement.

0
votes

Got rid of the java.nio.parts and replaced the try line with: try (BufferedReader reader = new BufferedReader(new FileReader(basePath+path))){

The file count stays at 11 now, but not sure if the issues that I started using java.nio for are back & not sure yet if this fixes the main issue.