Path: blob/master/test/jdk/com/apple/laf/ScreenMenu/ScreenMenuMemoryLeakTest.java
41152 views
/*1* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @key headful26* @bug 8158325 8180821 820287827* @summary Memory leak in com.apple.laf.ScreenMenu: removed JMenuItems are still referenced28* @requires (os.family == "mac")29* @library /javax/swing/regtesthelpers30* @build Util31* @run main/timeout=300/othervm -Xmx16m ScreenMenuMemoryLeakTest32*/3334import java.awt.EventQueue;35import java.lang.ref.WeakReference;36import java.lang.reflect.InvocationTargetException;37import java.util.Objects;3839import javax.swing.JFrame;40import javax.swing.JLabel;41import javax.swing.JMenu;42import javax.swing.JMenuBar;43import javax.swing.JMenuItem;44import javax.swing.WindowConstants;4546public class ScreenMenuMemoryLeakTest {4748private static WeakReference<JMenuItem> sMenuItem;49private static JFrame sFrame;50private static JMenu sMenu;5152public static void main(String[] args) throws InvocationTargetException, InterruptedException {53EventQueue.invokeAndWait(new Runnable() {54@Override55public void run() {56System.setProperty("apple.laf.useScreenMenuBar", "true");57showUI();58}59});6061EventQueue.invokeAndWait(new Runnable() {62@Override63public void run() {64removeMenuItemFromMenu();65}66});6768Util.generateOOME();69JMenuItem menuItem = sMenuItem.get();70EventQueue.invokeAndWait(new Runnable() {71@Override72public void run() {73sFrame.dispose();74}75});76if (menuItem != null) {77throw new RuntimeException("The menu item should have been GC-ed");78}79}8081private static void showUI() {82sFrame = new JFrame();83sFrame.add(new JLabel("Some dummy content"));8485JMenuBar menuBar = new JMenuBar();8687sMenu = new JMenu("Menu");88JMenuItem item = new JMenuItem("Item");89sMenu.add(item);9091sMenuItem = new WeakReference<>(item);9293menuBar.add(sMenu);9495sFrame.setJMenuBar(menuBar);9697sFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);98sFrame.pack();99sFrame.setVisible(true);100}101102private static void removeMenuItemFromMenu() {103JMenuItem menuItem = sMenuItem.get();104Objects.requireNonNull(menuItem, "The menu item should still be available at this point");105sMenu.remove(menuItem);106}107}108109110