01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.runtime;
05:
06: import com.tc.logging.TCLogger;
07: import com.tc.logging.TCLogging;
08:
09: class TCMemoryManagerJdk14 implements JVMMemoryManager {
10:
11: private static final TCLogger logger = TCLogging
12: .getLogger(TCMemoryManagerJdk14.class);
13:
14: private final Runtime rt;
15:
16: public TCMemoryManagerJdk14() {
17: rt = Runtime.getRuntime();
18: if (rt.maxMemory() == Long.MAX_VALUE) {
19: logger
20: .warn("Please specify Max memory using -Xmx flag for Memory manager to work properly");
21: }
22: }
23:
24: public MemoryUsage getMemoryUsage() {
25: return new Jdk14MemoryUsage(rt);
26: }
27:
28: public MemoryUsage getOldGenUsage() {
29: throw new UnsupportedOperationException();
30: }
31:
32: public boolean isMemoryPoolMonitoringSupported() {
33: return false;
34: }
35:
36: private static final class Jdk14MemoryUsage implements MemoryUsage {
37:
38: private final long max;
39: private final long used;
40: private final long free;
41: private final long total;
42: private final int usedPercentage;
43:
44: public Jdk14MemoryUsage(Runtime rt) {
45: this .max = rt.maxMemory();
46: this .free = rt.freeMemory();
47: this .total = rt.totalMemory();
48: this .used = this .total - this .free;
49: if (this .max == Long.MAX_VALUE) {
50: this .usedPercentage = (int) (this .used * 100 / this .total);
51: } else {
52: this .usedPercentage = (int) (this .used * 100 / this .max);
53: }
54: }
55:
56: public String getDescription() {
57: return "VM 1.4 Memory Usage";
58: }
59:
60: public long getFreeMemory() {
61: return free;
62: }
63:
64: public long getMaxMemory() {
65: return max;
66: }
67:
68: public long getUsedMemory() {
69: return used;
70: }
71:
72: public int getUsedPercentage() {
73: return usedPercentage;
74: }
75:
76: public String toString() {
77: return "Jdk14MemoryUsage ( max = " + max + ", used = "
78: + used + ", free = " + free + ", total = " + total
79: + ", used % = " + usedPercentage + ")";
80: }
81:
82: // This is not supported in 1.4
83: public long getCollectionCount() {
84: return -1;
85: }
86:
87: }
88: }
|