jdk/test/java/lang/management/MemoryMXBean/LowMemoryTest2.java
author mtobiass
Tue, 04 Mar 2014 09:35:54 +0100
changeset 23053 788f0ea9a703
parent 23010 6dadb192ad81
child 23721 83da5b498504
permissions -rw-r--r--
8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace Summary: Load classes until getUsageThresholdCount() > 0 instead of isUsageThresholdExceeded(). Reviewed-by: dfuchs, sjiang, sla
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 20784
diff changeset
     2
 * Copyright (c) 2004, 2013, 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 low memory detection of non-heap memory pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * The test set a listener to be notified when any of the non-heap pools
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * exceed 80%. It then starts a thread that continuously loads classes.
20784
c0864e216d36 7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents: 5506
diff changeset
    29
 * In the HotSpot implementation this causes metaspace to be consumed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Test completes when we the notification is received or an OutOfMemory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * is generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.openmbean.CompositeData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class LowMemoryTest2 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static volatile boolean listenerInvoked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static String INDENT = "    ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static class SensorListener implements NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        public void handleNotification(Notification notif, Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            String type = notif.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                type.equals(MemoryNotificationInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                    MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                listenerInvoked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                MemoryNotificationInfo minfo = MemoryNotificationInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                    from((CompositeData) notif.getUserData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                System.out.print("Notification for " + minfo.getPoolName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                System.out.print(" [type = " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                System.out.println(" count = " + minfo.getCount() + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                System.out.println(INDENT + "usage = " + minfo.getUsage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // Loads classes Test100001, Test100002, .... until OutOfMemoryErrror or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // low memory notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static class BoundlessLoaderThread extends ClassLoader implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        static int count = 100000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
23053
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
    70
        Class loadNext() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            // public class TestNNNNNN extends java.lang.Object{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            // public TestNNNNNN();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            //   Code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            //    0:    aload_0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            //    1:    invokespecial   #1; //Method java/lang/Object."<init>":()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            //    4:    return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            // }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            int begin[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                0xca, 0xfe, 0xba, 0xbe, 0x00, 0x00, 0x00, 0x30,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                0x00, 0x0a, 0x0a, 0x00, 0x03, 0x00, 0x07, 0x07,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                0x00, 0x08, 0x07, 0x00, 0x09, 0x01, 0x00, 0x06,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                0x3c, 0x69, 0x6e, 0x69, 0x74, 0x3e, 0x01, 0x00,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                0x03, 0x28, 0x29, 0x56, 0x01, 0x00, 0x04, 0x43,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                0x6f, 0x64, 0x65, 0x0c, 0x00, 0x04, 0x00, 0x05,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                0x01, 0x00, 0x0a, 0x54, 0x65, 0x73, 0x74 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            int end [] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                0x01, 0x00, 0x10,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                0x6a, 0x61, 0x76, 0x61, 0x2f, 0x6c, 0x61, 0x6e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                0x67, 0x2f, 0x4f, 0x62, 0x6a, 0x65, 0x63, 0x74,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                0x00, 0x21, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x04,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                0x00, 0x05, 0x00, 0x01, 0x00, 0x06, 0x00, 0x00,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                0x00, 0x11, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                0x00, 0x05, 0x2a, 0xb7, 0x00, 0x01, 0xb1, 0x00,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                0x00, 0x00, 0x00, 0x00, 0x00 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            // TestNNNNNN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
20784
c0864e216d36 7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents: 5506
diff changeset
   103
            int load_count = count++;
c0864e216d36 7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents: 5506
diff changeset
   104
            if (load_count > 999999) {
c0864e216d36 7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents: 5506
diff changeset
   105
                // The test will create a corrupt class file if the count
c0864e216d36 7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents: 5506
diff changeset
   106
                // exceeds 999999. Fix the test if this exception is thrown.
c0864e216d36 7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents: 5506
diff changeset
   107
                throw new RuntimeException("Load count exceeded");
c0864e216d36 7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents: 5506
diff changeset
   108
            }
c0864e216d36 7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents: 5506
diff changeset
   109
c0864e216d36 7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents: 5506
diff changeset
   110
            String name = "Test" + Integer.toString(load_count);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            byte value[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                value = name.substring(4).getBytes("UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            } catch (java.io.UnsupportedEncodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            // construct class file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            int len = begin.length + value.length + end.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            byte b[] = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            int i, pos=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            for (i=0; i<begin.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                b[pos++] = (byte)begin[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            for (i=0; i<value.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                b[pos++] = value[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            for (i=0; i<end.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                b[pos++] = (byte)end[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return defineClass(name, b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        /*
23053
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   138
         * Load classes until MemoryPoolMXBean.getUsageThresholdCount() > 0.
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   139
         * Then wait for the memory threshold notification to be received.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        public void run() {
23053
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   142
            List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            boolean thresholdExceeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
23053
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   145
            // Load classes until MemoryPoolMXBean.getUsageThresholdCount() > 0
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   146
            while (!thresholdExceeded) {
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   147
                // the classes are small so we load 10 at a time
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   148
                for (int i=0; i<10; i++) {
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   149
                    loadNext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
23053
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   152
                // check if the threshold has been exceeded
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   153
                for (MemoryPoolMXBean p : pools) {
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   154
                    if (p.getType() == MemoryType.NON_HEAP &&
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   155
                        p.isUsageThresholdSupported() &&
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   156
                        p.getUsageThresholdCount() > 0)
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   157
                    {
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   158
                        thresholdExceeded = true;
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   159
                        break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
23053
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   163
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   164
            System.out.println("thresholdExceeded. Waiting for notification");
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   165
            while (!listenerInvoked) {
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   166
                try {
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   167
                    Thread.currentThread().sleep(10);
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   168
                } catch (InterruptedException x) {}
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   169
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public static void main(String args[]) {
23053
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   174
        List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // Set threshold of 80% of all NON_HEAP memory pools
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // In the Hotspot implementation this means we should get a notification
20784
c0864e216d36 7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents: 5506
diff changeset
   178
        // if the CodeCache or metaspace fills up.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
23053
788f0ea9a703 8031065: java/lang/management/MemoryMXBean/LowMemoryTest2.sh fails: OutOfMemoryError: Metaspace
mtobiass
parents: 23010
diff changeset
   180
        for (MemoryPoolMXBean p : pools) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (p.getType() == MemoryType.NON_HEAP && p.isUsageThresholdSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                // set threshold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                MemoryUsage mu = p.getUsage();
20784
c0864e216d36 7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents: 5506
diff changeset
   185
                long max = mu.getMax();
c0864e216d36 7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents: 5506
diff changeset
   186
                if (max < 0) {
c0864e216d36 7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents: 5506
diff changeset
   187
                    throw new RuntimeException("There is no maximum set for "
c0864e216d36 7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents: 5506
diff changeset
   188
                            + p.getName() + " memory pool so the test is invalid");
c0864e216d36 7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents: 5506
diff changeset
   189
                }
c0864e216d36 7196801: NPG: Fix java/lang/management/MemoryMXBean/LowMemoryTest2
stefank
parents: 5506
diff changeset
   190
                long threshold = (max * 80) / 100;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                p.setUsageThreshold(threshold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                System.out.println("Selected memory pool for low memory " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        "detection.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                MemoryUtil.printMemoryPool(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        // Install the listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        SensorListener l2 = new SensorListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        NotificationEmitter emitter = (NotificationEmitter) mm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        emitter.addNotificationListener(l2, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        // Start the thread loading classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        Thread thr = new Thread(new BoundlessLoaderThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        thr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        // Wait for the thread to terminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            thr.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        } catch (InterruptedException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw new RuntimeException(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (listenerInvoked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            System.out.println("Notification received - test passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            throw new RuntimeException("Test failed - notification not received!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
}