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