Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/ref/OOMEInReferenceHandler.java
38813 views
/*1* Copyright (c) 2013, 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 7038914 801634126* @summary Verify that the reference handler does not die after an OOME allocating the InterruptedException object27* @run main/othervm -Xmx24M -XX:-UseTLAB OOMEInReferenceHandler28* @author [email protected]29*/3031import java.lang.ref.*;3233public class OOMEInReferenceHandler {34static Object[] fillHeap() {35Object[] first = null, last = null;36int size = 1 << 20;37while (size > 0) {38try {39Object[] array = new Object[size];40if (first == null) {41first = array;42} else {43last[0] = array;44}45last = array;46} catch (OutOfMemoryError oome) {47size = size >>> 1;48}49}50return first;51}5253public static void main(String[] args) throws Exception {54// preinitialize the InterruptedException class so that the reference handler55// does not die due to OOME when loading the class if it is the first use56InterruptedException ie = new InterruptedException("dummy");5758ThreadGroup tg = Thread.currentThread().getThreadGroup();59for (60ThreadGroup tgn = tg;61tgn != null;62tg = tgn, tgn = tg.getParent()63)64;6566Thread[] threads = new Thread[tg.activeCount()];67Thread referenceHandlerThread = null;68int n = tg.enumerate(threads);69for (int i = 0; i < n; i++) {70if ("Reference Handler".equals(threads[i].getName())) {71referenceHandlerThread = threads[i];72}73}7475if (referenceHandlerThread == null) {76throw new IllegalStateException("Couldn't find Reference Handler thread.");77}7879ReferenceQueue<Object> refQueue = new ReferenceQueue<>();80Object referent = new Object();81WeakReference<Object> weakRef = new WeakReference<>(referent, refQueue);8283Object waste = fillHeap();8485referenceHandlerThread.interrupt();8687// allow referenceHandlerThread some time to throw OOME88Thread.sleep(500L);8990// release waste & referent91waste = null;92referent = null;9394// wait at most 10 seconds for success or failure95for (int i = 0; i < 20; i++) {96if (refQueue.poll() != null) {97// Reference Handler thread still working -> success98return;99}100System.gc();101Thread.sleep(500L); // wait a little to allow GC to do it's work before allocating objects102if (!referenceHandlerThread.isAlive()) {103// Reference Handler thread died -> failure104throw new Exception("Reference Handler thread died.");105}106}107108// no sure answer after 10 seconds109throw new IllegalStateException("Reference Handler thread stuck. weakRef.get(): " + weakRef.get());110}111}112113114