11/*
2- * Copyright (c) 2014, 2016 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2014, 2023 , 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
2121 * questions.
2222 */
2323
24- /**
24+ import java .awt .Frame ;
25+ import java .awt .List ;
26+ import java .lang .ref .PhantomReference ;
27+ import java .lang .ref .Reference ;
28+ import java .lang .ref .ReferenceQueue ;
29+
30+ import jdk .test .lib .util .ForceGC ;
31+
32+ /*
2533 * @test
2634 * @key headful
2735 * @bug 8040076
2836 * @summary AwtList not garbage collected
29- * @run main/othervm -Xmx100m AwtListGarbageCollectionTest
37+ * @library /test/lib/
38+ * @build jdk.test.lib.util.ForceGC
39+ * @run main/othervm -Xmx100m -Xlog:gc=debug AwtListGarbageCollectionTest
3040 */
41+ public class AwtListGarbageCollectionTest {
3142
32- import java .awt .*;
33- import java .awt .event .WindowAdapter ;
34- import java .awt .event .WindowEvent ;
35- import java .lang .ref .WeakReference ;
43+ private static final long ENQUEUE_TIMEOUT = 50 ;
3644
37- public class AwtListGarbageCollectionTest {
38- public static void main (String [] args ) {
45+ public static void main (String [] args ) throws InterruptedException {
3946 Frame frame = new Frame ("List leak test" );
4047 try {
4148 test (frame );
@@ -45,29 +52,32 @@ public static void main(String[] args) {
4552 }
4653
4754 private static void test (Frame frame ) {
48- WeakReference <List > weakListRef = null ;
49- try {
50- frame .setSize (300 , 200 );
51- frame .setVisible (true );
55+ frame .setSize (300 , 200 );
56+ frame .setVisible (true );
5257
53- List strongListRef = new List ();
54- frame .add (strongListRef );
55- strongListRef .setMultipleMode (true );
56- frame .remove (strongListRef );
57- weakListRef = new WeakReference <List >(strongListRef );
58- strongListRef = null ;
58+ List strongListRef = new List ();
59+ frame .add (strongListRef );
60+ strongListRef .setMultipleMode (true );
61+ frame .remove (strongListRef );
5962
60- //make out of memory to force gc
61- String veryLongString = new String (new char [100 ]);
62- while (true ) {
63- veryLongString += veryLongString ;
64- }
65- } catch (OutOfMemoryError e ) {
66- if (weakListRef == null ) {
67- throw new RuntimeException ("Weak list ref wasn't created" );
68- } else if (weakListRef .get () != null ) {
69- throw new RuntimeException ("List wasn't garbage collected" );
70- }
63+ final ReferenceQueue <List > referenceQueue = new ReferenceQueue <>();
64+ final PhantomReference <List > phantomListRef =
65+ new PhantomReference <>(strongListRef , referenceQueue );
66+ System .out .println ("phantomListRef: " + phantomListRef );
67+
68+ strongListRef = null ; // Clear the strong reference
69+
70+ System .out .println ("Waiting for the reference to be cleared" );
71+ if (!ForceGC .wait (() -> phantomListRef == remove (referenceQueue ))) {
72+ throw new RuntimeException ("List wasn't garbage collected" );
73+ }
74+ }
75+
76+ private static Reference <?> remove (ReferenceQueue <?> queue ) {
77+ try {
78+ return queue .remove (ENQUEUE_TIMEOUT );
79+ } catch (InterruptedException e ) {
80+ throw new RuntimeException (e );
7181 }
7282 }
7383}
0 commit comments