public static long getDirectMemorySize() {
    RuntimeMXBean RuntimemxBean = ManagementFactory.getRuntimeMXBean();
    List<String> arguments = RuntimemxBean.getInputArguments();
    long multiplier = 1; //for the byte case.
    for (String s : arguments) {
      if (s.contains("-XX:MaxDirectMemorySize=")) {
        String memSize = s.toLowerCase()
            .replace("-xx:maxdirectmemorysize=", "").trim();

        if (memSize.contains("k")) {
          multiplier = 1024;
        }

        else if (memSize.contains("m")) {
          multiplier = 1048576;
        }

        else if (memSize.contains("g")) {
          multiplier = 1073741824;
        }
        memSize = memSize.replaceAll("[^\\d]", "");

        long retValue = Long.parseLong(memSize);
        return retValue * multiplier;
      }

    }
    return 0;
  }
public static long getDirectMemorySize ( ) { RuntimeMXBean RuntimemxBean = ManagementFactory . getRuntimeMXBean ( ) ; List < String > arguments = RuntimemxBean . getInputArguments ( ) ; long multiplier = 1 ; for ( String s : arguments ) { if ( s . contains ( "-XX:MaxDirectMemorySize=" ) ) { String memSize = s . toLowerCase ( ) . replace ( "-xx:maxdirectmemorysize=" , "" ) . trim ( ) ; if ( memSize . contains ( "k" ) ) { multiplier = 1024 ; } else if ( memSize . contains ( "m" ) ) { multiplier = 1048576 ; } else if ( memSize . contains ( "g" ) ) { multiplier = 1073741824 ; } memSize = memSize . replaceAll ( "[^\\d]" , "" ) ; long retValue = Long . parseLong ( memSize ) ; return retValue * multiplier ; } } return 0 ; }
@return the setting of -XX:MaxDirectMemorySize as a long. Returns 0 if
-XX:MaxDirectMemorySize is not set.