001 /*
002 * $Id: GetMem.java,v 1.6 2012/02/19 17:35:52 davep Exp $
003 *
004 * This file is part of McIDAS-V
005 *
006 * Copyright 2007-2012
007 * Space Science and Engineering Center (SSEC)
008 * University of Wisconsin - Madison
009 * 1225 W. Dayton Street, Madison, WI 53706, USA
010 * https://www.ssec.wisc.edu/mcidas
011 *
012 * All Rights Reserved
013 *
014 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
015 * some McIDAS-V source code is based on IDV and VisAD source code.
016 *
017 * McIDAS-V is free software; you can redistribute it and/or modify
018 * it under the terms of the GNU Lesser Public License as published by
019 * the Free Software Foundation; either version 3 of the License, or
020 * (at your option) any later version.
021 *
022 * McIDAS-V is distributed in the hope that it will be useful,
023 * but WITHOUT ANY WARRANTY; without even the implied warranty of
024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
025 * GNU Lesser Public License for more details.
026 *
027 * You should have received a copy of the GNU Lesser Public License
028 * along with this program. If not, see http://www.gnu.org/licenses.
029 */
030
031 package edu.wisc.ssec.mcidasv.util;
032
033 import java.lang.management.ManagementFactory;
034 import java.lang.management.OperatingSystemMXBean;
035 import java.lang.reflect.Method;
036
037 // Wrapper for OperatingSystemMXBean
038 public class GetMem {
039
040 private <T> T callMXBeanMethod(final String methodName, final T defaultValue) {
041 assert methodName != null : "Cannot invoke a null method name";
042 assert methodName.length() > 0: "Cannot invoke an empty method name";
043 OperatingSystemMXBean osBean =
044 ManagementFactory.getOperatingSystemMXBean();
045 T result = defaultValue;
046 try {
047 Method m = osBean.getClass().getMethod(methodName);
048 m.setAccessible(true);
049 // don't suppress warnings because we cannot guarantee that this
050 // cast is correct.
051 result = (T)m.invoke(osBean);
052 } catch (Exception e) {
053 System.err.println("Error invoking OperatingSystemMXBean method: " + methodName);
054 // do nothing for right now
055 }
056 return result;
057 }
058
059 /**
060 * The main. Get total system memory and print it out.
061 * Account for 32bit JRE limitation of 1.5GB.
062 * Print value in megabytes.
063 */
064 public static void main(String[] args) throws Exception {
065 GetMem nonStaticInstance = new GetMem();
066
067 try {
068 Object totalMemoryObject = nonStaticInstance.callMXBeanMethod("getTotalPhysicalMemorySize", 0);
069 long totalMemory = ((Number)totalMemoryObject).longValue();
070 boolean is64 = (System.getProperty("os.arch").indexOf("64") >= 0);
071 int megabytes = (int)(Math.round(totalMemory/1024/1024));
072 if (!is64 && megabytes > 1536) megabytes=1536;
073 String memoryString = String.valueOf(megabytes);
074 System.out.println(memoryString);
075 }
076 catch (Exception e) {
077 System.err.println("Error getting total physical memory size");
078 System.out.println("0");
079 }
080 }
081 }