Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/management/MemoryMXBean/Pending.java
38821 views
/*1* Copyright (c) 2003, 2010, 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*/3637import java.lang.management.*;3839public class Pending {40final static int NO_REF_COUNT = 600;41final static int REF_COUNT = 600;42final static int TOTAL_FINALIZABLE = (NO_REF_COUNT + REF_COUNT);43private static int finalized = 0;44private static MemoryMXBean mbean45= ManagementFactory.getMemoryMXBean();4647private static final String INDENT = " ";48private static void printFinalizerInstanceCount() {49if (!trace) return;5051int count = sun.misc.VM.getFinalRefCount();52System.out.println(INDENT + "Finalizable object Count = " + count);5354count = sun.misc.VM.getPeakFinalRefCount();55System.out.println(INDENT + "Peak Finalizable object Count = " + count);56}5758private static boolean trace = false;59public static void main(String argv[]) throws Exception {60if (argv.length > 0 && argv[0].equals("trace")) {61trace = true;62}6364try {65if (trace) {66// Turn on verbose:gc to track GC67mbean.setVerbose(true);68}69test();70} finally {71if (trace) {72mbean.setVerbose(false);73}74}75System.out.println("Test passed.");76}7778private static void test() throws Exception {79// Clean the memory and remove all objects that are pending80// finalization81System.gc();82Runtime.getRuntime().runFinalization();8384// Let the finalizer to finish85try {86Thread.sleep(200);87} catch (Exception e) {88throw e;89}9091// Create a number of new objects but no references to them92int startCount = mbean.getObjectPendingFinalizationCount();9394System.out.println("Number of objects pending for finalization:");95System.out.println(" Before creating object: " + startCount +96" finalized = " + finalized);97printFinalizerInstanceCount();9899for (int i = 0; i < NO_REF_COUNT; i++) {100new MyObject();101}102103Snapshot snapshot = getSnapshot();104System.out.println(" Afer creating objects with no ref: " + snapshot);105printFinalizerInstanceCount();106107Object[] objs = new Object[REF_COUNT];108for (int i = 0; i < REF_COUNT; i++) {109objs[i] = new MyObject();110}111snapshot = getSnapshot();112System.out.println(" Afer creating objects with ref: " + snapshot);113printFinalizerInstanceCount();114115// Now check the expected count - GC and runFinalization will be116// invoked.117checkFinalizerCount(NO_REF_COUNT, 0);118119// Clean the memory and remove all objects that are pending120// finalization again121objs = null;122snapshot = getSnapshot();123System.out.println("Clear all references finalized = " + snapshot);124printFinalizerInstanceCount();125126checkFinalizerCount(TOTAL_FINALIZABLE, NO_REF_COUNT);127128snapshot = getSnapshot();129printFinalizerInstanceCount();130131// Check the mbean now132if (snapshot.curFinalized != TOTAL_FINALIZABLE) {133throw new RuntimeException("Wrong number of finalized objects "134+ snapshot + ". Expected "135+ TOTAL_FINALIZABLE);136}137138if (startCount != 0 || snapshot.curPending != 0) {139throw new RuntimeException("Wrong number of objects pending "140+ "finalization start = " + startCount141+ " end = " + snapshot);142}143144}145146private static void checkFinalizerCount(int expectedTotal, int curFinalized)147throws Exception {148int prevCount = -1;149Snapshot snapshot = getSnapshot();150if (snapshot.curFinalized != curFinalized) {151throw new RuntimeException(152"Unexpected finalized objects: " + snapshot +153" but expected = " + curFinalized);154}155int MAX_GC_LOOP = 6;156for (int i = 1;157snapshot.curFinalized != expectedTotal && i <= MAX_GC_LOOP;158i++) {159System.gc();160161// Pause to give a chance to Finalizer thread to run162pause();163164printFinalizerInstanceCount();165// Race condition may occur; attempt to check this166// a few times before throwing exception.167for (int j = 0; j < 5; j++) {168// poll for another current pending count169snapshot = getSnapshot();170if (snapshot.curFinalized == expectedTotal ||171snapshot.curPending != 0) {172break;173}174}175System.out.println(" After GC " + i + ": " + snapshot);176177Runtime.getRuntime().runFinalization();178179// Pause to give a chance to Finalizer thread to run180pause();181182snapshot = getSnapshot();183if (snapshot.curFinalized == expectedTotal &&184snapshot.curPending != 0) {185throw new RuntimeException(186"Unexpected current number of objects pending for " +187"finalization: " + snapshot + " but expected = 0");188}189190System.out.println(" After runFinalization " + i + ": " + snapshot);191printFinalizerInstanceCount();192193try {194Thread.sleep(1000);195} catch (Exception e) {196throw e;197}198}199if (snapshot.curFinalized != expectedTotal) {200throw new RuntimeException(201"Unexpected current number of objects pending for " +202"finalization: " + snapshot + " but expected > 0");203}204}205206private static Object lock = new Object();207private static class MyObject {208Object[] dummy = new Object[10];209public void finalize () {210synchronized (lock) {211finalized++;212}213}214}215216static class Snapshot {217public int curFinalized;218public int curPending;219Snapshot(int f, int p) {220curFinalized = f;221curPending = p;222}223public String toString() {224return "Current finalized = " + curFinalized +225" Current pending = " + curPending;226}227}228229private static Snapshot getSnapshot() {230synchronized (lock) {231int curCount = mbean.getObjectPendingFinalizationCount();232return new Snapshot(finalized, curCount);233}234}235236private static Object pauseObj = new Object();237private static void pause() {238// Enter lock a without blocking239synchronized (pauseObj) {240try {241// may need to tune this timeout for different platforms242pauseObj.wait(20);243} catch (Exception e) {244System.err.println("Unexpected exception.");245e.printStackTrace(System.err);246}247}248}249}250251252