Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/ref/FinalizerHistogramTest.java
38812 views
/*1* Copyright (c) 2015, 2017, 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*/2223import java.util.concurrent.TimeUnit;24import java.util.concurrent.locks.Condition;25import java.util.concurrent.locks.ReentrantLock;2627import java.lang.reflect.Method;28import java.lang.reflect.Field;2930/*31* @test32* @summary Unit test for FinalizerHistogram33* @run main FinalizerHistogramTest34*/3536public class FinalizerHistogramTest {37static ReentrantLock lock = new ReentrantLock();38static volatile int wasInitialized = 0;39static volatile int wasTrapped = 0;40static final int objectsCount = 1000;4142static class MyObject {43public MyObject() {44// Make sure object allocation/deallocation is not optimized out45wasInitialized += 1;46}4748protected void finalize() {49// Trap the object in a finalization queue50wasTrapped += 1;51lock.lock();52}53}5455public static void main(String[] argvs) {56try {57lock.lock();58for(int i = 0; i < objectsCount; ++i) {59new MyObject();60}61System.out.println("Objects intialized: " + objectsCount);62System.gc();63while(wasTrapped < 1);6465Class<?> klass = Class.forName("java.lang.ref.FinalizerHistogram");6667Method m = klass.getDeclaredMethod("getFinalizerHistogram");68m.setAccessible(true);69Object entries[] = (Object[]) m.invoke(null);7071Class<?> entryKlass = Class.forName("java.lang.ref.FinalizerHistogram$Entry");72Field name = entryKlass.getDeclaredField("className");73name.setAccessible(true);74Field count = entryKlass.getDeclaredField("instanceCount");75count.setAccessible(true);7677System.out.println("Unreachable instances waiting for finalization");78System.out.println("#instances class name");79System.out.println("-----------------------");8081boolean found = false;82for (Object entry : entries) {83Object e = entryKlass.cast(entry);84System.out.printf("%10d %s\n", count.get(e), name.get(e));85if (((String) name.get(e)).indexOf("MyObject") != -1 ) {86found = true;87}88}8990if (!found) {91throw new RuntimeException("MyObject is not found in test output");92}9394System.out.println("Test PASSED");95} catch(Exception e) {96System.err.println("Test failed with " + e);97e.printStackTrace(System.err);98throw new RuntimeException("Test failed");99} finally {100lock.unlock();101}102}103}104105106