Showing the changes to java/pkg/sun/management/MemoryImpl.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,175 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved. | ||
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| 4 | * | ||
| 5 | * This code is free software; you can redistribute it and/or modify it | ||
| 6 | * under the terms of the GNU General Public License version 2 only, as | ||
| 7 | * published by the Free Software Foundation. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle in the LICENSE file that accompanied this code. | ||
| 10 | * | ||
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 14 | * version 2 for more details (a copy is included in the LICENSE file that | ||
| 15 | * accompanied this code). | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License version | ||
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, | ||
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 20 | * | ||
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | |||
| 26 | package sun.management; | ||
| 27 | |||
| 28 | import java.lang.management.ManagementFactory; | ||
| 29 | import java.lang.management.MemoryMXBean; | ||
| 30 | import java.lang.management.MemoryUsage; | ||
| 31 | import java.lang.management.MemoryNotificationInfo; | ||
| 32 | import java.lang.management.MemoryManagerMXBean; | ||
| 33 | import java.lang.management.MemoryPoolMXBean; | ||
| 34 | import javax.management.ObjectName; | ||
| 35 | import javax.management.MBeanNotificationInfo; | ||
| 36 | import javax.management.Notification; | ||
| 37 | import javax.management.openmbean.CompositeData; | ||
| 38 | |||
| 39 | /** | ||
| 40 | * Implementation class for the memory subsystem. | ||
| 41 | * Standard and committed hotspot-specific metrics if any. | ||
| 42 | * | ||
| 43 | * ManagementFactory.getMemoryMXBean() returns an instance | ||
| 44 | * of this class. | ||
| 45 | */ | ||
| 46 | class MemoryImpl extends NotificationEmitterSupport | ||
| 47 | implements MemoryMXBean { | ||
| 48 | |||
| 49 | private final VMManagement jvm; | ||
| 50 | |||
| 51 | private static MemoryPoolMXBean[] pools = null; | ||
| 52 | private static MemoryManagerMXBean[] mgrs = null; | ||
| 53 | |||
| 54 | /** | ||
| 55 | * Constructor of MemoryImpl class | ||
| 56 | */ | ||
| 57 | MemoryImpl(VMManagement vm) { | ||
| 58 | this.jvm = vm; | ||
| 59 | } | ||
| 60 | |||
| 61 | public int getObjectPendingFinalizationCount() { | ||
| 62 | return sun.misc.VM.getFinalRefCount(); | ||
| 63 | } | ||
| 64 | |||
| 65 | public void gc() { | ||
| 66 | Runtime.getRuntime().gc(); | ||
| 67 | } | ||
| 68 | |||
| 69 | // Need to make a VM call to get coherent value | ||
| 70 | public MemoryUsage getHeapMemoryUsage() { | ||
| 71 | return getMemoryUsage0(true); | ||
| 72 | } | ||
| 73 | |||
| 74 | public MemoryUsage getNonHeapMemoryUsage() { | ||
| 75 | return getMemoryUsage0(false); | ||
| 76 | } | ||
| 77 | |||
| 78 | public boolean isVerbose() { | ||
| 79 | return jvm.getVerboseGC(); | ||
| 80 | } | ||
| 81 | |||
| 82 | public void setVerbose(boolean value) { | ||
| 83 | Util.checkControlAccess(); | ||
| 84 | |||
| 85 | setVerboseGC(value); | ||
| 86 | } | ||
| 87 | |||
| 88 | // The current Hotspot implementation does not support | ||
| 89 | // dynamically add or remove memory pools & managers. | ||
| 90 | static synchronized MemoryPoolMXBean[] getMemoryPools() { | ||
| 91 | if (pools == null) { | ||
| 92 | pools = getMemoryPools0(); | ||
| 93 | } | ||
| 94 | return pools; | ||
| 95 | } | ||
| 96 | static synchronized MemoryManagerMXBean[] getMemoryManagers() { | ||
| 97 | if (mgrs == null) { | ||
| 98 | mgrs = getMemoryManagers0(); | ||
| 99 | } | ||
| 100 | return mgrs; | ||
| 101 | } | ||
| 102 | private static native MemoryPoolMXBean[] getMemoryPools0(); | ||
| 103 | private static native MemoryManagerMXBean[] getMemoryManagers0(); | ||
| 104 | private native MemoryUsage getMemoryUsage0(boolean heap); | ||
| 105 | private native void setVerboseGC(boolean value); | ||
| 106 | |||
| 107 | private final static String notifName = | ||
| 108 | "javax.management.Notification"; | ||
| 109 | private final static String[] notifTypes = { | ||
| 110 | MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, | ||
| 111 | MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED | ||
| 112 | }; | ||
| 113 | private final static String[] notifMsgs = { | ||
| 114 | "Memory usage exceeds usage threshold", | ||
| 115 | "Memory usage exceeds collection usage threshold" | ||
| 116 | }; | ||
| 117 | |||
| 118 | private MBeanNotificationInfo[] notifInfo = null; | ||
| 119 | public MBeanNotificationInfo[] getNotificationInfo() { | ||
| 120 | synchronized (this) { | ||
| 121 | if (notifInfo == null) { | ||
| 122 | notifInfo = new MBeanNotificationInfo[1]; | ||
| 123 | notifInfo[0] = new MBeanNotificationInfo(notifTypes, | ||
| 124 | notifName, | ||
| 125 | "Memory Notification"); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | return notifInfo; | ||
| 129 | } | ||
| 130 | |||
| 131 | private static String getNotifMsg(String notifType) { | ||
| 132 | for (int i = 0; i < notifTypes.length; i++) { | ||
| 133 | if (notifType == notifTypes[i]) { | ||
| 134 | return notifMsgs[i]; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | return "Unknown message"; | ||
| 138 | } | ||
| 139 | |||
| 140 | private static long seqNumber = 0; | ||
| 141 | private static long getNextSeqNumber() { | ||
| 142 | return ++seqNumber; | ||
| 143 | } | ||
| 144 | |||
| 145 | static void createNotification(String notifType, | ||
| 146 | String poolName, | ||
| 147 | MemoryUsage usage, | ||
| 148 | long count) { | ||
| 149 | MemoryImpl mbean = (MemoryImpl) ManagementFactory.getMemoryMXBean(); | ||
| 150 | if (!mbean.hasListeners()) { | ||
| 151 | // if no listener is registered. | ||
| 152 | return; | ||
| 153 | } | ||
| 154 | long timestamp = System.currentTimeMillis(); | ||
| 155 | String msg = getNotifMsg(notifType); | ||
| 156 | Notification notif = new Notification(notifType, | ||
| 157 | mbean.getObjectName(), | ||
| 158 | getNextSeqNumber(), | ||
| 159 | timestamp, | ||
| 160 | msg); | ||
| 161 | MemoryNotificationInfo info = | ||
| 162 | new MemoryNotificationInfo(poolName, | ||
| 163 | usage, | ||
| 164 | count); | ||
| 165 | CompositeData cd = | ||
| 166 | MemoryNotifInfoCompositeData.toCompositeData(info); | ||
| 167 | notif.setUserData(cd); | ||
| 168 | mbean.sendNotification(notif); | ||
| 169 | } | ||
| 170 | |||
| 171 | public ObjectName getObjectName() { | ||
| 172 | return Util.newObjectName(ManagementFactory.MEMORY_MXBEAN_NAME); | ||
| 173 | } | ||
| 174 | |||
| 175 | } | ||