1 /* 2 * Copyright (c) 2009, 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 /* @test %W% %E% 25 @bug 6798062 26 @summary Memory Leak on using getFiles of FileSystemView 27 @author Pavel Porvatov 28 @modules java.desktop/sun.awt 29 java.desktop/sun.awt.shell 30 @run applet/manual=done bug6798062.html 31 */ 32 33 import sun.awt.OSInfo; 34 import sun.awt.shell.ShellFolder; 35 36 import javax.swing.*; 37 import java.awt.event.ActionEvent; 38 import java.awt.event.ActionListener; 39 import java.awt.*; 40 import java.io.File; 41 import java.io.FileNotFoundException; 42 43 public class bug6798062 extends JApplet { 44 45 private final JSlider slider = new JSlider(0, 100); 46 47 private final JTextField tfLink = new JTextField(); 48 49 private final JButton btnStart = new JButton("Start"); 50 51 private final JButton btnStop = new JButton("Stop"); 52 53 private final JButton btnGC = new JButton("Run System.gc()"); 54 55 private ShellFolder folder; 56 57 private Thread thread; 58 main(String[] args)59 public static void main(String[] args) { 60 JFrame frame = new JFrame("bug6798062"); 61 62 frame.setSize(400, 300); 63 frame.setLocationRelativeTo(null); 64 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 65 frame.add(new bug6798062().initialize()); 66 67 frame.setVisible(true); 68 } 69 init()70 public void init() { 71 add(initialize()); 72 } 73 initialize()74 private JComponent initialize() { 75 if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) { 76 return new JLabel("The test is suitable only for Windows"); 77 } 78 79 String tempDir = System.getProperty("java.io.tmpdir"); 80 81 if (tempDir.length() == 0) { // 'java.io.tmpdir' isn't guaranteed to be defined 82 tempDir = System.getProperty("user.home"); 83 } 84 85 System.out.println("Temp directory: " + tempDir); 86 87 try { 88 folder = ShellFolder.getShellFolder(new File(tempDir)); 89 } catch (FileNotFoundException e) { 90 fail("Directory " + tempDir + " not found"); 91 } 92 93 slider.setMajorTickSpacing(10); 94 slider.setPaintTicks(true); 95 slider.setPaintLabels(true); 96 slider.setSnapToTicks(true); 97 slider.setValue(10); 98 99 btnStart.addActionListener(new ActionListener() { 100 public void actionPerformed(ActionEvent e) { 101 setEnabledState(false); 102 103 thread = new MyThread(slider.getValue(), tfLink.getText()); 104 thread.start(); 105 } 106 }); 107 108 btnStop.setEnabled(false); 109 110 btnStop.addActionListener(new ActionListener() { 111 public void actionPerformed(ActionEvent e) { 112 thread.interrupt(); 113 thread = null; 114 115 setEnabledState(true); 116 } 117 }); 118 119 btnGC.addActionListener(new ActionListener() { 120 public void actionPerformed(ActionEvent e) { 121 System.gc(); 122 } 123 }); 124 125 setEnabledState(true); 126 127 JPanel pnButtons = new JPanel(); 128 129 pnButtons.setLayout(new BoxLayout(pnButtons, BoxLayout.X_AXIS)); 130 131 pnButtons.add(btnStart); 132 pnButtons.add(btnStop); 133 pnButtons.add(btnGC); 134 135 tfLink.setMaximumSize(new Dimension(300, 20)); 136 137 JPanel pnContent = new JPanel(); 138 139 pnContent.setLayout(new BoxLayout(pnContent, BoxLayout.Y_AXIS)); 140 pnContent.add(new JLabel("Delay between listFiles() invocation (ms):")); 141 pnContent.add(slider); 142 pnContent.add(new JLabel("Provide link here:")); 143 pnContent.add(tfLink); 144 pnContent.add(pnButtons); 145 146 return pnContent; 147 } 148 setEnabledState(boolean enabled)149 private void setEnabledState(boolean enabled) { 150 slider.setEnabled(enabled); 151 btnStart.setEnabled(enabled); 152 btnStop.setEnabled(!enabled); 153 } 154 fail(String msg)155 private static void fail(String msg) { 156 throw new RuntimeException(msg); 157 } 158 159 private class MyThread extends Thread { 160 private final int delay; 161 162 private final ShellFolder link; 163 MyThread(int delay, String link)164 private MyThread(int delay, String link) { 165 this.delay = delay; 166 167 ShellFolder linkFolder; 168 169 try { 170 linkFolder = ShellFolder.getShellFolder(new File(link)); 171 } catch (FileNotFoundException e) { 172 e.printStackTrace(); 173 174 linkFolder = null; 175 } 176 177 this.link = linkFolder; 178 } 179 run()180 public void run() { 181 while (!isInterrupted()) { 182 folder.listFiles(); 183 if (link != null) { 184 try { 185 link.getLinkLocation(); 186 } catch (FileNotFoundException e) { 187 e.printStackTrace(); 188 } 189 } 190 191 if (delay > 0) { 192 try { 193 Thread.sleep(delay); 194 } catch (InterruptedException e1) { 195 // The thread was interrupted 196 return; 197 } 198 } 199 } 200 } 201 } 202 } 203