1 /* MemoryMXBeanImpl.java - Implementation of a memory bean
2 Copyright (C) 2006 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.java.lang.management;
40 import gnu.classpath.ListenerData;
42 import java.lang.management.MemoryMXBean;
43 import java.lang.management.MemoryNotificationInfo;
44 import java.lang.management.MemoryUsage;
46 import java.util.ArrayList;
47 import java.util.Iterator;
48 import java.util.List;
50 import javax.management.ListenerNotFoundException;
51 import javax.management.MBeanNotificationInfo;
52 import javax.management.NotCompliantMBeanException;
53 import javax.management.Notification;
54 import javax.management.NotificationEmitter;
55 import javax.management.NotificationFilter;
56 import javax.management.NotificationListener;
58 import javax.management.openmbean.CompositeData;
59 import javax.management.openmbean.CompositeDataSupport;
60 import javax.management.openmbean.CompositeType;
61 import javax.management.openmbean.OpenDataException;
62 import javax.management.openmbean.OpenType;
63 import javax.management.openmbean.SimpleType;
66 * Provides access to information about the memory
67 * management of the current invocation of the virtual
68 * machine. Instances of this bean are obtained by calling
69 * {@link ManagementFactory#getMemoryMXBean()}.
71 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
74 public final class MemoryMXBeanImpl
76 implements MemoryMXBean, NotificationEmitter
79 private List listeners;
81 private long notificationCount;
83 public static CompositeType notifType;
85 public static CompositeType usageType;
91 CompositeType usageType =
92 new CompositeType(MemoryUsage.class.getName(),
93 "Describes the usage levels of a pool",
94 new String[] { "init", "used",
97 new String[] { "Initial level",
103 SimpleType.LONG, SimpleType.LONG,
104 SimpleType.LONG, SimpleType.LONG
106 CompositeType notifType =
107 new CompositeType(MemoryNotificationInfo.class.getName(),
108 "Provides the notification info on memory usage",
109 new String[] { "poolName", "usage", "count" },
110 new String[] { "Name of the memory pool",
111 "Usage level of the memory pool",
112 "Number of times the threshold " +
116 SimpleType.STRING, usageType, SimpleType.LONG
119 catch (OpenDataException e)
121 throw new IllegalStateException("Something went wrong in creating " +
122 "the composite data types.", e);
127 * Constructs a new <code>MemoryMXBeanImpl</code>.
129 * @throws NotCompliantMBeanException if this class doesn't implement
130 * the interface or a method appears
131 * in the interface that doesn't comply
132 * with the naming conventions.
134 public MemoryMXBeanImpl()
135 throws NotCompliantMBeanException
137 super(MemoryMXBean.class);
138 listeners = new ArrayList();
139 notificationCount = 0;
147 public MemoryUsage getHeapMemoryUsage()
149 return VMMemoryMXBeanImpl.getHeapMemoryUsage();
152 public MemoryUsage getNonHeapMemoryUsage()
154 return VMMemoryMXBeanImpl.getNonHeapMemoryUsage();
157 public int getObjectPendingFinalizationCount()
159 return VMMemoryMXBeanImpl.getObjectPendingFinalizationCount();
162 public boolean isVerbose()
164 return VMMemoryMXBeanImpl.isVerbose();
167 public void setVerbose(boolean verbose)
169 checkControlPermissions();
170 VMMemoryMXBeanImpl.setVerbose(verbose);
173 public void addNotificationListener(NotificationListener listener,
174 NotificationFilter filter,
177 if (listener == null)
178 throw new IllegalArgumentException("Null listener added to bean.");
179 listeners.add(new ListenerData(listener, filter, passback));
182 public MBeanNotificationInfo[] getNotificationInfo()
184 return new MBeanNotificationInfo[]
186 new MBeanNotificationInfo(new String[]
188 MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED,
189 MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED
191 Notification.class.getName(),
192 "Memory Usage Notifications")
196 public void removeNotificationListener(NotificationListener listener)
197 throws ListenerNotFoundException
199 Iterator it = listeners.iterator();
200 boolean foundOne = false;
203 ListenerData data = (ListenerData) it.next();
204 if (data.getListener() == listener)
211 throw new ListenerNotFoundException("The specified listener, " + listener +
212 "is not registered with this bean.");
215 public void removeNotificationListener(NotificationListener listener,
216 NotificationFilter filter,
218 throws ListenerNotFoundException
220 if (!(listeners.remove(new ListenerData(listener, filter, passback))))
222 throw new ListenerNotFoundException("The specified listener, " + listener +
223 " with filter " + filter +
224 "and passback " + passback +
225 ", is not registered with this bean.");
229 void fireNotification(String type, String poolName, long init, long used,
230 long committed, long max, long count)
232 Notification notif = new Notification(type, this, notificationCount);
233 MemoryUsage usage = new MemoryUsage(init, used, committed, max);
237 data = new CompositeDataSupport(notifType,
239 "poolName", "usage", "count"
242 poolName, usage, Long.valueOf(count)
245 catch (OpenDataException e)
247 throw new IllegalStateException("Something went wrong in creating " +
248 "the composite data instance.", e);
250 notif.setUserData(data);
251 Iterator it = listeners.iterator();
254 ListenerData ldata = (ListenerData) it.next();
255 NotificationFilter filter = ldata.getFilter();
256 if (filter == null || filter.isNotificationEnabled(notif))
257 ldata.getListener().handleNotification(notif, ldata.getPassback());
262 void fireThresholdExceededNotification(String poolName, long init,
263 long used, long committed,
264 long max, long count)
266 fireNotification(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
267 poolName, init, used, committed, max, count);
270 void fireCollectionThresholdExceededNotification(String poolName,
277 fireNotification(MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED,
278 poolName, init, used, committed, max, count);