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