|
1 | 1 | /* |
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. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
21 | 21 | * questions. |
22 | 22 | */ |
23 | 23 |
|
24 | | -import java.lang.ref.WeakReference; |
25 | | -import java.util.ArrayList; |
26 | 24 | 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; |
31 | 25 |
|
32 | | -/** |
| 26 | +/* |
33 | 27 | * @test |
34 | | - * @key headful |
35 | 28 | * @bug 8204963 |
36 | 29 | * @summary Verifies TitledBorder's memory leak |
37 | | - * @run main TestTitledBorderLeak |
| 30 | + * @run main/othervm -Xmx10M TestTitledBorderLeak |
38 | 31 | */ |
39 | | - |
40 | 32 | 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 | | - |
47 | 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 | + } |
48 | 48 |
|
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 | + } |
73 | 50 | } |
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 |
87 | 53 | 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"); |
97 | 55 | } |
| 56 | + System.out.println("Passed"); |
98 | 57 | } |
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; |
108 | 63 | } |
109 | 64 | } |
| 65 | + |
0 commit comments