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