KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > util > NotificationMemoryMonitor


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/util/NotificationMemoryMonitor.java#5 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 2007-2007 Julian Hyde and others
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.util;
11
12 import java.lang.management.MemoryMXBean JavaDoc;
13 import java.lang.management.MemoryNotificationInfo JavaDoc;
14 import java.lang.management.MemoryUsage JavaDoc;
15 import java.lang.management.ManagementFactory JavaDoc;
16 import java.lang.management.MemoryPoolMXBean JavaDoc;
17 import java.lang.management.MemoryType JavaDoc;
18 import javax.management.Notification JavaDoc;
19 import javax.management.NotificationEmitter JavaDoc;
20 import javax.management.NotificationListener JavaDoc;
21 import org.apache.log4j.Logger;
22
23 /**
24  * The <code>NotificationMemoryMonitor</code> class uses the Java5
25  * memory management system to detect system low memory events.
26  * <p>
27  * This code is loosely based on the code taken from The Java
28  * Specialists' Newsletter,
29  * <a HREF="http://www.javaspecialists.co.za/archive/newsletter.do?issue=092"
30  * >issue 92</a> authored by Dr. Heinz M. Kabutz.
31  * As a note, his on-line newletters are a good source of Java information,
32  * take a look.
33  * <p>
34  * For more information one should review the Java5 classes in
35  * java.lang.management.
36  *
37  *
38  * @author <a>Richard M. Emberson</a>
39  * @since Feb 03 2007
40  * @version $Id: //open/mondrian/src/main/mondrian/util/NotificationMemoryMonitor.java#5 $
41  */

42 public class NotificationMemoryMonitor extends AbstractMemoryMonitor {
43     private static final Logger LOGGER =
44                 Logger.getLogger(NotificationMemoryMonitor.class);
45
46
47     protected static final MemoryPoolMXBean JavaDoc TENURED_POOL;
48
49     static {
50         TENURED_POOL = findTenuredGenPool();
51     }
52
53     private static MemoryPoolMXBean JavaDoc findTenuredGenPool() {
54         for (MemoryPoolMXBean JavaDoc pool : ManagementFactory.getMemoryPoolMXBeans()) {
55             // I don't know whether this approach is better, or whether
56
// we should rather check for the pool name "Tenured Gen"?
57
if (pool.getType() == MemoryType.HEAP &&
58                 pool.isUsageThresholdSupported()) {
59                 return pool;
60             }
61         }
62         throw new AssertionError JavaDoc("Could not find tenured space");
63     }
64
65     /**
66      * The <code>NotificationHandler</code> implements the Java memory
67      * notification listener, <code>NotificationListener</code>,
68      * and is used to take notifications from Java and pass them on
69      * to the <code>NotificationMemoryMonitor</code>'s
70      * <code>Listerner</code>s.
71      */

72     private class NotificationHandler implements NotificationListener JavaDoc {
73
74         /**
75          * This method is called by the Java5 code to notify clients
76          * registered with the JVM that the JVM memory threshold
77          * has been exceeded.
78          *
79          * @param notification
80          * @param unused
81          */

82         public void handleNotification(final Notification JavaDoc notification,
83                                        final Object JavaDoc unused) {
84             final String JavaDoc type = notification.getType();
85
86             if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED)) {
87                 final MemoryUsage JavaDoc usage = TENURED_POOL.getUsage();
88
89                 notifyListeners(usage.getUsed(), usage.getMax());
90             }
91         }
92     }
93
94
95     /**
96      * Construct a <code>NotificationMemoryMonitor</code> instance and
97      * register it with the Java5 memory management system.
98      */

99     NotificationMemoryMonitor() {
100         super();
101         final MemoryMXBean JavaDoc mbean = ManagementFactory.getMemoryMXBean();
102         final NotificationEmitter JavaDoc emitter = (NotificationEmitter JavaDoc) mbean;
103
104         // register with the Java5 memory management system.
105
emitter.addNotificationListener(new NotificationHandler(), null, null);
106     }
107
108     /**
109      * Get the <code>Logger</code>.
110      *
111      * @return the <code>Logger</code>.
112      */

113     protected Logger getLogger() {
114         return LOGGER;
115     }
116
117     /**
118      * Notify the Java5 memory management system that there is a new
119      * low threshold.
120      *
121      * @param newLowThreshold the new threshold.
122      */

123     protected void notifyNewLowThreshold(final long newLowThreshold) {
124
125         if (newLowThreshold == Long.MAX_VALUE) {
126             TENURED_POOL.setUsageThreshold(0);
127         } else {
128             TENURED_POOL.setUsageThreshold(newLowThreshold);
129         }
130     }
131
132     /**
133      * Get the maximum possible memory usage for this JVM instance.
134      *
135      * @return maximum memory that can be used.
136      */

137     public long getMaxMemory() {
138         return TENURED_POOL.getUsage().getMax();
139     }
140     /**
141      * Get the current memory usage for this JVM instance.
142      *
143      * @return current memory used.
144      */

145     public long getUsedMemory() {
146         return TENURED_POOL.getUsage().getUsed();
147     }
148 }
149
150 // End NotificationMemoryMonitor.java
151
Popular Tags