|
21 | 21 | * questions. |
22 | 22 | */ |
23 | 23 |
|
| 24 | +import java.lang.ref.Reference; |
| 25 | +import java.lang.ref.WeakReference; |
| 26 | + |
24 | 27 | import javax.swing.border.TitledBorder; |
25 | 28 |
|
26 | | -/* |
| 29 | +/** |
27 | 30 | * @test |
28 | 31 | * @bug 8204963 |
29 | 32 | * @summary Verifies TitledBorder's memory leak |
30 | | - * @run main/othervm -Xmx10M TestTitledBorderLeak |
| 33 | + * @library /javax/swing/regtesthelpers |
| 34 | + * @build Util |
| 35 | + * @run main/timeout=60/othervm -mx32m TestTitledBorderLeak |
31 | 36 | */ |
32 | | -public class TestTitledBorderLeak { |
33 | | - 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 | | - } |
| 37 | +public final class TestTitledBorderLeak { |
48 | 38 |
|
49 | | - } |
50 | | - } |
51 | | - }catch(OutOfMemoryError e) { |
52 | | - // Don't think it would work; should not happen |
53 | | - System.gc(); |
54 | | - throw new RuntimeException("There was OOM"); |
| 39 | + public static void main(String[] args) throws Exception { |
| 40 | + Reference<TitledBorder> border = getTitleBorder(); |
| 41 | + int attempt = 0; |
| 42 | + while (border.get() != null) { |
| 43 | + Util.generateOOME(); |
| 44 | + System.out.println("Not freed, attempt: " + attempt++); |
55 | 45 | } |
56 | | - System.out.println("Passed"); |
57 | 46 | } |
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; |
| 47 | + |
| 48 | + private static Reference<TitledBorder> getTitleBorder() { |
| 49 | + TitledBorder tb = new TitledBorder(""); |
| 50 | + return new WeakReference<>(tb); |
63 | 51 | } |
64 | 52 | } |
65 | | - |
0 commit comments