Merge branch 'develop' into features/JAL-4219_extended_fasta_rna_ss
[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 import jalview.util.ErrorLog;
27
28 /**
29  * Isolated class to ascertain physical memory of the system using
30  * com.sun.management.OperatingSystemMXBean class's getTotalPhysicalMemorySize
31  * method. This class is present in OpenJDK 8,9,10,11,12,13. It is present but
32  * marked as deprecated in the early-access(30) release of OpenJDK 14. In case
33  * of an alternative/unsupported JRE being used or the class/method not being
34  * implemented in an exotic architecture JRE this call has been isolated into
35  * this separate class.
36  * 
37  * @author bsoares
38  *
39  */
40 class GetMemory
41 {
42
43   /**
44    * Wrapper for
45    * com.sun.management.OperatingSystemMXBean.getTotalPhysicalMemorySize()
46    * 
47    * @return Result of
48    *         com.sun.management.OperatingSystemMXBean.getTotalPhysicalMemorySize()
49    *         or -1 if this class is not present in the JRE.
50    */
51   protected static long getPhysicalMemory()
52   {
53     final OperatingSystemMXBean o = ManagementFactory
54             .getOperatingSystemMXBean();
55
56     try
57     {
58       if (o instanceof com.sun.management.OperatingSystemMXBean)
59       {
60         final com.sun.management.OperatingSystemMXBean osb = (com.sun.management.OperatingSystemMXBean) o;
61         return osb.getTotalPhysicalMemorySize();
62       }
63     } catch (NoClassDefFoundError e)
64     {
65       // com.sun.management.OperatingSystemMXBean doesn't exist in this JVM
66       ErrorLog.errPrintln(
67               "No com.sun.management.OperatingSystemMXBean: cannot get total physical memory size");
68     }
69
70     // We didn't get a com.sun.management.OperatingSystemMXBean.
71     return -1;
72   }
73
74 }