JVM Tutorials - Herong's Tutorial Examples - Version 4.23, by Dr. Herong Yang
Direct Memory Expansion Demonstration
This section provides a tutorial example to show the expansion of Direct Memory area when more objects are allocated. When Direct Memory runs out of space, JVM throws the OutOfMemoryError exception.
The next runtime data area I want to learn more is the Direct Memory area. Here are my understandings collected so far:
To validate some of these understandings and demonstrate the Direct Memory usage and expansion, I wrote the following sample program, DirectMemoryCrash.java, forcing the JVM run out of memory on the Direct Memory:
/* DirectMemoryCrash.java * Copyright (c) 2014, HerongYang.com, All Rights Reserved. */ class DirectMemoryCrash { public static void main(String[] a) { int max = 1000; Object[] arr = new Object[max]; heapCheck(); for (int n=0; n<max; n++) { arr[n] = java.nio.ByteBuffer.allocateDirect(10*1024*1024); System.out.println((n+1)+": " +((n+1)*10)+" MB of objects created."); heapCheck(); } } public static void heapCheck() { Runtime rt = Runtime.getRuntime(); rt.gc(); long total = rt.totalMemory(); long free = rt.freeMemory(); long used = total - free; java.io.Console con = System.console(); con.format("Total memory: %s%n",total); con.format(" Free memory: %s%n",free); con.format(" Used memory: %s%n",used); String str = con.readLine("Press ENTER key to continue: "); } }
This program, DirectMemoryCrash.java, is designed in the same way as HeapMemoryCrash.java. The only difference is that DirectMemoryCrash.java uses java.nio.ByteBuffer.allocateDirect(10*1024*1024) to allocate the 10 MB long ByteBuffer object in the Direct Memory data area.
Compile and run with HotSpot JVM 1.8:
C:\herong>\progra~1\java\jdk1.8.0\bin\javac DirectMemoryCrash.java C:\herong>\progra~1\java\jdk1.8.0\bin\java DirectMemoryCrash Total memory: 16318464 Free memory: 16059952 Used memory: 258512 Press ENTER key to continue: (tasklist output: java.exe 9356 Console 1 14,856 K) 1: 10 MB of objects created. Total memory: 16318464 Free memory: 16028376 Used memory: 290088 Press ENTER key to continue: (tasklist output: java.exe 9356 Console 1 25,192 K) 2: 20 MB of objects created. Total memory: 16318464 Free memory: 16028248 Used memory: 290216 Press ENTER key to continue: (tasklist output: java.exe 9356 Console 1 35,456 K) 3: 30 MB of objects created. Total memory: 16318464 Free memory: 16048992 Used memory: 269472 Press ENTER key to continue: (tasklist output: java.exe 9356 Console 1 45,720 K) ... 24: 240 MB of objects created. Total memory: 16318464 Free memory: 16046304 Used memory: 272160 Press ENTER key to continue: (tasklist output: java.exe 9356 Console 1 261,336 K) 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:311) at DirectMemoryCrash.main(DirectMemoryCrash.java:10)
The output confirms that:
Now run it again with a lower Direct Memory Area size limit:
C:\herong>\progra~1\java\jdk1.8.0\bin\java -Xmx30m DirectMemoryCrash Total memory: 16318464 Free memory: 16059952 Used memory: 258512 Press ENTER key to continue: 1: 10 MB of objects created. Total memory: 16318464 Free memory: 16028376 Used memory: 290088 Press ENTER key to continue: 2: 20 MB of objects created. Total memory: 16318464 Free memory: 16028248 Used memory: 290216 Press ENTER key to continue: 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:311) at DirectMemoryCrash.main(DirectMemoryCrash.java:10)
This confirms that "-Xmx30m" sets the maximum limit on both Heap area and Direct Memory area.
Last update: 2014.
Table of Contents
Downloading and Installing JDK 1.8.0 on Windows
Downloading and Installing JDK 1.7.0 on Windows
java.lang.Runtime Class - The JVM Instance
java.lang.System Class - The Operating System
ClassLoader Class - Class Loaders
Class Class - Class Reflections
JRockit JVM 28.2.7 by Oracle Corporation
Method Area Expansion Demonstration
OutOfMemoryError on the Method Area
Method Area Growth with Dynamically Generated Classes
Garbage Collection Issue with Dynamically Generated Classes
Interned Strings Stored in Heap
►Direct Memory Expansion Demonstration
allocateMemory() Method on Direct Memory
JVM Stack Expansion Demonstration
PC Register and Native Method Stack
Memory Management and Garbage Collectors
JVM Stack, Frame and Stack Overflow
Thread Testing Program and Result
CPU Impact of Multi-Thread Applications
I/O Impact of Multi-Thread Applications
Micro Benchmark Runner and JVM Options
Micro Benchmark Tests on "int" Operations
Micro Benchmark Tests on "long" Operations
Micro Benchmark Tests in JIT Compilation Mode