gctoolkit icon indicating copy to clipboard operation
gctoolkit copied to clipboard

[I/O] GCLogFile.diary() open the file 2 times and don't release it

Open MansuyDavid opened this issue 1 year ago • 0 comments

Describe the bug GCLogFile.diary() open the file 2 times and don't release it in GCToolkit 3.0.4

To Reproduce Steps to reproduce the behavior:

  1.          final SingleGCLogFile logFile = new SingleGCLogFile(file.toPath());
             Diary diary = logFile.diary();
    
  2. watch files handles in OS : For example in windows : open "resource monitor", go to tab "CPU", select the java process and look the "associated handles" for the file used. => It appears 2 times.

Expected behavior Opened file should be close after reading, to release the resource. More specifically, the 2 resources are created via Files.lines() in com.microsoft.gctoolkit.io.DataSource.stream() implementations, for example by doing :

Stream<String> stream = Files.lines(metadata.getPath())

Those streams are not closed when used in caller method :

   private TripleState discoverFormat() {
        try {
            boolean isUnified = firstNLines(stream(), SHOULD_HAVE_SEEN_A_UNIFIED_DECORATOR_BY_THIS_LINE_IN_THE_LOG)
                    .map(LINE_STARTS_WITH_DECORATOR::matcher)
                    .anyMatch(Matcher::find);
            return TripleState.valueOf(isUnified);
        } catch(IOException ioe) {
            ...
        }
        ...
   }

Can be closed via :

   private TripleState discoverFormat() {
      try (Stream<String> stream = stream()){
         boolean isUnified = firstNLines(stream, SHOULD_HAVE_SEEN_A_UNIFIED_DECORATOR_BY_THIS_LINE_IN_THE_LOG)
               .map(LINE_STARTS_WITH_DECORATOR::matcher)
               .anyMatch(Matcher::find);
         return TripleState.valueOf(isUnified);
      } catch(IOException ioe) {
         ...
      }
      ...
   }

This is the second resource opened in my case GCLogFile.diary() :

                stream()
                    .filter(Objects::nonNull)
                    .map(String::trim)
                    .filter(s -> s.length() > 0)
                    .map(diarizer::diarize)
                    .filter(completed -> completed)
                    .findFirst();

Desktop (please complete the following information):

  • OS: Windows
  • Version GCToolkit 3.0.4

MansuyDavid avatar Feb 09 '24 16:02 MansuyDavid