JDK-4919795 : RandomAccessFile doesn't close when it gets garbage collected
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_98
  • CPU: x86
  • Submitted: 2003-09-09
  • Updated: 2003-09-09
  • Resolved: 2003-09-09
Related Reports
Duplicate :  
Description

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) 
======================================================================

Comments
EVALUATION This bug report does not suggest adding a finalize() method to RandomAccessFile, but any other GC-driven solution will have the same performance overhead as an actual finalize() method (see, e.g., 4777499). This bug was therefore closed as a duplicate of 4081750. -- ###@###.### 2003/9/15
09-11-0181