public static long getTotalPhysicalMemory ( ) { long memory = - 1 ; try { MBeanServer mBeanServer = ManagementFactory . getPlatformMBeanServer ( ) ; Object attribute = mBeanServer . getAttribute ( new ObjectName ( "java.lang" , "type" , "OperatingSystem" ) , "TotalPhysicalMemorySize" ) ; return ( Long ) attribute ; } catch ( Throwable e ) { e . printStackTrace ( ) ; } return memory ; }
Safe call to obtain size of total physical memory .