Path: blob/master/test/jdk/java/lang/management/MemoryMXBean/Pending.java
41155 views
/*1* Copyright (c) 2003, 2015, 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 453053826* @summary Basic unit test of27* RuntimeMXBean.getObjectPendingFinalizationCount()28* 1. GC and runFinalization() to get the current pending number29* 2. Create some number of objects with reference and without ref.30* 3. Clear all the references31* 4. GC and runFinalization() and the finalizable objects should32* be garbage collected.33* @author Alexei Guibadoulline and Mandy Chung34*35* @modules java.base/jdk.internal.misc36* java.management37*/3839import java.lang.management.*;4041public class Pending {42static final int NO_REF_COUNT = 600;43static final int REF_COUNT = 500;44static final int TOTAL_FINALIZABLE = (NO_REF_COUNT + REF_COUNT);45private static int finalized = 0;46private static MemoryMXBean mbean47= ManagementFactory.getMemoryMXBean();4849private static final String INDENT = " ";50private static void printFinalizerInstanceCount() {51if (!trace) return;5253int count = jdk.internal.misc.VM.getFinalRefCount();54System.out.println(INDENT + "Finalizable object Count = " + count);5556count = jdk.internal.misc.VM.getPeakFinalRefCount();57System.out.println(INDENT + "Peak Finalizable object Count = " + count);58}5960private static boolean trace = false;61public static void main(String argv[]) throws Exception {62if (argv.length > 0 && argv[0].equals("trace")) {63trace = true;64}6566try {67if (trace) {68// Turn on verbose:gc to track GC69mbean.setVerbose(true);70}71test();72} finally {73if (trace) {74mbean.setVerbose(false);75}76}77System.out.println("Test passed.");78}7980// Keep objs public so the optimizer will not remove them too early.81public static Object[] objs = null;8283private static void test() throws Exception {84// Clean the memory and remove all objects that are pending85// finalization86System.gc();87Snapshot snapshot = getSnapshotAfterFinalization();8889System.out.println("Number of objects pending for finalization:");90System.out.println(" Before creating object: " + snapshot);91printFinalizerInstanceCount();9293// Create objects without saving reference. Should be removed at next GC.94for (int i = 0; i < NO_REF_COUNT; i++) {95new MyObject();96}9798snapshot = getSnapshot();99System.out.println(" Afer creating objects with no ref: " + snapshot);100printFinalizerInstanceCount();101102// Create objects and save references.103objs = new Object[REF_COUNT];104for (int i = 0; i < REF_COUNT; i++) {105objs[i] = new MyObject();106}107snapshot = getSnapshot();108System.out.println(" Afer creating objects with ref: " + snapshot);109printFinalizerInstanceCount();110111// Now check the expected count - GC and runFinalization will be112// invoked.113checkFinalizerCount(NO_REF_COUNT, 0);114115// Clean the memory and remove all objects that are pending116// finalization again117objs = null;118snapshot = getSnapshot();119System.out.println("Clear all references finalized = " + snapshot);120printFinalizerInstanceCount();121122checkFinalizerCount(TOTAL_FINALIZABLE, NO_REF_COUNT);123124snapshot = getSnapshot();125printFinalizerInstanceCount();126127// Check the mbean now128if (snapshot.curFinalized != TOTAL_FINALIZABLE) {129throw new RuntimeException("Wrong number of finalized objects "130+ snapshot + ". Expected "131+ TOTAL_FINALIZABLE);132}133134if (snapshot.curPending != 0) {135throw new RuntimeException("Wrong number of objects pending "136+ " end = " + snapshot);137}138139}140141private static void checkFinalizerCount(int expectedTotal, int curFinalized)142throws Exception {143int prevCount = -1;144Snapshot snapshot = getSnapshot();145if (snapshot.curFinalized != curFinalized) {146throw new RuntimeException(147"Unexpected finalized objects: " + snapshot +148" but expected = " + curFinalized);149}150int MAX_GC_LOOP = 6;151for (int i = 1;152snapshot.curFinalized != expectedTotal && i <= MAX_GC_LOOP;153i++) {154System.gc();155snapshot = getSnapshotAfterFinalization();156157if (snapshot.curFinalized == expectedTotal &&158snapshot.curPending != 0) {159throw new RuntimeException(160"Unexpected current number of objects pending for " +161"finalization: " + snapshot + " but expected = 0");162}163164System.out.println(" After runFinalization " + i + ": " + snapshot);165printFinalizerInstanceCount();166167try {168Thread.sleep(1000);169} catch (Exception e) {170throw e;171}172}173if (snapshot.curFinalized != expectedTotal) {174throw new RuntimeException(175"Unexpected current number of objects pending for " +176"finalization: " + snapshot + " but expected > 0");177}178}179180private static Object lock = new Object();181private static class MyObject {182Object[] dummy = new Object[10];183public void finalize () {184synchronized (lock) {185finalized++;186}187}188}189190static class Snapshot {191public int curFinalized;192public int curPending;193Snapshot(int f, int p) {194curFinalized = f;195curPending = p;196}197public String toString() {198return "Current finalized = " + curFinalized +199" Current pending = " + curPending;200}201}202203private static Snapshot getSnapshot() {204synchronized (lock) {205int curCount = mbean.getObjectPendingFinalizationCount();206return new Snapshot(finalized, curCount);207}208}209210// Repeat getSnapshot until no pending finalization.211private static Snapshot getSnapshotAfterFinalization() throws Exception {212int loopCount = 0;213Snapshot snapshot = null;214while (loopCount < 100) {215Runtime.getRuntime().runFinalization();216Thread.sleep(50);217snapshot = getSnapshot();218if (snapshot.curPending == 0) {219return snapshot;220}221++loopCount;222System.out.println("Waiting for curPending to be 0. snapshot=" + snapshot);223}224String msg = "Objects pending finalization is not 0. snapshot=%s";225throw new RuntimeException(String.format(msg, snapshot));226}227}228229230