1 /* 2 * Copyright (c) 2013, 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 */ 23 24 /** 25 * @test 26 * @bug 7038914 27 * @summary Verify that the reference handler does not die after an OOME allocating the InterruptedException object 28 * @run main/othervm -Xmx16M -XX:-UseTLAB OOMEInReferenceHandler 29 * @author peter.levart@gmail.com 30 */ 31 32 import java.lang.ref.*; 33 34 public class OOMEInReferenceHandler { 35 static Object[] fillHeap() { 36 Object[] first = null, last = null; 37 int size = 1 << 20; 38 while (size > 0) { 39 try { 40 Object[] array = new Object[size]; 41 if (first == null) { 42 first = array; 43 } else { 44 last[0] = array; 45 } 46 last = array; 47 } catch (OutOfMemoryError oome) { 48 size = size >>> 1; 49 } 50 } 51 return first; 52 } 53 54 public static void main(String[] args) throws Exception { 55 ThreadGroup tg = Thread.currentThread().getThreadGroup(); 56 for ( 57 ThreadGroup tgn = tg; 58 tgn != null; 59 tg = tgn, tgn = tg.getParent() 60 ) 61 ; 62 63 Thread[] threads = new Thread[tg.activeCount()]; 64 Thread referenceHandlerThread = null; 65 int n = tg.enumerate(threads); 66 for (int i = 0; i < n; i++) { 67 if ("Reference Handler".equals(threads[i].getName())) { 68 referenceHandlerThread = threads[i]; 69 } 70 } 71 72 if (referenceHandlerThread == null) { 73 throw new IllegalStateException("Couldn't find Reference Handler thread."); 74 } 75 76 ReferenceQueue<Object> refQueue = new ReferenceQueue<>(); 77 Object referent = new Object(); 78 WeakReference<Object> weakRef = new WeakReference<>(referent, refQueue); 79 80 Object waste = fillHeap(); 81 82 referenceHandlerThread.interrupt(); 83 84 // allow referenceHandlerThread some time to throw OOME 85 Thread.sleep(500L); 86 87 // release waste & referent 88 waste = null; 89 referent = null; 90 91 // wait at most 10 seconds for success or failure 92 for (int i = 0; i < 20; i++) { 93 if (refQueue.poll() != null) { 94 // Reference Handler thread still working -> success 95 return; 96 } 97 System.gc(); 98 Thread.sleep(500L); // wait a little to allow GC to do it's work before allocating objects 99 if (!referenceHandlerThread.isAlive()) { 100 // Reference Handler thread died -> failure 101 throw new Exception("Reference Handler thread died."); 102 } 103 } 104 105 // no sure answer after 10 seconds 106 throw new IllegalStateException("Reference Handler thread stuck."); 107 } 108 }