Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8315310

After try-resource closes resources, java.nio.Bits.reservedMemory didn't reduce

XMLWordPrintable

    • x86_64
    • os_x

      ADDITIONAL SYSTEM INFORMATION :
      Darwin gzk0329 21.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000 arm64


      A DESCRIPTION OF THE PROBLEM :
      After try-with-resources closes resources, java.nio.Bits.reservedMemory didn't reduce
      1. Write content to file via output stream.
              File tempFile = new File("/home/tmp/" + tempFileName);
              if (!tempFile.getParentFile().exists()) {
                  tempFile.getParentFile().mkdirs();
              }
              try (OutputStream out = Files.newOutputStream(tempFile.toPath())){
                  out.write(multipartFile.getBytes());
              } catch (IOException e) {
                  e.printStackTrace();
              }
      2. Observe that reservedMemory the field of java.nio.Bits has a significant growth

      3.After multiple uploads, reservedMemory reaches maxMemory, and an exception java.lang.OutOfMemoryError: Direct buffer memory will be thrown

      4.Try..... no problems
             try(FileOutputStream fos = new FileOutputStream(tempFile.getAbsolutePath())) {
                  fos.write(multipartFile.getBytes());
              } catch (IOException e) {
                  e.printStackTrace();
              }

      5.So try-with-resources use with Files.newOutputStream will produce this problems, is there a bug here?

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. try-with-resources with Files.newOutputStream.

        File tempFile = new File("/home/tmp/" + tempFileName);
              if (!tempFile.getParentFile().exists()) {
                  tempFile.getParentFile().mkdirs();
              }
              try (OutputStream out = Files.newOutputStream(tempFile.toPath())){
                  out.write(multipartFile.getBytes());
              } catch (IOException e) {
                  e.printStackTrace();
              }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      reservedMemory the field of java.nio.Bits will not grown
      ACTUAL -
      reservedMemory the field of java.nio.Bits grown and won't recover

      ---------- BEGIN SOURCE ----------
      1.Observe that reservedMemory the field of java.nio.Bits
      File tempFile = new File("/home/tmp/" + tempFileName);
      if (!tempFile.getParentFile().exists()) {
             tempFile.getParentFile().mkdirs();
      }
       try (OutputStream out = Files.newOutputStream(tempFile.toPath())){
             out.write(multipartFile.getBytes());
      } catch (IOException e) {
             e.printStackTrace();
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
       1. this code won't have the problem
       try(FileOutputStream fos = new FileOutputStream(tempFile.getAbsolutePath())) {
              fos.write(multipartFile.getBytes());
       } catch (IOException e) {
             e.printStackTrace();
       }



      FREQUENCY : always


            tongwan Andrew Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: