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