Skip to content

Commit fe29cad

Browse files
author
Brent Christian
committed
8298783: java/lang/ref/FinalizerHistogramTest.java failed with "RuntimeException: MyObject is not found in test output"
Reviewed-by: kbarrett, jpai
1 parent 0cb355e commit fe29cad

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

test/jdk/java/lang/ref/FinalizerHistogramTest.java

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,8 +21,9 @@
2121
* questions.
2222
*/
2323

24-
import java.util.concurrent.TimeUnit;
25-
import java.util.concurrent.locks.Condition;
24+
import jdk.test.whitebox.WhiteBox;
25+
26+
import java.util.concurrent.atomic.AtomicInteger;
2627
import java.util.concurrent.locks.ReentrantLock;
2728

2829
import java.lang.reflect.Method;
@@ -32,37 +33,49 @@
3233
* @test
3334
* @summary Unit test for FinalizerHistogram
3435
* @modules java.base/java.lang.ref:open
35-
* @run main FinalizerHistogramTest
36+
* @library /test/lib
37+
* @build jdk.test.whitebox.WhiteBox
38+
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
39+
* @run main/othervm
40+
* -Xbootclasspath/a:.
41+
* -XX:+UnlockDiagnosticVMOptions
42+
* -XX:+WhiteBoxAPI
43+
* FinalizerHistogramTest
3644
*/
3745

3846
public class FinalizerHistogramTest {
3947
static ReentrantLock lock = new ReentrantLock();
40-
static volatile int wasInitialized = 0;
41-
static volatile int wasTrapped = 0;
42-
static final int objectsCount = 1000;
48+
static final AtomicInteger initializedCount = new AtomicInteger(0);
49+
static final int OBJECTS_COUNT = 1000;
50+
51+
static WhiteBox wb;
4352

4453
static class MyObject {
4554
public MyObject() {
4655
// Make sure object allocation/deallocation is not optimized out
47-
wasInitialized += 1;
56+
initializedCount.incrementAndGet();
4857
}
4958

5059
protected void finalize() {
5160
// Trap the object in a finalization queue
52-
wasTrapped += 1;
5361
lock.lock();
5462
}
5563
}
5664

57-
public static void main(String[] argvs) {
65+
public static void main(String[] argvs) throws InterruptedException {
5866
try {
5967
lock.lock();
60-
for(int i = 0; i < objectsCount; ++i) {
68+
for(int i = 0; i < OBJECTS_COUNT; ++i) {
6169
new MyObject();
6270
}
63-
System.out.println("Objects intialized: " + objectsCount);
64-
System.gc();
65-
while(wasTrapped < 1);
71+
System.out.println("Objects intialized: " + initializedCount.get());
72+
wb = WhiteBox.getWhiteBox();
73+
wb.fullGC();
74+
boolean refProResult;
75+
do {
76+
refProResult = wb.waitForReferenceProcessing();
77+
System.out.println("waitForReferenceProcessing returned: " + refProResult);
78+
} while (refProResult);
6679

6780
Class<?> klass = Class.forName("java.lang.ref.FinalizerHistogram");
6881

0 commit comments

Comments
 (0)