1/*
2 * Copyright (c) 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
24import java.util.concurrent.TimeUnit;
25import java.util.concurrent.locks.Condition;
26import java.util.concurrent.locks.ReentrantLock;
27
28import java.lang.reflect.Method;
29import java.lang.reflect.Field;
30
31/*
32 * @test
33 * @summary Unit test for FinalizerHistogram
34 * @modules java.base/java.lang.ref:open
35 * @run main FinalizerHistogramTest
36 */
37
38public class FinalizerHistogramTest {
39    static ReentrantLock lock = new ReentrantLock();
40    static volatile int wasInitialized = 0;
41    static volatile int wasTrapped = 0;
42    static final int objectsCount = 1000;
43
44    static class MyObject {
45        public MyObject() {
46            // Make sure object allocation/deallocation is not optimized out
47            wasInitialized += 1;
48        }
49
50        protected void finalize() {
51            // Trap the object in a finalization queue
52            wasTrapped += 1;
53            lock.lock();
54        }
55    }
56
57    public static void main(String[] argvs) {
58        try {
59            lock.lock();
60            for(int i = 0; i < objectsCount; ++i) {
61                new MyObject();
62            }
63            System.out.println("Objects intialized: " + objectsCount);
64            System.gc();
65            while(wasTrapped < 1);
66
67            Class<?> klass = Class.forName("java.lang.ref.FinalizerHistogram");
68
69            Method m = klass.getDeclaredMethod("getFinalizerHistogram");
70            m.setAccessible(true);
71            Object entries[] = (Object[]) m.invoke(null);
72
73            Class<?> entryKlass = Class.forName("java.lang.ref.FinalizerHistogram$Entry");
74            Field name = entryKlass.getDeclaredField("className");
75            name.setAccessible(true);
76            Field count = entryKlass.getDeclaredField("instanceCount");
77            count.setAccessible(true);
78
79            System.out.println("Unreachable instances waiting for finalization");
80            System.out.println("#instances  class name");
81            System.out.println("-----------------------");
82
83            boolean found = false;
84            for (Object entry : entries) {
85                Object e = entryKlass.cast(entry);
86                System.out.printf("%10d %s\n", count.get(e), name.get(e));
87                if (((String) name.get(e)).indexOf("MyObject") != -1 ) {
88                    found = true;
89                }
90            }
91
92            if (!found) {
93                throw new RuntimeException("MyObject is not found in test output");
94            }
95
96            System.out.println("Test PASSED");
97        } catch(Exception e) {
98           System.err.println("Test failed with " + e);
99           e.printStackTrace(System.err);
100           throw new RuntimeException("Test failed");
101        } finally {
102            lock.unlock();
103        }
104    }
105}
106