8175968: The javax.swing.filechooser.FileSystemView constructor consumes memory by adding a PropertyChangeListener that is never removed
authorserb
Fri, 22 Sep 2017 17:36:55 -0700
changeset 47368 147cb4cf2556
parent 47367 7e8a1abb5168
child 47369 a28c513d1c4e
8175968: The javax.swing.filechooser.FileSystemView constructor consumes memory by adding a PropertyChangeListener that is never removed Reviewed-by: azvegint
src/java.desktop/share/classes/javax/swing/filechooser/FileSystemView.java
test/jdk/javax/swing/JFileChooser/FileSystemView/FileSystemViewListenerLeak.java
--- a/src/java.desktop/share/classes/javax/swing/filechooser/FileSystemView.java	Fri Sep 22 17:09:56 2017 -0700
+++ b/src/java.desktop/share/classes/javax/swing/filechooser/FileSystemView.java	Fri Sep 22 17:36:55 2017 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2017, 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
@@ -25,23 +25,25 @@
 
 package javax.swing.filechooser;
 
-
-import javax.swing.*;
-
 import java.awt.Image;
+import java.beans.PropertyChangeListener;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.text.MessageFormat;
-import java.util.List;
-import java.util.ArrayList;
 import java.lang.ref.WeakReference;
-import java.beans.PropertyChangeListener;
-import java.beans.PropertyChangeEvent;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.List;
 
-import sun.awt.shell.*;
+import javax.swing.Icon;
+import javax.swing.ImageIcon;
+import javax.swing.JFileChooser;
+import javax.swing.UIManager;
+
+import jdk.internal.ref.CleanerFactory;
+import sun.awt.shell.ShellFolder;
 
 /**
  * FileSystemView is JFileChooser's gateway to the
@@ -109,22 +111,19 @@
      * Constructs a FileSystemView.
      */
     public FileSystemView() {
-        final WeakReference<FileSystemView> weakReference = new WeakReference<FileSystemView>(this);
-
-        UIManager.addPropertyChangeListener(new PropertyChangeListener() {
-            public void propertyChange(PropertyChangeEvent evt) {
-                FileSystemView fileSystemView = weakReference.get();
+        final WeakReference<FileSystemView> weakReference = new WeakReference<>(this);
+        final PropertyChangeListener pcl = evt -> {
+            final FileSystemView fsv = weakReference.get();
+            if (fsv != null && evt.getPropertyName().equals("lookAndFeel")) {
+                fsv.useSystemExtensionHiding =
+                        UIManager.getDefaults().getBoolean(
+                                "FileChooser.useSystemExtensionHiding");
+            }
+        };
 
-                if (fileSystemView == null) {
-                    // FileSystemView was destroyed
-                    UIManager.removePropertyChangeListener(this);
-                } else {
-                    if (evt.getPropertyName().equals("lookAndFeel")) {
-                        fileSystemView.useSystemExtensionHiding =
-                                UIManager.getDefaults().getBoolean("FileChooser.useSystemExtensionHiding");
-                    }
-                }
-            }
+        UIManager.addPropertyChangeListener(pcl);
+        CleanerFactory.cleaner().register(this, () -> {
+            UIManager.removePropertyChangeListener(pcl);
         });
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/swing/JFileChooser/FileSystemView/FileSystemViewListenerLeak.java	Fri Sep 22 17:36:55 2017 -0700
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2017, 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.io.File;
+import java.io.IOException;
+import java.util.concurrent.TimeUnit;
+
+import javax.swing.UIManager;
+import javax.swing.filechooser.FileSystemView;
+
+/**
+ * @test
+ * @bug 8175968
+ * @library /javax/swing/regtesthelpers
+ * @build Util
+ * @run main/othervm -Xmx8m -Djava.awt.headless=true FileSystemViewListenerLeak
+*/
+public final class FileSystemViewListenerLeak {
+
+    public static void main(final String[] args) {
+        checkListenersCount();
+        test();
+        Util.generateOOME();
+        checkListenersCount();
+    }
+
+    private static void test() {
+        // Will run the test no more than 30 seconds
+        long endtime = System.nanoTime() + TimeUnit.SECONDS.toNanos(30);
+        while (!(endtime - System.nanoTime() < 0)) {
+            new CustomFileSystemView();
+        }
+    }
+
+    private static void checkListenersCount() {
+        int length = UIManager.getPropertyChangeListeners().length;
+        if (length != 0) {
+            throw new RuntimeException("The count of listeners is: " + length);
+        }
+    }
+
+    private static final class CustomFileSystemView extends FileSystemView {
+
+        public File createNewFolder(File containingDir) throws IOException {
+            return null;
+        }
+    }
+}