private int getMaxMemory ( ) { int total = 0 ; for ( MemoryPoolMXBean mpBean : ManagementFactory . getMemoryPoolMXBeans ( ) ) { if ( mpBean . getType ( ) == MemoryType . HEAP ) { total += mpBean . getUsage ( ) . getMax ( ) >> 20 ; } } return total ; }
Gets the maximum memory heap size for the application . This size can be modified by passing - Xmx option to the virtual machine like java - Xmx5G MyClass . java .