8300727: java/awt/List/ListGarbageCollectionTest/AwtListGarbageCollectionTest.java failed with "List wasn't garbage collected"
Reviewed-by: prr, tr, serb
This commit is contained in:
parent
466ffebcae
commit
f835aaafc7
@ -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
|
||||
@ -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
|
||||
*/
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
public class AwtListGarbageCollectionTest {
|
||||
public static void main(String[] args) {
|
||||
|
||||
private static final long ENQUEUE_TIMEOUT = 50;
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
Frame frame = new Frame("List leak test");
|
||||
try {
|
||||
test(frame);
|
||||
@ -45,29 +52,32 @@ public class AwtListGarbageCollectionTest {
|
||||
}
|
||||
|
||||
private static void test(Frame frame) {
|
||||
WeakReference<List> weakListRef = null;
|
||||
frame.setSize(300, 200);
|
||||
frame.setVisible(true);
|
||||
|
||||
List strongListRef = new List();
|
||||
frame.add(strongListRef);
|
||||
strongListRef.setMultipleMode(true);
|
||||
frame.remove(strongListRef);
|
||||
|
||||
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 {
|
||||
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;
|
||||
|
||||
//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");
|
||||
}
|
||||
return queue.remove(ENQUEUE_TIMEOUT);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user