1 /*
   2  * Copyright (c) 2016 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
  25  * @bug 8158325
  26  * @summary Memory leak in com.apple.laf.ScreenMenu: removed JMenuItems are still referenced
  27  * @requires (os.family == "mac")
  28  * @run main/timeout=300/othervm -Xmx16m ScreenMenuMemoryLeakTest
  29  */
  30 import java.awt.EventQueue;
  31 import java.lang.ref.WeakReference;
  32 import java.lang.reflect.InvocationTargetException;
  33 import java.util.Objects;
  34 
  35 import javax.swing.JFrame;
  36 import javax.swing.JLabel;
  37 import javax.swing.JMenu;
  38 import javax.swing.JMenuBar;
  39 import javax.swing.JMenuItem;
  40 import javax.swing.WindowConstants;
  41 
  42 public class ScreenMenuMemoryLeakTest {
  43 
  44     private static byte[] sBytes;
  45 
  46     private static WeakReference<JMenuItem> sMenuItem;
  47     private static JFrame sFrame;
  48     private static JMenu sMenu;
  49 
  50     public static void main(String[] args) throws InvocationTargetException, InterruptedException {
  51         EventQueue.invokeAndWait(new Runnable() {
  52             @Override
  53             public void run() {
  54                 System.setProperty("apple.laf.useScreenMenuBar", "true");
  55                 showUI();
  56             }
  57         });
  58 
  59         EventQueue.invokeAndWait(new Runnable() {
  60             @Override
  61             public void run() {
  62                 removeMenuItemFromMenu();
  63             }
  64         });
  65         System.gc();
  66         System.runFinalization();
  67         JMenuItem menuItem = sMenuItem.get();
  68         EventQueue.invokeAndWait(new Runnable() {
  69             @Override
  70             public void run() {
  71                 sFrame.dispose();
  72             }
  73         });
  74         if (menuItem != null) {
  75             throw new RuntimeException("The menu item should have been GC-ed");
  76         }
  77     }
  78 
  79     private static void showUI() {
  80         sFrame = new JFrame();
  81         sFrame.add(new JLabel("Some dummy content"));
  82 
  83         JMenuBar menuBar = new JMenuBar();
  84 
  85         sMenu = new JMenu("Menu");
  86         JMenuItem item = new JMenuItem("Item");
  87         sMenu.add(item);
  88 
  89         sMenuItem = new WeakReference<>(item);
  90 
  91         menuBar.add(sMenu);
  92 
  93         sFrame.setJMenuBar(menuBar);
  94 
  95         sFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  96         sFrame.pack();
  97         sFrame.setVisible(true);
  98     }
  99 
 100     private static void removeMenuItemFromMenu() {
 101         JMenuItem menuItem = sMenuItem.get();
 102         Objects.requireNonNull(menuItem, "The menu item should still be available at this point");
 103         sMenu.remove(menuItem);
 104     }