changeset 17818:9bae8d7e5b8a

8175015: FileSystemView.isDrive(File) memory leak on "C:\" file reference Reviewed-by: serb, psadhukhan Contributed-by: krishna.addepalli@oracle.com
author pkbalakr
date Wed, 30 Aug 2017 16:56:59 +0530
parents 77d046dced5d
children 892a8e0b5834
files src/java.desktop/windows/classes/sun/awt/shell/Win32ShellFolderManager2.java test/sun/awt/shell/FileSystemViewMemoryLeak.java
diffstat 2 files changed, 92 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.desktop/windows/classes/sun/awt/shell/Win32ShellFolderManager2.java	Tue Aug 29 10:47:16 2017 -0700
+++ b/src/java.desktop/windows/classes/sun/awt/shell/Win32ShellFolderManager2.java	Wed Aug 30 16:56:59 2017 +0530
@@ -443,17 +443,12 @@
     public boolean isFileSystemRoot(File dir) {
         //Note: Removable drives don't "exist" but are listed in "My Computer"
         if (dir != null) {
-            Win32ShellFolder2 drives = getDrives();
+
             if (dir instanceof Win32ShellFolder2) {
                 Win32ShellFolder2 sf = (Win32ShellFolder2)dir;
-                if (sf.isFileSystem()) {
-                    if (sf.parent != null) {
-                        return sf.parent.equals(drives);
-                    }
-                    // else fall through ...
-                } else {
-                    return false;
-                }
+
+                return (sf.isFileSystem() && sf.parent != null &&
+                        sf.parent.equals(Win32ShellFolder2.listRoots()));
             }
             String path = dir.getPath();
 
@@ -461,9 +456,9 @@
                 return false;
             }
 
-            File[] files = drives.listFiles();
+            File[] roots = Win32ShellFolder2.listRoots();
 
-            return files != null && Arrays.asList(files).contains(dir);
+            return roots != null && Arrays.asList(roots).contains(dir);
         }
         return false;
     }
@@ -561,6 +556,11 @@
                         ThreadGroupUtils.getRootThreadGroup(), comRun, name,
                         0, false);
                 thread.setDaemon(true);
+                /* This is important, since this thread running at lower priority
+                   leads to memory consumption when listDrives() function is called
+                   repeatedly.
+                 */
+                thread.setPriority(Thread.MAX_PRIORITY);
                 return thread;
             });
             return comThread;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/sun/awt/shell/FileSystemViewMemoryLeak.java	Wed Aug 30 16:56:59 2017 +0530
@@ -0,0 +1,81 @@
+/*
+ * 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.
+ */
+
+/*
+ * @test
+ * @bug 8175015
+ * @summary FileSystemView.isDrive(File) memory leak on "C:\" file reference
+ * @modules java.desktop/sun.awt.shell
+ * @requires (os.family == "windows")
+ * @run main/othervm  -Xmx8m FileSystemViewMemoryLeak
+ */
+import java.io.File;
+import java.text.NumberFormat;
+import javax.swing.filechooser.FileSystemView;
+
+public class FileSystemViewMemoryLeak {
+
+    public static void main(String[] args) {
+        test();
+    }
+
+    private static void test() {
+
+        File root = new File("C:\\");
+        System.out.println("Root Exists: " + root.exists());
+        System.out.println("Root Absolute Path: " + root.getAbsolutePath());
+        System.out.println("Root Is Directory?: " + root.isDirectory());
+
+        FileSystemView fileSystemView = FileSystemView.getFileSystemView();
+        NumberFormat nf = NumberFormat.getNumberInstance();
+
+        int iMax = 50000;
+        long lastPercentFinished = 0L;
+        for (int i = 0; i < iMax; i++) {
+
+            long percentFinished = Math.round(((i * 1000d) / (double) iMax));
+
+            if (lastPercentFinished != percentFinished) {
+                double pf = ((double) percentFinished) / 10d;
+                String pfMessage = String.valueOf(pf) + " % (" + i + "/" + iMax + ")";
+
+                long totalMemory = Runtime.getRuntime().totalMemory() / 1024;
+                long freeMemory = Runtime.getRuntime().freeMemory() / 1024;
+                long maxMemory = Runtime.getRuntime().maxMemory() / 1024;
+                String memMessage = "[Memory Used: " + nf.format(totalMemory) +
+                                    " kb Free=" + nf.format(freeMemory) +
+                                    " kb Max: " + nf.format(maxMemory) + " kb]";
+
+                System.out.println(pfMessage + " " + memMessage);
+                lastPercentFinished = percentFinished;
+            }
+
+            boolean floppyDrive = fileSystemView.isFloppyDrive(root);
+            boolean computerNode = fileSystemView.isComputerNode(root);
+
+            // "isDrive()" seems to be the painful method...
+            boolean drive = fileSystemView.isDrive(root);
+        }
+    }
+}
+