import java.nio.ByteBuffer; import java.util.Date; import sun.misc.VM; public class DirectByteBufferExample { private static volatile long maxMemory = VM.maxDirectMemory(); public static void main (String [] args) throws Exception { System.out.println("System classpath "+System.getProperty("java.class.path")); System.out.println("Direct Memory Size "+ VM.maxDirectMemory() +" "+maxMemory); int COUNT = 32; long startTime = new Date().getTime(); if (args.length > 0) { System.out.println("Creating 1 GB memory which will be released if GCed"); ByteBuffer buffer = null; for (int i = 0; i < COUNT; i++) { try { buffer = ByteBuffer.allocateDirect(1 * 1024 * 1024 * 1024); } catch(OutOfMemoryError e) { if (args.length > 1) { try { Thread.sleep(100); } catch (InterruptedException x) { Thread.currentThread().interrupt(); } continue; } else throw(e); } System.out.println("Direct Memory Size "+ VM.maxDirectMemory() +" "+maxMemory); } } long endTime = new Date().getTime(); System.out.println(""); System.out.println("Time taken (millis): " + (endTime - startTime)); } }