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