|
1 | 1 | /*
|
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. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
21 | 21 | * questions.
|
22 | 22 | */
|
23 | 23 |
|
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; |
26 | 27 | import java.util.concurrent.locks.ReentrantLock;
|
27 | 28 |
|
28 | 29 | import java.lang.reflect.Method;
|
|
32 | 33 | * @test
|
33 | 34 | * @summary Unit test for FinalizerHistogram
|
34 | 35 | * @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 |
36 | 44 | */
|
37 | 45 |
|
38 | 46 | public class FinalizerHistogramTest {
|
39 | 47 | 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; |
43 | 52 |
|
44 | 53 | static class MyObject {
|
45 | 54 | public MyObject() {
|
46 | 55 | // Make sure object allocation/deallocation is not optimized out
|
47 |
| - wasInitialized += 1; |
| 56 | + initializedCount.incrementAndGet(); |
48 | 57 | }
|
49 | 58 |
|
50 | 59 | protected void finalize() {
|
51 | 60 | // Trap the object in a finalization queue
|
52 |
| - wasTrapped += 1; |
53 | 61 | lock.lock();
|
54 | 62 | }
|
55 | 63 | }
|
56 | 64 |
|
57 |
| - public static void main(String[] argvs) { |
| 65 | + public static void main(String[] argvs) throws InterruptedException { |
58 | 66 | try {
|
59 | 67 | lock.lock();
|
60 |
| - for(int i = 0; i < objectsCount; ++i) { |
| 68 | + for(int i = 0; i < OBJECTS_COUNT; ++i) { |
61 | 69 | new MyObject();
|
62 | 70 | }
|
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); |
66 | 79 |
|
67 | 80 | Class<?> klass = Class.forName("java.lang.ref.FinalizerHistogram");
|
68 | 81 |
|
|
0 commit comments