JAL-3609 Added a -DforceHiDPISetting=true option mainly for testing, and updated...
[jalview.git] / getdown / src / getdown / core / src / main / java / jalview / bin / GetMemory.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.bin;
22
23 import java.lang.management.ManagementFactory;
24 import java.lang.management.OperatingSystemMXBean;
25
26 /**
27  * Isolated class to ascertain physical memory of the system using
28  * com.sun.management.OperatingSystemMXBean class's getTotalPhysicalMemorySize
29  * method. This class is present in OpenJDK 8,9,10,11,12,13. It is present but
30  * marked as deprecated in the early-access(30) release of OpenJDK 14. In case
31  * of an alternative/unsupported JRE being used or the class/method not being
32  * implemented in an exotic architecture JRE this call has been isolated into
33  * this separate class.
34  * 
35  * @author bsoares
36  *
37  */
38 class GetMemory
39 {
40
41   /**
42    * Wrapper for
43    * com.sun.management.OperatingSystemMXBean.getTotalPhysicalMemorySize()
44    * 
45    * @return Result of
46    *         com.sun.management.OperatingSystemMXBean.getTotalPhysicalMemorySize()
47    *         or -1 if this class is not present in the JRE.
48    */
49   protected static long getPhysicalMemory()
50   {
51     final OperatingSystemMXBean o = ManagementFactory
52             .getOperatingSystemMXBean();
53
54     try
55     {
56       if (o instanceof com.sun.management.OperatingSystemMXBean)
57       {
58         final com.sun.management.OperatingSystemMXBean osb = (com.sun.management.OperatingSystemMXBean) o;
59         return osb.getTotalPhysicalMemorySize();
60       }
61     } catch (NoClassDefFoundError e)
62     {
63       // com.sun.management.OperatingSystemMXBean doesn't exist in this JVM
64       System.err.println(
65               "No com.sun.management.OperatingSystemMXBean: cannot get total physical memory size");
66     }
67
68     // We didn't get a com.sun.management.OperatingSystemMXBean.
69     return -1;
70   }
71
72 }