JDK-6749531 : ByteBuffer.allocateDirect is still causing OutOfMemoryErrors
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 6u10
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2008-09-17
  • Updated: 2010-04-26
  • Resolved: 2008-09-18
Related Reports
Duplicate :  
Description
J2SE Version (please include all output from java -version flag):
java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)

Same thing for 6u10 RC2 release

Does this problem occur on J2SE 5.0.x or 6.0?  Yes / No (pick one)
Yes

Operating System Configuration Information (be specific):
Microsoft Windows XP [Version 5.1.2600]

Bug Description:
ByteBuffer.allocateDirect is still causing OutOfMemoryErrors

I found lots of Bug Reports with the same Test Case, each closed saying fixed in much earlier versions of Java (like 1.4).
I still get the OutOfMemoryErrors every single time running on 1.6.0_07.

Expected: The code below should run to completion as the code is never holding on to any of the ByteBuffers.

>java -Xmx1g Test

Actual: Allocation Size: 107039948
0
1
2
3
4
Exception in thread "main" java.lang.OutOfMemoryError
        at sun.misc.Unsafe.allocateMemory(Native Method)
        at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:99)
        at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:288)
        at Test.main(Test.java:9)


import static java.lang.System.*;
import static java.nio.ByteBuffer.*;

public class Test {
  public static void main(String[] args) {
    int allocationSize = (int)(Runtime.getRuntime().maxMemory() / 10);
    out.println("Allocation Size: " + allocationSize);
    for (int i = 0; i < 10000; i++) {
      allocateDirect(allocationSize);
      out.println(i);
    }
  }
}