1 3 package jodd.util; 4 5 8 public class SystemUtil { 9 13 public static double systemTimerResolution() { 14 long t1, t2; 15 int sumres = 0; 16 System.gc(); 18 int loops = 20; 19 for (int i = 0; i < loops; ++i) { 20 t1 = System.currentTimeMillis(); 21 while (true) { 22 t2 = System.currentTimeMillis(); 23 if (t2 != t1) { 24 sumres += (int) (t2 - t1); 25 break; 26 } 27 } 28 } 29 return (sumres / (double) loops); 30 } 31 32 34 public static final String SYS_USER_DIR = "user.dir"; 35 public static final String SYS_JAVA_HOME = "java.home"; 36 public static final String SYS_TEMP_DIR = "java.io.tmpdir"; 37 public static final String SYS_OS_NAME = "os.name"; 38 public static final String SYS_OS_VERSION = "os.version"; 39 public static final String SYS_JAVA_VERSION = "java.version"; 40 public static final String SYS_JAVA_VENDOR = "java.vendor"; 41 public static final String SYS_JAVA_CLASSPATH = "java.class.path"; 42 public static final String SYS_PATH_SEPARATOR = "path.separator"; 43 44 47 public static String getUserDir() { 48 return System.getProperty(SYS_USER_DIR); 49 } 50 51 public static String getJavaJreHome() { 52 return System.getProperty(SYS_JAVA_HOME); 53 } 54 55 59 public static String getJavaHome() { 60 String home = System.getProperty(SYS_JAVA_HOME); 61 if (home == null) { 62 return null; 63 } 64 int i = home.lastIndexOf('\\'); 65 int j = home.lastIndexOf('/'); 66 if (j > i) { 67 i = j; 68 } 69 return home.substring(0, i); 70 } 71 72 public static String getTempDir() { 73 return System.getProperty(SYS_TEMP_DIR); 74 } 75 76 public static String getOsName() { 77 return System.getProperty(SYS_OS_NAME); 78 } 79 80 public static String getOsVersion() { 81 return System.getProperty(SYS_OS_VERSION); 82 } 83 84 public static String getJavaVersion() { 85 return System.getProperty(SYS_JAVA_VERSION); 86 } 87 88 public static String getJavaVendor() { 89 return System.getProperty(SYS_JAVA_VENDOR); 90 } 91 92 public static String getClassPath() { 93 return System.getProperty(SYS_JAVA_CLASSPATH); 94 } 95 96 public static String getPathSeparator() { 97 return System.getProperty(SYS_PATH_SEPARATOR); 98 } 99 100 102 105 public static long totalMemory() { 106 return Runtime.getRuntime().totalMemory(); 107 } 108 109 112 public static long freeMemory() { 113 return Runtime.getRuntime().freeMemory(); 114 } 115 116 119 public static double freeMemoryPercents() { 120 Runtime runtime = Runtime.getRuntime(); 121 return ((double) runtime.freeMemory() / runtime.totalMemory()) * 100.0; 122 } 123 124 127 public static long usedMemory() { 128 return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); 129 } 130 131 134 public static double usedMemoryPercents() { 135 Runtime runtime = Runtime.getRuntime(); 136 long totalMemory = runtime.totalMemory(); 137 return ((double) (totalMemory - runtime.freeMemory()) / totalMemory) * 100.0; 138 } 139 140 141 } 142 | Popular Tags |