United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6632696 Writing to closed output files (writeBytes) leaks native memory (unix)
JDK-6632696 : Writing to closed output files (writeBytes) leaks native memory (unix)

Details
Type:
Bug
Submit Date:
2007-11-21
Status:
Closed
Updated Date:
2011-05-18
Project Name:
JDK
Resolved Date:
2011-05-18
Component:
core-libs
OS:
generic
Sub-Component:
java.io
CPU:
generic
Priority:
P2
Resolution:
Fixed
Affected Versions:
7
Fixed Versions:

Related Reports
Backport:

Sub Tasks

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



Hardware and Software, Engineered to Work Together