Path: blob/master/test/jdk/sun/awt/shell/ShellFolderMemoryLeak.java
41149 views
/*1* Copyright (c) 2015, 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 803009926* @summary Memory usage of java process increases27after calling Win32ShellFolder:listFiles28multiple times on some directory with29large number of files/folders30* @modules java.desktop/sun.awt.shell31* @requires (os.family == "windows")32* @run main/timeout=1000 ShellFolderMemoryLeak33*/34import java.io.BufferedReader;35import java.io.File;36import java.io.FileNotFoundException;37import java.io.IOException;38import java.io.InputStream;39import java.io.InputStreamReader;40import java.util.logging.Level;41import java.util.logging.Logger;42import sun.awt.shell.ShellFolder;4344public class ShellFolderMemoryLeak {4546private final static String tempDir = System.getProperty("java.io.tmpdir");47private static Process process;48public static void main(String[] args) throws Exception {49if (args.length == 0) {50boolean testResultParallel51= createChildProcessWithParallelCollector();52String result = "";53if (!testResultParallel) {54result = "Test failed with Parallel collector";55}56boolean testResultDefault57= createChildProcessWithDefaultCollector();58if (!testResultDefault && !testResultParallel) {59result += " and with default collector both.";60} else if (!testResultDefault) {61result = "Test failed with default collector";62}63if (!"".equals(result)) {64throw new RuntimeException(result);65}66} else {67testListFile(args[args.length - 1]);68}69}7071public static boolean createChildProcessWithDefaultCollector()72throws Exception {73String testDirectory = "TestDirectory1";74testDirectory = tempDir + testDirectory +File.separator;75createTestData(testDirectory);76return runProcess("", testDirectory);77}7879public static boolean createChildProcessWithParallelCollector()80throws Exception {81String testDirectory = "TestDirectory2";82testDirectory = tempDir + testDirectory +File.separator;83createTestData(testDirectory);84return runProcess(" -XX:+UseParallelGC", testDirectory);85}8687public static boolean runProcess(String arg1, String arg2) throws Exception {88String javaPath = System.getProperty("java.home");89String classPathDir = System.getProperty("java.class.path");9091//creating java process which run same class with different Xmx value92String command = javaPath + File.separator + "bin" + File.separator93+ "java -Xmx256M" + arg1 + " -cp "94+ classPathDir95+ " --add-exports=java.desktop/sun.awt.shell=ALL-UNNAMED"96+ " ShellFolderMemoryLeak " + arg2;97process = Runtime.getRuntime().exec(command);98BufferedReader input = null;99InputStream errorStream = null;100String line = null;101try {102int exitVal = process.waitFor();103input = new BufferedReader(new InputStreamReader(104process.getInputStream()));105while ((line = input.readLine()) != null) {106}107errorStream = process.getErrorStream();108if (checkExceptions(errorStream) || exitVal != 0) {109return false;110}111} catch (IllegalThreadStateException e) {112throw new RuntimeException(e);113} finally {114if (input != null) {115input.close();116}117if (errorStream != null) {118errorStream.close();119}120process.destroy();121}122return true;123}124125public static boolean checkExceptions(InputStream in) throws IOException {126String tempString;127int count = in.available();128boolean exception = false;129while (count > 0) {130byte[] b = new byte[count];131in.read(b);132tempString = new String(b);133if (!exception) {134exception = tempString.contains("RunTimeException");135}136count = in.available();137}138return exception;139}140141private static void createTestData(String testDirectory) {142String folder = "folder_";143File testFolder = new File(testDirectory);144if (testFolder.exists()) {145clearTestData(testDirectory);146} else {147if (testFolder.mkdir()) {148for (int inx = 0; inx < 100; inx++) {149new File(testFolder + File.separator + folder + inx).mkdir();150}151} else {152throw new RuntimeException("Failed to create testDirectory");153}154}155}156157public static void deleteDirectory(File file)158throws IOException {159160if (file.isDirectory()) {161if (file.list().length == 0) {162file.delete();163} else {164String files[] = file.list();165for (String temp : files) {166File fileDelete = new File(file, temp);167deleteDirectory(fileDelete);168}169if (file.list().length == 0) {170file.delete();171}172}173}174}175176private static void testListFile(String testDirectory) {177try {178int mb = 1024 * 1024;179ShellFolder folder = ShellFolder.getShellFolder(180new File(testDirectory));181Runtime instance = Runtime.getRuntime();182183//Memory used before calling listFiles184long startmem = instance.totalMemory() - instance.freeMemory();185long start = System.currentTimeMillis();186long endmem = 0;187188//Calling listFiles for 5 minutes with sleep of 10 ms.189while ((System.currentTimeMillis() - start) < 300000) {190try {191folder.listFiles();192Thread.sleep(10);193endmem = instance.totalMemory() - instance.freeMemory();194} catch (InterruptedException ex) {195Logger.getLogger(ShellFolderMemoryLeak.class.getName())196.log(Level.SEVERE, "InterruptedException", ex);197}198}199200//Total increase in memory after 5 minutes201long result = (endmem - startmem) / mb;202203if (result > 100) {204clearTestData(testDirectory);205throw new RuntimeException("Test Failed");206}207clearTestData(testDirectory);208} catch (FileNotFoundException ex) {209if(process != null && process.isAlive()) {210process.destroy();211}212Logger.getLogger(ShellFolderMemoryLeak.class.getName())213.log(Level.SEVERE, "File Not Found Exception", ex);214}215}216217private static void clearTestData(String testDirectory) {218File testFolder = new File(testDirectory);219try {220deleteDirectory(testFolder);221} catch (IOException ex) {222Logger.getLogger(ShellFolderMemoryLeak.class.getName())223.log(Level.SEVERE, "Unable to delete files", ex);224}225}226}227228229