code: public static int getTotalPhysicalMemorySizeInMB() {
  
      int memorySizeInMB;
      try {
        long memorySizeInBytes = ((com.sun.management.OperatingSystemMXBean) ManagementFactory
                      .getOperatingSystemMXBean()).getTotalPhysicalMemorySize();
  
        memorySizeInMB = (int) (memorySizeInBytes / BYTES_IN_MEGABYTE);
      } catch (Exception e) {
        memorySizeInMB = -1;
      }
  
      return memorySizeInMB;
    }
docs: Returns the total amount of physical memory on the current machine in megabytes.
  
  Some JVMs may not support the underlying API call.
  
  @return memory size in MB if the call succeeds; -1 otherwise