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