Created
October 8, 2016 17:06
-
-
Save hugo4715/6393140256058ae9659d79a8b7d6eaea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.lang.management.ManagementFactory; | |
| import java.lang.management.OperatingSystemMXBean; | |
| import java.lang.reflect.InvocationTargetException; | |
| import java.lang.reflect.Method; | |
| public class SystemInfo { | |
| private static OperatingSystemMXBean sys = ManagementFactory.getOperatingSystemMXBean(); | |
| private static Method getFreePhysicalMemorySize; | |
| private static Method getTotalPhysicalMemorySize; | |
| static{ | |
| try { | |
| getFreePhysicalMemorySize = sys.getClass().getDeclaredMethod("getFreePhysicalMemorySize"); | |
| getFreePhysicalMemorySize.setAccessible(true); | |
| getTotalPhysicalMemorySize = sys.getClass().getDeclaredMethod("getTotalPhysicalMemorySize"); | |
| getTotalPhysicalMemorySize.setAccessible(true); | |
| //System.out.println("Used memory: " + ((long)getTotalPhysicalMemorySize.invoke(sys) - (long)getFreePhysicalMemorySize.invoke(sys))); | |
| } catch (NoSuchMethodException | SecurityException | IllegalArgumentException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| /** | |
| * Get the system free memory, in bytes | |
| * @return | |
| * @throws IllegalAccessException | |
| * @throws IllegalArgumentException | |
| * @throws InvocationTargetException | |
| */ | |
| public static long getFreePhysicalMemorySize() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{ | |
| return (long)getFreePhysicalMemorySize.invoke(sys); | |
| } | |
| /** | |
| * Get the system total memory, in bytes | |
| * @return | |
| * @throws IllegalAccessException | |
| * @throws IllegalArgumentException | |
| * @throws InvocationTargetException | |
| */ | |
| public static long getTotalPhysicalMemorySize() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{ | |
| return (long)getTotalPhysicalMemorySize.invoke(sys); | |
| } | |
| /** | |
| * Get the system used memory, in bytes | |
| * @return | |
| * @throws IllegalAccessException | |
| * @throws IllegalArgumentException | |
| * @throws InvocationTargetException | |
| */ | |
| public static long getUsedPhysicalMemorySize() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{ | |
| return (long)getTotalPhysicalMemorySize.invoke(sys)-(long)getFreePhysicalMemorySize.invoke(sys); | |
| } | |
| /** | |
| * Get the system used memory, in bytes | |
| * @return | |
| * @throws IllegalAccessException | |
| * @throws IllegalArgumentException | |
| * @throws InvocationTargetException | |
| */ | |
| public static long getFreePhysicalMemorySizePercent() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{ | |
| return (getFreePhysicalMemorySize()*100)/getTotalPhysicalMemorySize(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment