long () { try { Class<?> aClass = Class.forName("jdk.internal.misc.VM"); Method maxDirectMemory = aClass.getMethod("maxDirectMemory"); return (Long)maxDirectMemory.invoke(null); } catch (Throwable ignore) { } try { Class<?> aClass = Class.forName("sun.misc.VM"); Method maxDirectMemory = aClass.getMethod("maxDirectMemory"); return (Long)maxDirectMemory.invoke(null); } catch (Throwable ignore) { } try { Class<?> aClass = Class.forName("java.nio.Bits"); //noinspection JavaReflectionMemberAccess Field maxMemory = aClass.getDeclaredField("maxMemory"); maxMemory.setAccessible(true); return (Long)maxMemory.get(null); } catch (Throwable ignore) { } try { Class<?> aClass = Class.forName("java.nio.Bits"); Field maxMemory = aClass.getDeclaredField("MAX_MEMORY"); maxMemory.setAccessible(true); return (Long)maxMemory.get(null); } catch (Throwable ignore) { } return Runtime.getRuntime().maxMemory(); }