1 7 8 package java.lang.management; 9 10 import javax.management.openmbean.CompositeData ; 11 import sun.management.MemoryUsageCompositeData; 12 13 91 public class MemoryUsage { 92 private final long init; 93 private final long used; 94 private final long committed; 95 private final long max; 96 97 120 public MemoryUsage(long init, 121 long used, 122 long committed, 123 long max) { 124 if (init < -1) { 125 throw new IllegalArgumentException ( "init parameter = " + 126 init + " is negative but not -1."); 127 } 128 if (max < -1) { 129 throw new IllegalArgumentException ( "max parameter = " + 130 max + " is negative but not -1."); 131 } 132 if (used < 0) { 133 throw new IllegalArgumentException ( "used parameter = " + 134 used + " is negative."); 135 } 136 if (committed < 0) { 137 throw new IllegalArgumentException ( "committed parameter = " + 138 committed + " is negative."); 139 } 140 if (used > committed) { 141 throw new IllegalArgumentException ( "used = " + used + 142 " should be <= committed = " + committed); 143 } 144 if (max >= 0 && committed > max) { 145 throw new IllegalArgumentException ( "committed = " + committed + 146 " should be < max = " + max); 147 } 148 149 this.init = init; 150 this.used = used; 151 this.committed = committed; 152 this.max = max; 153 } 154 155 159 private MemoryUsage(CompositeData cd) { 160 MemoryUsageCompositeData.validateCompositeData(cd); 162 163 this.init = MemoryUsageCompositeData.getInit(cd); 164 this.used = MemoryUsageCompositeData.getUsed(cd); 165 this.committed = MemoryUsageCompositeData.getCommitted(cd); 166 this.max = MemoryUsageCompositeData.getMax(cd); 167 } 168 169 177 public long getInit() { 178 return init; 179 } 180 181 187 public long getUsed() { 188 return used; 189 }; 190 191 199 public long getCommitted() { 200 return committed; 201 }; 202 203 217 public long getMax() { 218 return max; 219 }; 220 221 224 public String toString() { 225 StringBuffer buf = new StringBuffer (); 226 buf.append("init = " + init + "(" + (init >> 10) + "K) "); 227 buf.append("used = " + used + "(" + (used >> 10) + "K) "); 228 buf.append("committed = " + committed + "(" + 229 (committed >> 10) + "K) " ); 230 buf.append("max = " + max + "(" + (max >> 10) + "K)"); 231 return buf.toString(); 232 } 233 234 274 public static MemoryUsage from(CompositeData cd) { 275 if (cd == null) { 276 return null; 277 } 278 279 if (cd instanceof MemoryUsageCompositeData) { 280 return ((MemoryUsageCompositeData) cd).getMemoryUsage(); 281 } else { 282 return new MemoryUsage (cd); 283 } 284 285 } 286 } 287 | Popular Tags |