1 /*
   2  * Copyright (c) 2003, 2015, 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     4530538
  27  * @summary Basic unit test of
  28  *          RuntimeMXBean.getObjectPendingFinalizationCount()
  29  *          1. GC and runFinalization() to get the current pending number
  30  *          2. Create some number of objects with reference and without ref.
  31  *          3. Clear all the references
  32  *          4. GC and runFinalization() and the finalizable objects should
  33  *             be garbage collected.
  34  * @author  Alexei Guibadoulline and Mandy Chung
  35  *
  36  * @modules java.base/jdk.internal.misc
  37  *          java.management
  38  */
  39 
  40 import java.lang.management.*;
  41 
  42 public class Pending {
  43     static final int NO_REF_COUNT = 600;
  44     static final int REF_COUNT = 500;
  45     static final int TOTAL_FINALIZABLE = (NO_REF_COUNT + REF_COUNT);
  46     private static int finalized = 0;
  47     private static MemoryMXBean mbean
  48         = ManagementFactory.getMemoryMXBean();
  49 
  50     private static final String INDENT = "      ";
  51     private static void printFinalizerInstanceCount() {
  52         if (!trace) return;
  53 
  54         int count = jdk.internal.misc.VM.getFinalRefCount();
  55         System.out.println(INDENT + "Finalizable object Count = " + count);
  56 
  57         count = jdk.internal.misc.VM.getPeakFinalRefCount();
  58         System.out.println(INDENT + "Peak Finalizable object Count = " + count);
  59     }
  60 
  61     private static boolean trace = false;
  62     public static void main(String argv[]) throws Exception {
  63         if (argv.length > 0 && argv[0].equals("trace")) {
  64             trace = true;
  65         }
  66 
  67         try {
  68             if (trace) {
  69                 // Turn on verbose:gc to track GC
  70                 mbean.setVerbose(true);
  71             }
  72             test();
  73         } finally {
  74             if (trace) {
  75                 mbean.setVerbose(false);
  76             }
  77         }
  78         System.out.println("Test passed.");
  79     }
  80 
  81     // Keep objs public so the optimizer will not remove them too early.
  82     public static Object[] objs = null;
  83 
  84     private static void test() throws Exception {
  85         // Clean the memory and remove all objects that are pending
  86         // finalization
  87         System.gc();
  88         Snapshot snapshot = getSnapshotAfterFinalization();
  89 
  90         System.out.println("Number of objects pending for finalization:");
  91         System.out.println("   Before creating object: " + snapshot);
  92         printFinalizerInstanceCount();
  93 
  94         // Create objects without saving reference. Should be removed at next GC.
  95         for (int i = 0; i < NO_REF_COUNT; i++) {
  96             new MyObject();
  97         }
  98 
  99         snapshot = getSnapshot();
 100         System.out.println("   Afer creating objects with no ref: " + snapshot);
 101         printFinalizerInstanceCount();
 102 
 103         // Create objects and save references.
 104         objs = new Object[REF_COUNT];
 105         for (int i = 0; i < REF_COUNT; i++) {
 106             objs[i] = new MyObject();
 107         }
 108         snapshot = getSnapshot();
 109         System.out.println("   Afer creating objects with ref: " + snapshot);
 110         printFinalizerInstanceCount();
 111 
 112         // Now check the expected count - GC and runFinalization will be
 113         // invoked.
 114         checkFinalizerCount(NO_REF_COUNT, 0);
 115 
 116         // Clean the memory and remove all objects that are pending
 117         // finalization again
 118         objs = null;
 119         snapshot = getSnapshot();
 120         System.out.println("Clear all references finalized = " + snapshot);
 121         printFinalizerInstanceCount();
 122 
 123         checkFinalizerCount(TOTAL_FINALIZABLE, NO_REF_COUNT);
 124 
 125         snapshot = getSnapshot();
 126         printFinalizerInstanceCount();
 127 
 128         // Check the mbean now
 129         if (snapshot.curFinalized != TOTAL_FINALIZABLE) {
 130             throw new RuntimeException("Wrong number of finalized objects "
 131                                      + snapshot + ". Expected "
 132                                      + TOTAL_FINALIZABLE);
 133         }
 134 
 135         if (snapshot.curPending != 0) {
 136             throw new RuntimeException("Wrong number of objects pending "
 137                                      + " end = " + snapshot);
 138         }
 139 
 140     }
 141 
 142     private static void checkFinalizerCount(int expectedTotal, int curFinalized)
 143         throws Exception {
 144         int prevCount = -1;
 145         Snapshot snapshot = getSnapshot();
 146         if (snapshot.curFinalized != curFinalized) {
 147             throw new RuntimeException(
 148                     "Unexpected finalized objects: " + snapshot +
 149                     " but expected = " + curFinalized);
 150         }
 151         int MAX_GC_LOOP = 6;
 152         for (int i = 1;
 153              snapshot.curFinalized != expectedTotal && i <= MAX_GC_LOOP;
 154              i++) {
 155             System.gc();
 156             snapshot = getSnapshotAfterFinalization();
 157 
 158             if (snapshot.curFinalized == expectedTotal &&
 159                 snapshot.curPending != 0) {
 160                 throw new RuntimeException(
 161                     "Unexpected current number of objects pending for " +
 162                     "finalization: " + snapshot + " but expected = 0");
 163             }
 164 
 165             System.out.println("   After runFinalization " + i + ": " + snapshot);
 166             printFinalizerInstanceCount();
 167 
 168             try {
 169                 Thread.sleep(1000);
 170             } catch (Exception e) {
 171                 throw e;
 172             }
 173         }
 174         if (snapshot.curFinalized != expectedTotal) {
 175             throw new RuntimeException(
 176                 "Unexpected current number of objects pending for " +
 177                 "finalization: " + snapshot + " but expected > 0");
 178         }
 179     }
 180 
 181     private static Object lock = new Object();
 182     private static class MyObject {
 183         Object[] dummy = new Object[10];
 184         public void finalize () {
 185             synchronized (lock) {
 186                 finalized++;
 187             }
 188         }
 189     }
 190 
 191     static class Snapshot {
 192         public int curFinalized;
 193         public int curPending;
 194         Snapshot(int f, int p) {
 195             curFinalized = f;
 196             curPending = p;
 197         }
 198         public String toString() {
 199             return "Current finalized = " + curFinalized +
 200                    " Current pending = " + curPending;
 201         }
 202     }
 203 
 204     private static Snapshot getSnapshot() {
 205         synchronized (lock) {
 206             int curCount = mbean.getObjectPendingFinalizationCount();
 207             return new Snapshot(finalized, curCount);
 208         }
 209     }
 210 
 211     // Repeat getSnapshot until no pending finalization.
 212     private static Snapshot getSnapshotAfterFinalization() throws Exception {
 213         int loopCount = 0;
 214         Snapshot snapshot = null;
 215         while (loopCount < 100) {
 216             Runtime.getRuntime().runFinalization();
 217             Thread.sleep(50);
 218             snapshot = getSnapshot();
 219             if (snapshot.curPending == 0) {
 220                 return snapshot;
 221             }
 222             ++loopCount;
 223             System.out.println("Waiting for curPending to be 0. snapshot=" + snapshot);
 224         }
 225         String msg = "Objects pending finalization is not 0. snapshot=%s";
 226         throw new RuntimeException(String.format(msg, snapshot));
 227     }
 228 }