JDK-8133021 : DirectMemory is not getting released resulting in out of memory exception
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 7u80
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux_ubuntu
  • CPU: x86_64
  • Submitted: 2015-07-28
  • Updated: 2015-08-20
  • Resolved: 2015-08-20
Related Reports
Duplicate :  
java version "1.7.0_80-ea"
Java(TM) SE Runtime Environment (build 1.7.0_80-ea-b05)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b07, mixed mode)

Linux  3.2.0-70-generic #105-Ubuntu SMP Wed Sep 24 19:49:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

When running a test program to allocate Direct Memory of 1 GB in the following loop, we are getting a OOM error if we execute the logic where the OOM error is not handled and gc is not called explicitly. When we make an explicit gc call the allocation goes through fine. Looking at the java.nio.Bits code (https://github.com/openjdk-mirror/jdk/blob/jdk7u/jdk7u6/master/src/share/classes/java/nio/Bits.java#L649) there is an System.gc() call and this doesn't seem to have any effect in the Ubuntu platform but works fine in redhat.

            for (int i = 0; i < 32; i++) {
             try {
               buffer = ByteBuffer.allocateDirect(1 * 1024 * 1024 * 1024);
             } catch(OutOfMemoryError e) {
               if (args.length > 1) {
               } else
Exception in thread "main" java.lang.OutOfMemoryError: Direct buffer memory
 at java.nio.Bits.reserveMemory(Bits.java:658)
 at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:123)
 at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:306)
 at DirectByteBufferExample.main(DirectByteBufferExample.java:37)

This bug can be reproduced always.

The long standing recommendation (since JDK 1.4) is to cache references to direct memory. It would be useful to know if the test case is representative of a real-world library or application or whether the test case was created to make the point that System.gc + referencing processing approach might not be done before the next attempted allocation.

Seems to be a duplicate of JDK-6857566 We've already got a request to backport that fix to 8u and 7u: http://mail.openjdk.java.net/pipermail/jdk7u-dev/2015-August/010360.html

Re-open to assess further based upon submitter's response.

Received following response from the submitter: ================================================================================== On 8/5/2015 9:00 PM, ........... wrote: > Thanks Pradeep for the update. Definitely this is an issue on Ubuntu 12.04 LTS using Open JDK and Oracle JDK. How to go about fixing it? > Thanks, > .......... > On Wed, Aug 5, 2015 at 12:33 AM, ............ wrote: > > Hi ......, > > I ran this in Oracle Linux x86_64 using kernel 3.8. > > Thank You, > .............. > > On 7/31/2015 7:10 PM, .......... wrote: >> Hi ..........., >> What OS are you running this on? I can run this successfully on red hat linux with Linux kernel 2.6.x and it fails on Ubuntu 12.04 LTS which uses Linux kernel 3.2. As I mentioned earlier, the sleep time which is hardcoded doesn't seem to be enough for Ubuntu to complete the gc in "Bits" class while it is fine on Red Hat. Hope this helps. Please let me know if you have any other questions. >> Thanks, >> ....... >> On Fri, Jul 31, 2015 at 4:08 AM, ..............wrote: >> >> Hi...., >> >> I ran the program shared by you and couldn't reproduce the exception. >> Result with JDK 7u80: >> > java DirectByteBufferExample >> System classpath . >> Direct Memory Size 1840250880 1840250880 >> >> Time taken (millis): 0 >> >> Thank You, >> .............. >> =====================================================================================

1. Run the attached test case (DirectByteBufferExample.java) in Orcale Linux (64-bit). 2. Checked this for JDK 7u80, 7u85, 8u51, 8u60 ea b26, and 9 ea b75. 7u80: FAIL 7u85: FAIL 8u51: FAIL 8u60 ea b26: FAIL 9 ea b75: FAIL 3. Output with JDK 8u51: System classpath . Direct Memory Size 3477078016 3477078016 Time taken (millis): 0 4. The issue is not reproducible as reported by the submitter. Could this be OS specific concern? ------------------------------------------------------------ Sent an email to the submitter requesting additional information.