1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8175015
  27  * @summary FileSystemView.isDrive(File) memory leak on "C:\" file reference
  28  * @modules java.desktop/sun.awt.shell
  29  * @requires (os.family == "windows")
  30  * @run main/othervm  -Xmx8m FileSystemViewMemoryLeak
  31  */
  32 import java.io.File;
  33 import java.text.NumberFormat;
  34 import javax.swing.filechooser.FileSystemView;
  35 
  36 public class FileSystemViewMemoryLeak {
  37 
  38     public static void main(String[] args) {
  39         test();
  40     }
  41 
  42     private static void test() {
  43 
  44         File root = new File("C:\\");
  45         System.out.println("Root Exists: " + root.exists());
  46         System.out.println("Root Absolute Path: " + root.getAbsolutePath());
  47         System.out.println("Root Is Directory?: " + root.isDirectory());
  48 
  49         FileSystemView fileSystemView = FileSystemView.getFileSystemView();
  50         NumberFormat nf = NumberFormat.getNumberInstance();
  51 
  52         int iMax = 50000;
  53         long lastPercentFinished = 0L;
  54         for (int i = 0; i < iMax; i++) {
  55 
  56             long percentFinished = Math.round(((i * 1000d) / (double) iMax));
  57 
  58             if (lastPercentFinished != percentFinished) {
  59                 double pf = ((double) percentFinished) / 10d;
  60                 String pfMessage = String.valueOf(pf) + " % (" + i + "/" + iMax + ")";
  61 
  62                 long totalMemory = Runtime.getRuntime().totalMemory() / 1024;
  63                 long freeMemory = Runtime.getRuntime().freeMemory() / 1024;
  64                 long maxMemory = Runtime.getRuntime().maxMemory() / 1024;
  65                 String memMessage = "[Memory Used: " + nf.format(totalMemory) +
  66                                     " kb Free=" + nf.format(freeMemory) +
  67                                     " kb Max: " + nf.format(maxMemory) + " kb]";
  68 
  69                 System.out.println(pfMessage + " " + memMessage);
  70                 lastPercentFinished = percentFinished;
  71             }
  72 
  73             boolean floppyDrive = fileSystemView.isFloppyDrive(root);
  74             boolean computerNode = fileSystemView.isComputerNode(root);
  75 
  76             // "isDrive()" seems to be the painful method...
  77             boolean drive = fileSystemView.isDrive(root);
  78         }
  79     }
  80 }
  81