KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > runtime > TCMemoryManagerJdk14


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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