Skip to content

Commit

Permalink
8300727: java/awt/List/ListGarbageCollectionTest/AwtListGarbageCollec…
Browse files Browse the repository at this point in the history
…tionTest.java failed with "List wasn't garbage collected"

Backport-of: f835aaafc7332d324ca9b08b2a34539fc1c573aa
  • Loading branch information
GoeLin committed Oct 11, 2023
1 parent a387205 commit 92398a6
Showing 1 changed file with 40 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -21,21 +21,28 @@
* questions.
*/

/**
import java.awt.Frame;
import java.awt.List;
import java.lang.ref.PhantomReference;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;

import jdk.test.lib.util.ForceGC;

/*
* @test
* @key headful
* @bug 8040076
* @summary AwtList not garbage collected
* @run main/othervm -Xmx100m AwtListGarbageCollectionTest
* @library /test/lib/
* @build jdk.test.lib.util.ForceGC
* @run main/othervm -Xmx100m -Xlog:gc=debug AwtListGarbageCollectionTest
*/
public class AwtListGarbageCollectionTest {

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.ref.WeakReference;
private static final long ENQUEUE_TIMEOUT = 50;

public class AwtListGarbageCollectionTest {
public static void main(String[] args) {
public static void main(String[] args) throws InterruptedException {
Frame frame = new Frame("List leak test");
try {
test(frame);
Expand All @@ -45,29 +52,32 @@ public static void main(String[] args) {
}

private static void test(Frame frame) {
WeakReference<List> weakListRef = null;
try {
frame.setSize(300, 200);
frame.setVisible(true);
frame.setSize(300, 200);
frame.setVisible(true);

List strongListRef = new List();
frame.add(strongListRef);
strongListRef.setMultipleMode(true);
frame.remove(strongListRef);
weakListRef = new WeakReference<List>(strongListRef);
strongListRef = null;
List strongListRef = new List();
frame.add(strongListRef);
strongListRef.setMultipleMode(true);
frame.remove(strongListRef);

//make out of memory to force gc
String veryLongString = new String(new char[100]);
while (true) {
veryLongString += veryLongString;
}
} catch (OutOfMemoryError e) {
if (weakListRef == null) {
throw new RuntimeException("Weak list ref wasn't created");
} else if (weakListRef.get() != null) {
throw new RuntimeException("List wasn't garbage collected");
}
final ReferenceQueue<List> referenceQueue = new ReferenceQueue<>();
final PhantomReference<List> phantomListRef =
new PhantomReference<>(strongListRef, referenceQueue);
System.out.println("phantomListRef: " + phantomListRef);

strongListRef = null; // Clear the strong reference

System.out.println("Waiting for the reference to be cleared");
if (!ForceGC.wait(() -> phantomListRef == remove(referenceQueue))) {
throw new RuntimeException("List wasn't garbage collected");
}
}

private static Reference<?> remove(ReferenceQueue<?> queue) {
try {
return queue.remove(ENQUEUE_TIMEOUT);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}

1 comment on commit 92398a6

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.