Skip to content

Commit f93eed2

Browse files
Masanori YanoPaul Hohensee
authored andcommitted
8213531: Test javax/swing/border/TestTitledBorderLeak.java fails
Reviewed-by: phh Backport-of: 07d24509a68cc06f062b8249f207bc0d2148c79b
1 parent efe27f2 commit f93eed2

File tree

2 files changed

+28
-73
lines changed

2 files changed

+28
-73
lines changed

test/jdk/ProblemList.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,6 @@ javax/sound/midi/Sequencer/Looping.java 8136897 generic-all
701701

702702
javax/swing/plaf/basic/BasicTextUI/8001470/bug8001470.java 8233177 linux-all,windows-all
703703

704-
javax/swing/border/TestTitledBorderLeak.java 8213531 linux-all
705704
javax/swing/JComponent/6683775/bug6683775.java 8172337 generic-all
706705
javax/swing/JWindow/ShapedAndTranslucentWindows/ShapedTranslucentPerPixelTranslucentGradient.java 8233582 linux-all
707706
javax/swing/JWindow/ShapedAndTranslucentWindows/ShapedPerPixelTranslucentGradient.java 8233582 linux-all
Lines changed: 28 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, 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,89 +21,45 @@
2121
* questions.
2222
*/
2323

24-
import java.lang.ref.WeakReference;
25-
import java.util.ArrayList;
2624
import javax.swing.border.TitledBorder;
27-
import javax.swing.JFrame;
28-
import javax.swing.JPanel;
29-
import javax.swing.JLabel;
30-
import javax.swing.SwingUtilities;
3125

32-
/**
26+
/*
3327
* @test
34-
* @key headful
3528
* @bug 8204963
3629
* @summary Verifies TitledBorder's memory leak
37-
* @run main TestTitledBorderLeak
30+
* @run main/othervm -Xmx10M TestTitledBorderLeak
3831
*/
39-
4032
public class TestTitledBorderLeak {
41-
42-
final static int TOTAL_TITLEDBORDER = 10;
43-
final static int GC_ATTEMPTS = 10;
44-
static ArrayList<WeakReference<TitledBorder>> weakRefArrTB =
45-
new ArrayList(TOTAL_TITLEDBORDER);
46-
4733
public static void main(String[] args) throws Exception {
34+
int max = 100000;
35+
long initialFreeMemory = 0L;
36+
long currentFreeMemory;
37+
try {
38+
for (int i = 1; i <= max; i++) {
39+
new TitledBorder("");
40+
if ((i % 1000) == 0) {
41+
System.gc();
42+
currentFreeMemory = dumpMemoryStatus("After " + i);
43+
if(initialFreeMemory == 0L) {
44+
initialFreeMemory = currentFreeMemory;
45+
} else if( currentFreeMemory < initialFreeMemory/2) {
46+
throw new RuntimeException("Memory halved: there's a leak");
47+
}
4848

49-
JFrame frame[] = new JFrame[TOTAL_TITLEDBORDER];
50-
51-
SwingUtilities.invokeAndWait(() -> {
52-
for (int i = 0; i < TOTAL_TITLEDBORDER; i++) {
53-
TitledBorder tb = new TitledBorder("");
54-
weakRefArrTB.add(i, new WeakReference<TitledBorder>(tb));
55-
JLabel label = new JLabel("TitledBorder");
56-
label.setBorder(tb);
57-
frame[i] = new JFrame("Borders");
58-
JPanel panel = new JPanel();
59-
panel.add(label);
60-
frame[i].setContentPane(panel);
61-
frame[i].setVisible(true);
62-
63-
}
64-
});
65-
if (TOTAL_TITLEDBORDER != weakRefArrTB.size()) {
66-
System.err.println("TOTAL_TITLEDBORDER != weakRefArrTB.size()");
67-
}
68-
Thread.sleep(3000);
69-
SwingUtilities.invokeAndWait(() -> {
70-
for (int i = 0; i < TOTAL_TITLEDBORDER; i++) {
71-
frame[i].dispose();
72-
frame[i] = null;
49+
}
7350
}
74-
});
75-
Thread.sleep(3000);
76-
attemptGCTitledBorder();
77-
if (TOTAL_TITLEDBORDER != getCleanedUpTitledBorderCount()) {
78-
throw new RuntimeException("Expected Total TitledBorder to be freed : " + TOTAL_TITLEDBORDER +
79-
" Freed " + getCleanedUpTitledBorderCount());
80-
}
81-
System.out.println("OK");
82-
}
83-
84-
private static void attemptGCTitledBorder() {
85-
// Attempt gc GC_ATTEMPTS times
86-
for (int i = 0; i < GC_ATTEMPTS; i++) {
51+
}catch(OutOfMemoryError e) {
52+
// Don't think it would work; should not happen
8753
System.gc();
88-
System.runFinalization();
89-
if (getCleanedUpTitledBorderCount() == TOTAL_TITLEDBORDER) {
90-
break;
91-
}
92-
try {
93-
Thread.sleep(500);
94-
} catch (InterruptedException e) {
95-
System.err.println("InterruptedException occurred during Thread.sleep()");
96-
}
54+
throw new RuntimeException("There was OOM");
9755
}
56+
System.out.println("Passed");
9857
}
99-
100-
private static int getCleanedUpTitledBorderCount() {
101-
int count = 0;
102-
for (WeakReference<TitledBorder> ref : weakRefArrTB) {
103-
if (ref.get() == null) {
104-
count++;
105-
}
106-
}
107-
return count;
58+
private static long dumpMemoryStatus(String msg) {
59+
Runtime rt = Runtime.getRuntime();
60+
long freeMem = rt.freeMemory();
61+
System.out.println(msg + ": " + freeMem + " free");
62+
return freeMem;
10863
}
10964
}
65+

0 commit comments

Comments
 (0)