1 /* 2 * Copyright (c) 2012, 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 6396844 27 * @summary Tests memory leak for 20000 files 28 * @author Sergey Malenkov 29 * @library ../../regtesthelpers 30 * @build Util 31 * @run main/othervm/timeout=1000 -mx128m TwentyThousandTest 32 */ 33 34 import sun.java2d.Disposer; 35 import sun.java2d.DisposerRecord; 36 37 import javax.swing.*; 38 import java.awt.event.HierarchyEvent; 39 import java.awt.event.HierarchyListener; 40 import java.io.File; 41 import java.io.FileWriter; 42 43 public class TwentyThousandTest { 44 45 private static final int FILES = 20000; 46 private static final int ATTEMPTS = 20; 47 private static final int INTERVAL = 100; 48 49 private static String tmpDir; 50 51 private static volatile boolean disposerComplete; 52 53 public static void main(String[] args) throws Exception { 54 tmpDir = System.getProperty("java.io.tmpdir"); 55 56 if (tmpDir.length() == 0) { //'java.io.tmpdir' isn't guaranteed to be defined 57 tmpDir = System.getProperty("user.home"); 58 } 59 60 System.out.println("Temp directory: " + tmpDir); 61 62 System.out.println("Creating " + FILES + " files"); 63 64 for (int i = 0; i < FILES; i++) { 65 File file = getTempFile(i); 66 67 FileWriter writer = new FileWriter(file); 68 writer.write("File " + i); 69 writer.close(); 70 } 71 72 for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) { 73 if (laf.getClassName().contains("Motif")) { 74 continue; 75 } 76 77 UIManager.setLookAndFeel(laf.getClassName()); 78 79 System.out.println("Do " + ATTEMPTS + " attempts for " + laf.getClassName()); 80 81 for (int i = 0; i < ATTEMPTS; i++) { 82 System.out.print(i + " "); 83 84 doAttempt(); 85 } 86 87 System.out.println(); 88 } 89 90 System.out.println("Removing " + FILES + " files"); 91 92 for (int i = 0; i < FILES; i++) { 93 getTempFile(i).delete(); 94 } 95 96 System.out.println("Test passed successfully"); 97 } 98 99 private static File getTempFile(int i) { 100 return new File(tmpDir, "temp" + i + ".txt"); 101 } 102 103 private static void doAttempt() throws Exception { 104 SwingUtilities.invokeAndWait(new Runnable() { 105 public void run() { 106 final JFileChooser chooser = new JFileChooser(tmpDir); 107 108 chooser.updateUI(); 109 110 // Postpone JFileChooser closing until it becomes visible 111 chooser.addHierarchyListener(new HierarchyListener() { 112 @Override 113 public void hierarchyChanged(HierarchyEvent e) { 114 if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) { 115 if (chooser.isShowing()) { 116 Thread thread = new Thread(new Runnable() { 117 public void run() { 118 try { 119 Thread.sleep(INTERVAL); 120 121 // Close JFileChooser 122 SwingUtilities.invokeLater(new Runnable() { 123 public void run() { 124 chooser.cancelSelection(); 125 } 126 }); 127 } catch (InterruptedException e) { 128 throw new RuntimeException(e); 129 } 130 } 131 }); 132 133 thread.start(); 134 } 135 } 136 } 137 }); 138 139 chooser.showOpenDialog(null); 140 } 141 }); 142 143 DisposerRecord disposerRecord = new DisposerRecord() { 144 public void dispose() { 145 disposerComplete = true; 146 } 147 }; 148 149 disposerComplete = false; 150 151 Disposer.addRecord(new Object(), disposerRecord); 152 153 while (!disposerComplete) { 154 Util.generateOOME(); 155 } 156 } 157 }