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