Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/apple/laf/ScreenMenu/ScreenMenuMemoryLeakTest.java
38854 views
/*1* Copyright (c) 2016 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*/22/*23* @test24* @bug 815832525* @summary Memory leak in com.apple.laf.ScreenMenu: removed JMenuItems are still referenced26* @requires (os.family == "mac")27* @run main/timeout=300/othervm -Xmx16m ScreenMenuMemoryLeakTest28*/29import java.awt.EventQueue;30import java.lang.ref.WeakReference;31import java.lang.reflect.InvocationTargetException;32import java.util.Objects;3334import javax.swing.JFrame;35import javax.swing.JLabel;36import javax.swing.JMenu;37import javax.swing.JMenuBar;38import javax.swing.JMenuItem;39import javax.swing.WindowConstants;4041public class ScreenMenuMemoryLeakTest {4243private static byte[] sBytes;4445private static WeakReference<JMenuItem> sMenuItem;46private static JFrame sFrame;47private static JMenu sMenu;4849public static void main(String[] args) throws InvocationTargetException, InterruptedException {50EventQueue.invokeAndWait(new Runnable() {51@Override52public void run() {53System.setProperty("apple.laf.useScreenMenuBar", "true");54showUI();55}56});5758EventQueue.invokeAndWait(new Runnable() {59@Override60public void run() {61removeMenuItemFromMenu();62}63});64System.gc();65System.runFinalization();66JMenuItem menuItem = sMenuItem.get();67EventQueue.invokeAndWait(new Runnable() {68@Override69public void run() {70sFrame.dispose();71}72});73if (menuItem != null) {74throw new RuntimeException("The menu item should have been GC-ed");75}76}7778private static void showUI() {79sFrame = new JFrame();80sFrame.add(new JLabel("Some dummy content"));8182JMenuBar menuBar = new JMenuBar();8384sMenu = new JMenu("Menu");85JMenuItem item = new JMenuItem("Item");86sMenu.add(item);8788sMenuItem = new WeakReference<>(item);8990menuBar.add(sMenu);9192sFrame.setJMenuBar(menuBar);9394sFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);95sFrame.pack();96sFrame.setVisible(true);97}9899private static void removeMenuItemFromMenu() {100JMenuItem menuItem = sMenuItem.get();101Objects.requireNonNull(menuItem, "The menu item should still be available at this point");102sMenu.remove(menuItem);103}104}105106