8204963: javax.swing.border.TitledBorder has a memory leak
authorpsadhukhan
Thu, 09 Aug 2018 11:01:59 +0530
changeset 51905 cd7d2f9154fd
parent 51904 5ade5b3a227e
child 51906 a2f64e4e75ca
8204963: javax.swing.border.TitledBorder has a memory leak Reviewed-by: serb, kaddepalli
src/java.desktop/share/classes/javax/swing/border/TitledBorder.java
test/jdk/javax/swing/border/TestTitledBorderLeak.java
--- a/src/java.desktop/share/classes/javax/swing/border/TitledBorder.java	Wed Aug 08 18:31:24 2018 -0700
+++ b/src/java.desktop/share/classes/javax/swing/border/TitledBorder.java	Thu Aug 09 11:01:59 2018 +0530
@@ -42,6 +42,8 @@
 import javax.swing.UIManager;
 import javax.swing.plaf.basic.BasicHTML;
 
+import jdk.internal.ref.CleanerFactory;
+
 /**
  * A class which implements an arbitrary border
  * with the addition of a String title in a
@@ -759,22 +761,19 @@
 
     private void installPropertyChangeListeners() {
         final WeakReference<TitledBorder> weakReference = new WeakReference<TitledBorder>(this);
-        final PropertyChangeListener listener = new PropertyChangeListener() {
-            @Override
-            public void propertyChange(PropertyChangeEvent evt) {
-                if (weakReference.get() == null) {
-                    UIManager.removePropertyChangeListener(this);
-                    UIManager.getDefaults().removePropertyChangeListener(this);
-                } else {
-                    String prop = evt.getPropertyName();
-                    if ("lookAndFeel".equals(prop) || "LabelUI".equals(prop)) {
-                        label.updateUI();
-                    }
-                }
+        final PropertyChangeListener listener = evt -> {
+            TitledBorder tb = weakReference.get();
+            String prop = evt.getPropertyName();
+            if (tb != null && ("lookAndFeel".equals(prop) || "LabelUI".equals(prop))) {
+                tb.label.updateUI();
             }
         };
 
         UIManager.addPropertyChangeListener(listener);
         UIManager.getDefaults().addPropertyChangeListener(listener);
+        CleanerFactory.cleaner().register(this, () -> {
+            UIManager.removePropertyChangeListener(listener);
+            UIManager.getDefaults().removePropertyChangeListener(listener);
+        });
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/swing/border/TestTitledBorderLeak.java	Thu Aug 09 11:01:59 2018 +0530
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2018, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.lang.ref.WeakReference;
+import java.util.ArrayList;
+import javax.swing.border.TitledBorder;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+import javax.swing.JLabel;
+import javax.swing.SwingUtilities;
+
+/**
+ * @test
+ * @bug 8204963
+ * @summary Verifies TitledBorder's memory leak
+ * @run main TestTitledBorderLeak
+ */
+
+public class TestTitledBorderLeak {
+
+    final static int TOTAL_TITLEDBORDER = 10;
+    final static int GC_ATTEMPTS = 10;
+    static ArrayList<WeakReference<TitledBorder>> weakRefArrTB =
+                                         new ArrayList(TOTAL_TITLEDBORDER);
+
+    public static void main(String[] args) throws Exception {
+
+        JFrame frame[] = new JFrame[TOTAL_TITLEDBORDER];
+
+        SwingUtilities.invokeAndWait(() -> {
+            for (int i = 0; i < TOTAL_TITLEDBORDER; i++) {
+                TitledBorder tb = new TitledBorder("");
+                weakRefArrTB.add(i, new WeakReference<TitledBorder>(tb));
+                JLabel label = new JLabel("TitledBorder");
+                label.setBorder(tb);
+                frame[i] = new JFrame("Borders");
+                JPanel panel = new JPanel();
+                panel.add(label);
+                frame[i].setContentPane(panel);
+                frame[i].setVisible(true);
+
+            }
+        });
+        if (TOTAL_TITLEDBORDER != weakRefArrTB.size()) {
+            System.err.println("TOTAL_TITLEDBORDER != weakRefArrTB.size()");
+        }
+        Thread.sleep(3000);
+        SwingUtilities.invokeAndWait(() -> {
+            for (int i = 0; i < TOTAL_TITLEDBORDER; i++) {
+                frame[i].dispose();
+                frame[i] = null;
+            }
+        });
+        Thread.sleep(3000);
+        attemptGCTitledBorder();
+        if (TOTAL_TITLEDBORDER != getCleanedUpTitledBorderCount()) {
+            throw new RuntimeException("Expected Total TitledBorder to be freed : " + TOTAL_TITLEDBORDER +
+                           " Freed " + getCleanedUpTitledBorderCount());
+        }
+        System.out.println("OK");
+    }
+
+    private static void attemptGCTitledBorder() {
+        // Attempt gc GC_ATTEMPTS times
+        for (int i = 0; i < GC_ATTEMPTS; i++) {
+            System.gc();
+            System.runFinalization();
+            if (getCleanedUpTitledBorderCount() == TOTAL_TITLEDBORDER) {
+                break;
+            }
+            try {
+                Thread.sleep(500);
+            } catch (InterruptedException e) {
+                System.err.println("InterruptedException occurred during Thread.sleep()");
+            }
+        }
+    }
+
+    private static int getCleanedUpTitledBorderCount() {
+        int count = 0;
+        for (WeakReference<TitledBorder> ref : weakRefArrTB) {
+            if (ref.get() == null) {
+                count++;
+            }
+        }
+        return count;
+    }
+}