Path: blob/master/test/jdk/sun/awt/shell/FileSystemViewMemoryLeak.java
41152 views
/*1* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 817501526* @summary FileSystemView.isDrive(File) memory leak on "C:\" file reference27* @modules java.desktop/sun.awt.shell28* @requires (os.family == "windows")29* @run main/othervm/timeout=320 -Xmx8m FileSystemViewMemoryLeak30*/31import java.io.File;32import java.text.NumberFormat;33import java.util.concurrent.TimeUnit;3435import javax.swing.filechooser.FileSystemView;3637public class FileSystemViewMemoryLeak {3839public static void main(String[] args) {40test();41}4243// Will run the test no more than 300 seconds44static long endtime = System.nanoTime() + TimeUnit.SECONDS.toNanos(300);4546private static void test() {4748File root = new File("C:\\");49System.out.println("Root Exists: " + root.exists());50System.out.println("Root Absolute Path: " + root.getAbsolutePath());51System.out.println("Root Is Directory?: " + root.isDirectory());5253FileSystemView fileSystemView = FileSystemView.getFileSystemView();54NumberFormat nf = NumberFormat.getNumberInstance();5556int iMax = 50000;57long lastPercentFinished = 0L;58for (int i = 0; i < iMax; i++) {59if (isComplete()) {60System.out.println("Time is over");61return;62}6364long percentFinished = Math.round(((i * 1000d) / (double) iMax));6566if (lastPercentFinished != percentFinished) {67double pf = ((double) percentFinished) / 10d;68String pfMessage = String.valueOf(pf) + " % (" + i + "/" + iMax + ")";6970long totalMemory = Runtime.getRuntime().totalMemory() / 1024;71long freeMemory = Runtime.getRuntime().freeMemory() / 1024;72long maxMemory = Runtime.getRuntime().maxMemory() / 1024;73String memMessage = "[Memory Used: " + nf.format(totalMemory) +74" kb Free=" + nf.format(freeMemory) +75" kb Max: " + nf.format(maxMemory) + " kb]";7677System.out.println(pfMessage + " " + memMessage);78lastPercentFinished = percentFinished;79}8081boolean floppyDrive = fileSystemView.isFloppyDrive(root);82boolean computerNode = fileSystemView.isComputerNode(root);8384// "isDrive()" seems to be the painful method...85boolean drive = fileSystemView.isDrive(root);86}87}8889private static boolean isComplete() {90return endtime - System.nanoTime() < 0;91}92}93949596