1 /*
   2  * Copyright (c) 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 import java.awt.EventQueue;
  25 import java.awt.FlowLayout;
  26 
  27 import javax.swing.*;
  28 import javax.swing.UIManager.LookAndFeelInfo;
  29 
  30 import static javax.swing.UIManager.getInstalledLookAndFeels;
  31 
  32 /**
  33  * @test
  34  * @bug 8134947
  35  * @author Sergey Bylokhov
  36  * @run main/timeout=300/othervm -Xmx12m -XX:+HeapDumpOnOutOfMemoryError UnninstallUIMemoryLeaks
  37  */
  38 public final class UnninstallUIMemoryLeaks {
  39 
  40     private static JFrame frame;
  41 
  42     public static void main(final String[] args) throws Exception {
  43         try {
  44             createGUI();
  45             for (final LookAndFeelInfo laf : getInstalledLookAndFeels()) {
  46                 final String name = laf.getName();
  47                 if (name.contains("OS X") || name.contains("Metal")) {
  48                     SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
  49                     SwingUtilities.invokeAndWait(() -> {
  50                         for (int i = 0; i < 4000; ++i) {
  51                             SwingUtilities.updateComponentTreeUI(frame);
  52                         }
  53                     });
  54                 }
  55             }
  56         } finally {
  57             EventQueue.invokeAndWait(() -> frame.dispose());
  58         }
  59     }
  60 
  61     private static void createGUI() throws Exception {
  62         EventQueue.invokeAndWait(() -> {
  63             frame = new JFrame();
  64             frame.setLayout(new FlowLayout());
  65 
  66             frame.add(new JButton("JButton"));
  67             frame.add(new JCheckBox("JCheckBox"));
  68             frame.add(new JComboBox<>());
  69             frame.add(new JEditorPane());
  70             frame.add(new JFormattedTextField("JFormattedTextField"));
  71             frame.add(new JLabel("label"));
  72             frame.add(new JPanel());
  73             frame.add(new JPasswordField("JPasswordField"));
  74             frame.add(new JProgressBar());
  75             frame.add(new JRadioButton("JRadioButton"));
  76             frame.add(new JScrollBar());
  77             frame.add(new JScrollPane());
  78             frame.add(new JSeparator());
  79             frame.add(new JSlider());
  80             frame.add(new JSpinner());
  81             frame.add(new JSplitPane());
  82             frame.add(new JTabbedPane());
  83             frame.add(new JTable());
  84             frame.add(new JTextArea("JTextArea"));
  85             frame.add(new JTextField("JTextField"));
  86             frame.add(new JTextPane());
  87             frame.add(new JToggleButton());
  88             frame.add(new JToolBar());
  89             frame.add(new JToolTip());
  90             frame.add(new JTree());
  91             frame.add(new JViewport());
  92 
  93             final JMenuBar bar = new JMenuBar();
  94             final JMenu menu1 = new JMenu("menu1");
  95             final JMenu menu2 = new JMenu("menu2");
  96             menu1.add(new JMenuItem("menuitem"));
  97             menu2.add(new JCheckBoxMenuItem("JCheckBoxMenuItem"));
  98             menu2.add(new JRadioButtonMenuItem("JRadioButtonMenuItem"));
  99             bar.add(menu1);
 100             bar.add(menu2);
 101             frame.setJMenuBar(bar);
 102 
 103             final String[] data = {"one", "two", "three", "four"};
 104             final JList<String> list = new JList<>(data);
 105             frame.add(list);
 106 
 107             final JDesktopPane pane = new JDesktopPane();
 108             final JInternalFrame internalFrame = new JInternalFrame();
 109             internalFrame.setBounds(10, 10, 130, 130);
 110             internalFrame.setVisible(true);
 111             pane.add(internalFrame);
 112             pane.setSize(150, 150);
 113 
 114             frame.add(pane);
 115             frame.pack();
 116             frame.setSize(600, 600);
 117             frame.setLocationRelativeTo(null);
 118             // Commented to prevent a reference from RepaintManager
 119             // frame.setVisible(true);
 120         });
 121     }
 122 
 123     private static void setLookAndFeel(final LookAndFeelInfo laf) {
 124         try {
 125             UIManager.setLookAndFeel(laf.getClassName());
 126             System.out.println("LookAndFeel: " + laf.getClassName());
 127         } catch (ClassNotFoundException | InstantiationException |
 128                 UnsupportedLookAndFeelException | IllegalAccessException e) {
 129             throw new RuntimeException(e);
 130         }
 131     }
 132 }