From e5566ace66e346b5337b80f4f1035670971ed3e3 Mon Sep 17 00:00:00 2001 From: Sergey Bylokhov Date: Fri, 22 Sep 2017 17:36:55 -0700 Subject: [PATCH] 8175968: The javax.swing.filechooser.FileSystemView constructor consumes memory by adding a PropertyChangeListener that is never removed Reviewed-by: azvegint --- .../swing/filechooser/FileSystemView.java | 49 +++++++------ .../FileSystemViewListenerLeak.java | 68 +++++++++++++++++++ 2 files changed, 92 insertions(+), 25 deletions(-) create mode 100644 test/jdk/javax/swing/JFileChooser/FileSystemView/FileSystemViewListenerLeak.java diff --git a/src/java.desktop/share/classes/javax/swing/filechooser/FileSystemView.java b/src/java.desktop/share/classes/javax/swing/filechooser/FileSystemView.java index e573cf6f6af..328a9e7687e 100644 --- a/src/java.desktop/share/classes/javax/swing/filechooser/FileSystemView.java +++ b/src/java.desktop/share/classes/javax/swing/filechooser/FileSystemView.java @@ -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 @@ public abstract class FileSystemView { * Constructs a FileSystemView. */ public FileSystemView() { - final WeakReference weakReference = new WeakReference(this); - - UIManager.addPropertyChangeListener(new PropertyChangeListener() { - public void propertyChange(PropertyChangeEvent evt) { - FileSystemView fileSystemView = weakReference.get(); - - if (fileSystemView == null) { - // FileSystemView was destroyed - UIManager.removePropertyChangeListener(this); - } else { - if (evt.getPropertyName().equals("lookAndFeel")) { - fileSystemView.useSystemExtensionHiding = - UIManager.getDefaults().getBoolean("FileChooser.useSystemExtensionHiding"); - } - } + final WeakReference 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"); } + }; + + UIManager.addPropertyChangeListener(pcl); + CleanerFactory.cleaner().register(this, () -> { + UIManager.removePropertyChangeListener(pcl); }); } diff --git a/test/jdk/javax/swing/JFileChooser/FileSystemView/FileSystemViewListenerLeak.java b/test/jdk/javax/swing/JFileChooser/FileSystemView/FileSystemViewListenerLeak.java new file mode 100644 index 00000000000..120c9767d44 --- /dev/null +++ b/test/jdk/javax/swing/JFileChooser/FileSystemView/FileSystemViewListenerLeak.java @@ -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; + } + } +}