jdk/test/java/lang/management/MemoryMXBean/MemoryManagement.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5808 3a1f603c5ca7
child 22569 587cc5a1f9dc
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5808
diff changeset
     2
 * Copyright (c) 2003, 2010, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug     4530538
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Basic unit test of memory management testing:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          1) setUsatgeThreshold() and getUsageThreshold()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          2) test low memory detection on the old generation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @author  Mandy Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @build MemoryManagement MemoryUtil
5808
3a1f603c5ca7 6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents: 5506
diff changeset
    34
 * @run main/othervm/timeout=600 MemoryManagement
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.openmbean.CompositeData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class MemoryManagement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static List pools = ManagementFactory.getMemoryPoolMXBeans();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static List managers = ManagementFactory.getMemoryManagerMXBeans();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static MemoryPoolMXBean mpool = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static boolean trace = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static boolean testFailed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int NUM_CHUNKS = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static long chunkSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static int listenerInvoked = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static class SensorListener implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        public void handleNotification(Notification notif, Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            String type = notif.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                type.equals(MemoryNotificationInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                    MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                MemoryNotificationInfo minfo = MemoryNotificationInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                    from((CompositeData) notif.getUserData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                MemoryUtil.printMemoryNotificationInfo(minfo, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                listenerInvoked++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static long newThreshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (args.length > 0 && args[0].equals("trace")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            trace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            MemoryUtil.printMemoryPools(pools);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            MemoryUtil.printMemoryManagers(managers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // Find the Old generation which supports low memory detection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        ListIterator iter = pools.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if (p.getType() == MemoryType.HEAP &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                p.isUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                mpool = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    System.out.println("Selected memory pool for low memory " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        "detection.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    MemoryUtil.printMemoryPool(mpool);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        SensorListener listener = new SensorListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        NotificationEmitter emitter = (NotificationEmitter) mm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        emitter.addNotificationListener(listener, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        Thread allocator = new AllocatorThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        // Now set threshold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        MemoryUsage mu = mpool.getUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        chunkSize = (mu.getMax() - mu.getUsed()) / 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        newThreshold = mu.getUsed() + (chunkSize * NUM_CHUNKS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        System.out.println("Setting threshold for " + mpool.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            " from " + mpool.getUsageThreshold() + " to " + newThreshold +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            ".  Current used = " + mu.getUsed());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        mpool.setUsageThreshold(newThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (mpool.getUsageThreshold() != newThreshold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw new RuntimeException("TEST FAILED: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                "Threshold for Memory pool " + mpool.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                "is " + mpool.getUsageThreshold() + " but expected to be" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                newThreshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // Start the AllocatorThread to continously allocate memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        System.out.println("Starting an AllocatorThread to allocate memory.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        allocator.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            allocator.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (listenerInvoked == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new RuntimeException("No listener is invoked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (testFailed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new RuntimeException("TEST FAILED.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        System.out.println("Test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    static class AllocatorThread extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        private List objectPool = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            int iterations = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            int numElements = (int) (chunkSize / 4); // minimal object size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            while (listenerInvoked == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                iterations++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    System.out.println("    Iteration " + iterations +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        ": before allocation " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        mpool.getUsage().getUsed());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                Object[] o = new Object[numElements];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                if (iterations <= NUM_CHUNKS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    // only hold a reference to the first NUM_CHUNKS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    // allocated objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    objectPool.add(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                if (trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    System.out.println("                " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                        "  after allocation " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        mpool.getUsage().getUsed());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    System.out.println("Unexpected exception.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            System.out.println("AllocatedThread finished memory allocation " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                " num_iteration = " + iterations);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}