Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/MemoryMXBean/MemoryManagement.java
38821 views
/*1* Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4530538 698098426* @summary Basic unit test of memory management testing:27* 1) setUsageThreshold() and getUsageThreshold()28* 2) test low memory detection on the old generation.29*30* @author Mandy Chung31*32* @build MemoryManagement MemoryUtil33* @run main/othervm/timeout=600 MemoryManagement34*/3536import java.lang.management.*;37import java.util.*;38import javax.management.*;39import javax.management.openmbean.CompositeData;4041public class MemoryManagement {42private static final MemoryMXBean mm = ManagementFactory.getMemoryMXBean();43private static final List pools =44Collections.synchronizedList(ManagementFactory.getMemoryPoolMXBeans());45private static final List managers =46Collections.synchronizedList(ManagementFactory.getMemoryManagerMXBeans());47private static volatile MemoryPoolMXBean mpool = null;48private static volatile boolean trace = false;49private static volatile boolean testFailed = false;50private static final int NUM_CHUNKS = 2;51private static volatile long chunkSize;52private static volatile int listenerInvoked = 0;5354static class SensorListener implements NotificationListener {55public void handleNotification(Notification notif, Object handback) {56String type = notif.getType();57if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) ||58type.equals(MemoryNotificationInfo.59MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) {6061MemoryNotificationInfo minfo = MemoryNotificationInfo.62from((CompositeData) notif.getUserData());6364MemoryUtil.printMemoryNotificationInfo(minfo, type);65listenerInvoked++;66}67}68}6970private static long newThreshold;71public static void main(String args[]) throws Exception {72if (args.length > 0 && args[0].equals("trace")) {73trace = true;74}7576if (trace) {77MemoryUtil.printMemoryPools(pools);78MemoryUtil.printMemoryManagers(managers);79}8081// Find the Old generation which supports low memory detection82ListIterator iter = pools.listIterator();83while (iter.hasNext()) {84MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();85if (p.getType() == MemoryType.HEAP &&86p.isUsageThresholdSupported()) {87mpool = p;88if (trace) {89System.out.println("Selected memory pool for low memory " +90"detection.");91MemoryUtil.printMemoryPool(mpool);92}93break;94}95}9697SensorListener listener = new SensorListener();98NotificationEmitter emitter = (NotificationEmitter) mm;99emitter.addNotificationListener(listener, null, null);100101Thread allocator = new AllocatorThread();102103// Now set threshold104MemoryUsage mu = mpool.getUsage();105long max = mu.getMax();106if (max != -1) {107chunkSize = (max - mu.getUsed()) / 20;108} else { // 6980984109System.gc();110chunkSize = Runtime.getRuntime().freeMemory()/20;111}112newThreshold = mu.getUsed() + (chunkSize * NUM_CHUNKS);113114System.out.println("Setting threshold for " + mpool.getName() +115" from " + mpool.getUsageThreshold() + " to " + newThreshold +116". Current used = " + mu.getUsed());117mpool.setUsageThreshold(newThreshold);118119if (mpool.getUsageThreshold() != newThreshold) {120throw new RuntimeException("TEST FAILED: " +121"Threshold for Memory pool " + mpool.getName() +122"is " + mpool.getUsageThreshold() + " but expected to be" +123newThreshold);124}125126// Start the AllocatorThread to continously allocate memory127System.out.println("Starting an AllocatorThread to allocate memory.");128allocator.start();129130try {131allocator.join();132} catch (InterruptedException e) {133e.printStackTrace();134System.out.println("Unexpected exception.");135testFailed = true;136}137138if (listenerInvoked == 0) {139throw new RuntimeException("No listener is invoked");140}141142if (testFailed)143throw new RuntimeException("TEST FAILED.");144145System.out.println("Test passed.");146147}148149static class AllocatorThread extends Thread {150private List objectPool = new ArrayList();151public void run() {152int iterations = 0;153int numElements = (int) (chunkSize / 4); // minimal object size154while (listenerInvoked == 0) {155iterations++;156if (trace) {157System.out.println(" Iteration " + iterations +158": before allocation " +159mpool.getUsage().getUsed());160}161162Object[] o = new Object[numElements];163if (iterations <= NUM_CHUNKS) {164// only hold a reference to the first NUM_CHUNKS165// allocated objects166objectPool.add(o);167}168169if (trace) {170System.out.println(" " +171" after allocation " +172mpool.getUsage().getUsed());173}174try {175Thread.sleep(100);176} catch (InterruptedException e) {177e.printStackTrace();178System.out.println("Unexpected exception.");179testFailed = true;180}181}182183System.out.println("AllocatedThread finished memory allocation " +184" num_iteration = " + iterations);185}186}187188}189190191