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

RandomAccessFile doesn't close when it gets garbage collected

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.2
    • core-libs
    • x86
    • windows_98



      Name: rmT116609 Date: 09/09/2003


      FULL PRODUCT VERSION :
      java version "1.4.2_01"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_01-b06)
      Java HotSpot(TM) Client VM (build 1.4.2_01-b06, mixed mode)

      FULL OS VERSION :
      Microsoft Windows 2000 [Version 5.00.2195], Windows ME.

      A DESCRIPTION OF THE PROBLEM :
      RandomAccessFile doesn't close when it gets garbage collected.
      Both FileInputStream and FileOutputStream will close when they get garbage collected, however RandomAccessFile does not.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      run code and wait

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Code to run fine without any error message.
      ACTUAL -
      After the line RAF count == 2036 is displayed the error occurs.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.io.IOException: Cannot create a file when that file already exists

      at java.io.RandomAccessFile.open(Native Method)

      at java.io.RandomAccessFile.<init>(RandomAccessFile.java:204)

      at java.io.RandomAccessFile.<init>(RandomAccessFile.java:94)

      at Test.main(Test.java:22)


      java.io.FileNotFoundException: c:\temp.txt (Too many open files)
      at java.io.RandomAccessFile.open(Native Method)
      at java.io.RandomAccessFile.<init>(Unknown Source)
      at java.io.RandomAccessFile.<init>(Unknown Source)



      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.*;

      public class Test {
        public static void main(final String[] args) {
          try {
            for (int i = 0; i < 3000; i++) {
              System.out.println("FOS count == " + i);
              new FileOutputStream("c:/temp.txt");
              System.gc();
              System.runFinalization();
            }
            for (int i = 0; i < 3000; i++) {
              System.out.println("FIS count == " + i);
              new FileInputStream("c:/temp.txt");
              System.gc();
              System.runFinalization();
            }
            for (int i = 0; i < 3000; i++) {
              System.out.println("RAF count == " + i);
              new RandomAccessFile("c:/temp.txt", "rw");
              System.gc();
              System.runFinalization();
            }
          }
          catch (IOException ex) {
            ex.printStackTrace();
          }
          System.out.println("Done");
        }
      }
      ---------- END SOURCE ----------
      (Incident Review ID: 200878)
      ======================================================================

            mmcclosksunw Michael Mccloskey (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: