Exception in thread
"xDispatcher#CLIENT" 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 org.xsocket.connection.spi.AbstractMemoryManager.newBuffer(AbstractMemoryManager.java:219) at....... |
DirectByteBuffer(int cap)
{ super(-1, 0, cap, cap, false); Bits.reserveMemory(cap); int ps = Bits.pageSize(); long base = 0; try { base = unsafe.allocateMemory(cap + ps); } catch (OutOfMemoryError x) { Bits.unreserveMemory(cap); throw x; } unsafe.setMemory(base, cap + ps, (byte) 0); if (base % ps != 0) { // Round up to page boundary address = base + ps - (base & (ps - 1)); } else { address = base; } cleaner = Cleaner.create(this, new Deallocator(base, cap)); } |
// -- Direct memory
management -- // A user-settable upper limit on the maximum amount of allocatable // direct buffer memory. This value may be changed during VM // initialization if it is launched with "-XX:MaxDirectMemorySize=<size>". private static volatile long maxMemory = VM.maxDirectMemory(); private static volatile long reservedMemory = 0; private static boolean memoryLimitSet = false; // These methods should be called whenever direct memory is allocated or // freed. They allow the user to control the amount of direct memory // which a process may access. All sizes are specified in bytes. static void reserveMemory(long size) { synchronized (Bits.class) { if (!memoryLimitSet && VM.isBooted()) { maxMemory = VM.maxDirectMemory(); memoryLimitSet = true; } if (size <= maxMemory - reservedMemory) { reservedMemory += size; return; } } System.gc(); try { Thread.sleep(100); } catch (InterruptedException x) { // Restore interrupt status Thread.currentThread().interrupt(); } synchronized (Bits.class) { if (reservedMemory + size > maxMemory) throw new OutOfMemoryError("Direct buffer memory"); reservedMemory += size; } } |
private static long directMemory =
67108864L; ... public static long maxDirectMemory() { if (booted) return directMemory; Properties localProperties = System.getProperties(); String str = (String)localProperties.remove("sun.nio.MaxDirectMemorySize"); System.setProperties(localProperties); if (str != null) if (str.equals("-1")) { directMemory = Runtime.getRuntime().maxMemory(); } else { long l = Long.parseLong(str); if (l > -1L) directMemory = l; } return directMemory; } |
import
java.lang.management.ManagementFactory; import java.nio.ByteBuffer; import java.util.logging.Logger; import com.sun.management.OperatingSystemMXBean; public class TestMemoryLeak { private static Logger logger = Logger.getAnonymousLogger(); public static void main(String[] args) throws Exception{ OperatingSystemMXBean osmb = (OperatingSystemMXBean) ManagementFactory .getOperatingSystemMXBean(); System.out.println("Total physic memory:" + osmb.getTotalPhysicalMemorySize() / 1024 / 1024 + "MB"); System.out.println("Free physic memory:" + osmb.getFreePhysicalMemorySize() / 1024 / 1024 + "MB"); System.out.println("Max memory:" + Runtime.getRuntime().maxMemory()); System.out .println("Total memory:" + Runtime.getRuntime().totalMemory()); System.out.println("Free memory:" + Runtime.getRuntime().freeMemory()); System.out.println("===================================="); //testDirectByteBuffer(); testByteBuffer(); System.out.println("===================================="); System.out.println("Free physic memory:" + osmb.getFreePhysicalMemorySize() / 1024 / 1024 + "MB"); } public static void testDirectByteBuffer(){ try{ ByteBuffer bb1 = ByteBuffer.allocateDirect(1024 * 100000 * 4); bb1 = null; System.out.println("Free memory:" + Runtime.getRuntime().freeMemory()); ByteBuffer bb2 = ByteBuffer.allocateDirect(1024 * 100000 * 5); bb2 = null; System.out.println("Free memory:" + Runtime.getRuntime().freeMemory()); //System.gc(); //pause expect for gc Thread.sleep(1000*6); ByteBuffer bb3 = ByteBuffer.allocateDirect(1024 * 100000 * 8); System.out.println("Free memory:" + Runtime.getRuntime().freeMemory()); }catch(Exception e){ e.printStackTrace(); } } public static void testByteBuffer(){ try{ ByteBuffer bb1 = ByteBuffer.allocate(1024 * 100000 * 4); bb1 = ByteBuffer.allocate(1024 * 10 * 4); System.out.println("Free memory:" + Runtime.getRuntime().freeMemory()); ByteBuffer bb2 = ByteBuffer.allocate(1024 * 100000 * 3); bb1 = ByteBuffer.allocate(1024 * 10 * 3); System.out.println("Free memory:" + Runtime.getRuntime().freeMemory()); //System.gc(); //pause expect for gc Thread.sleep(1000*6); ByteBuffer bb3 = ByteBuffer.allocate(1024 * 100000 * 6); System.out.println("Free memory:" + Runtime.getRuntime().freeMemory()); }catch(Exception e){ e.printStackTrace(); } } } |