JDK-6632696 : Writing to closed output files (writeBytes) leaks native memory (unix)
  • Type: Bug
  • Status: Closed
  • Resolution: Fixed
  • Component: core-libs
  • Sub-Component: java.io
  • Priority: P2
  • Affected Version: 7
  • OS: generic
  • CPU: generic
  • Submit Date: 2007-11-21
  • Updated Date: 2011-05-18
  • Resolved Date: 2011-05-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availabitlity Release.

To download the current JDK release, click here.
JDK 7
7 b25Fixed
Description
The function
./src/share/native/java/io/io_util.c:writeBytes
leaks memory if the stream is closed and more than 8kb of data is to be written

Self-explanatory shell transcript follows:

 $ cat ioLeak.java; echo ---; (ulimit -v 200000; jver 7 jr -Xmx6m ioLeak)
import java.io.*;

public class ioLeak {
    public static void main(String[] args) throws Throwable {
        FileOutputStream s = new FileOutputStream(new File("foo"));
        s.close();
        for (int n = 0; n < 10000 ; n++) {
            try { s.write(new byte[100000]); }
            catch (IOException e) { /* ignore */ }
            catch (OutOfMemoryError e) {
                System.out.println(n);
                return;
            }
        }
    }
}
---
==> javac ioLeak.java
==> java -Xmx6m ioLeak
730

Comments
EVALUATION Here's the obvious fix: --- /u/martin/ws/dolphin/src/share/native/java/io/io_util.c 2007-05-13 03:08:26.882713000 -0700 +++ /u/martin/ws/process/src/share/native/java/io/io_util.c 2007-11-20 20:25:09.024661000 -0800 @@ -173,7 +173,7 @@ fd = GET_FD(this, fid); if (fd == -1) { JNU_ThrowIOException (env, "Stream Closed"); - return; + break; } n = IO_Write(fd, buf+off, len); if (n == JVM_IO_ERR) {
2007-11-21