e89bffbc53d7035a31ed0af4ce90e667e8b92e3a
[jalview.git] / getdown / src / getdown / core / src / main / java / jalview / bin / GetMemory.java
1 package jalview.bin;
2
3 import java.lang.management.ManagementFactory;
4 import java.lang.management.OperatingSystemMXBean;
5
6 /**
7  * Isolated class to ascertain physical memory of the system using
8  * com.sun.management.OperatingSystemMXBean class's getTotalPhysicalMemorySize
9  * method. This class is present in OpenJDK 8,9,10,11,12,13. It is present but
10  * marked as deprecated in the early-access(30) release of OpenJDK 14. In case
11  * of an alternative/unsupported JRE being used or the class/method not being
12  * implemented in an exotic architecture JRE this call has been isolated into
13  * this separate class.
14  * 
15  * @author bsoares
16  *
17  */
18 class GetMemory
19 {
20
21   /**
22    * Wrapper for
23    * com.sun.management.OperatingSystemMXBean.getTotalPhysicalMemorySize()
24    * 
25    * @return Result of
26    *         com.sun.management.OperatingSystemMXBean.getTotalPhysicalMemorySize()
27    *         or -1 if this class is not present in the JRE.
28    */
29   protected static long getPhysicalMemory()
30   {
31     final OperatingSystemMXBean o = ManagementFactory
32             .getOperatingSystemMXBean();
33
34     try
35     {
36       if (o instanceof com.sun.management.OperatingSystemMXBean)
37       {
38         final com.sun.management.OperatingSystemMXBean osb = (com.sun.management.OperatingSystemMXBean) o;
39         return osb.getTotalPhysicalMemorySize();
40       }
41     } catch (NoClassDefFoundError e)
42     {
43       // com.sun.management.OperatingSystemMXBean doesn't exist in this JVM
44       System.err.println(
45               "No com.sun.management.OperatingSystemMXBean: cannot get total physical memory size");
46     }
47
48     // We didn't get a com.sun.management.OperatingSystemMXBean.
49     return -1;
50   }
51
52 }