< prev index next >

test/jdk/java/lang/management/MemoryMXBean/MemoryManagement.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2016, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


  65                 MemoryNotificationInfo minfo = MemoryNotificationInfo.
  66                     from((CompositeData) notif.getUserData());
  67 
  68                 MemoryUtil.printMemoryNotificationInfo(minfo, type);
  69                 listenerInvoked++;
  70             }
  71         }
  72     }
  73 
  74     private static long newThreshold;
  75     public static void main(String args[]) throws Exception {
  76         if (args.length > 0 && args[0].equals("trace")) {
  77             trace = true;
  78         }
  79 
  80         if (trace) {
  81             MemoryUtil.printMemoryPools(pools);
  82             MemoryUtil.printMemoryManagers(managers);
  83         }
  84 
  85         // Find the Old generation which supports low memory detection
  86         ListIterator iter = pools.listIterator();
  87         while (iter.hasNext()) {
  88             MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
  89             if (p.getType() == MemoryType.HEAP &&
  90                 p.isUsageThresholdSupported()) {










  91                 mpool = p;
  92                 if (trace) {
  93                     System.out.println("Selected memory pool for low memory " +
  94                         "detection.");
  95                     MemoryUtil.printMemoryPool(mpool);
  96                 }
  97                 break;
  98             }
  99         }




 100 
 101         SensorListener listener = new SensorListener();
 102         NotificationEmitter emitter = (NotificationEmitter) mm;
 103         emitter.addNotificationListener(listener, null, null);
 104 
 105         Thread allocator = new AllocatorThread();
 106 
 107         // The chunk size needs to be larger than YOUNG_GEN_SIZE,
 108         // otherwise we will get intermittent failures when objects
 109         // end up in the young gen instead of the old gen.
 110         final long epsilon = 1024;
 111         chunkSize = YOUNG_GEN_SIZE + epsilon;
 112 
 113         // Now set threshold
 114         MemoryUsage mu = mpool.getUsage();
 115         newThreshold = mu.getUsed() + (chunkSize * NUM_CHUNKS);
 116 
 117         // Sanity check. Make sure the new threshold isn't too large.
 118         // Tweak the test if this fails.
 119         final long headRoom = chunkSize * 2;


   1 /*
   2  * Copyright (c) 2003, 2018, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


  65                 MemoryNotificationInfo minfo = MemoryNotificationInfo.
  66                     from((CompositeData) notif.getUserData());
  67 
  68                 MemoryUtil.printMemoryNotificationInfo(minfo, type);
  69                 listenerInvoked++;
  70             }
  71         }
  72     }
  73 
  74     private static long newThreshold;
  75     public static void main(String args[]) throws Exception {
  76         if (args.length > 0 && args[0].equals("trace")) {
  77             trace = true;
  78         }
  79 
  80         if (trace) {
  81             MemoryUtil.printMemoryPools(pools);
  82             MemoryUtil.printMemoryManagers(managers);
  83         }
  84 
  85         // Find a pool which which supports low memory detection
  86         ListIterator iter = pools.listIterator();
  87         while (iter.hasNext()) {
  88             MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
  89             if (p.getType() == MemoryType.HEAP &&
  90                 p.isUsageThresholdSupported()) {
  91                 if (!p.getName().equals("G1 Old Space")) {
  92                     // In G1, humongous objects are tracked in the old space only in
  93                     // legacy monitoring mode. In default mode, G1 tracks humongous
  94                     // objects in the humongous space, which latter also supports a
  95                     // usage threshold. Since we're allocating humongous objects in
  96                     // this test, in default mode the old space doesn't change. For
  97                     // this test, we use the old space in legacy mode (it's called
  98                     // "G1 Old Gen" and the humongous space in default mode. If we
  99                     // used "G1 Old Space" in default mode, notification would never
 100                     // happen.
 101                     mpool = p;
 102                     if (trace) {
 103                         System.out.println("Selected memory pool for low memory " +
 104                                            "detection.");
 105                         MemoryUtil.printMemoryPool(mpool);
 106                     }
 107                     break;
 108                 }
 109             }
 110         }
 111         if (mpool == null) {
 112             throw new RuntimeException("TEST FAILED: No heap pool found");
 113         }
 114 
 115         SensorListener listener = new SensorListener();
 116         NotificationEmitter emitter = (NotificationEmitter) mm;
 117         emitter.addNotificationListener(listener, null, null);
 118 
 119         Thread allocator = new AllocatorThread();
 120 
 121         // The chunk size needs to be larger than YOUNG_GEN_SIZE,
 122         // otherwise we will get intermittent failures when objects
 123         // end up in the young gen instead of the old gen.
 124         final long epsilon = 1024;
 125         chunkSize = YOUNG_GEN_SIZE + epsilon;
 126 
 127         // Now set threshold
 128         MemoryUsage mu = mpool.getUsage();
 129         newThreshold = mu.getUsed() + (chunkSize * NUM_CHUNKS);
 130 
 131         // Sanity check. Make sure the new threshold isn't too large.
 132         // Tweak the test if this fails.
 133         final long headRoom = chunkSize * 2;


< prev index next >