< prev index next >
test/java/awt/TrayIcon/PopupMenuLeakTest/PopupMenuLeakTest.java
Print this page
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2016 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
@@ -21,19 +21,28 @@
* questions.
*/
/*
@test
- @bug 8007220
- @summary Reference to the popup leaks after the TrayIcon is removed
- @author Petr Pchelko
+ @key headful
+ @bug 8007220 8039081
+ @summary Reference to the popup leaks after the TrayIcon is removed.
+ @requires os.family != "windows"
@library ../../../../lib/testlibrary/
@build ExtendedRobot
@run main/othervm -Xmx50m PopupMenuLeakTest
*/
-import java.awt.*;
+import java.awt.AWTException;
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.Image;
+import java.awt.MenuItem;
+import java.awt.PopupMenu;
+import java.awt.RenderingHints;
+import java.awt.SystemTray;
+import java.awt.TrayIcon;
import javax.swing.SwingUtilities;
import java.awt.image.BufferedImage;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
@@ -42,22 +51,21 @@
public class PopupMenuLeakTest {
static final AtomicReference<WeakReference<TrayIcon>> iconWeakReference = new AtomicReference<>();
static final AtomicReference<WeakReference<PopupMenu>> popupWeakReference = new AtomicReference<>();
static ExtendedRobot robot;
-
public static void main(String[] args) throws Exception {
robot = new ExtendedRobot();
SwingUtilities.invokeAndWait(PopupMenuLeakTest::createSystemTrayIcon);
sleep();
// To make the test automatic we explicitly call addNotify on a popup to create the peer
SwingUtilities.invokeAndWait(PopupMenuLeakTest::addNotifyPopup);
sleep();
SwingUtilities.invokeAndWait(PopupMenuLeakTest::removeIcon);
sleep();
- assertCollected(popupWeakReference.get(), "Failed, reference to popup not collected");
assertCollected(iconWeakReference.get(), "Failed, reference to tray icon not collected");
+ assertCollected(popupWeakReference.get(), "Failed, reference to popup not collected");
}
private static void addNotifyPopup() {
PopupMenu menu = popupWeakReference.get().get();
if (menu == null) {
@@ -75,23 +83,35 @@
}
private static void assertCollected(WeakReference<?> reference, String message) {
java.util.List<byte[]> bytes = new ArrayList<>();
for (int i = 0; i < 5; i ++) {
+ if (reference.get() == null) {
+ // reference is collected, avoid OOMs.
+ break;
+ }
try {
while (true) {
- bytes.add(new byte[1024]);
+ bytes.add(new byte[4096]);
}
} catch (OutOfMemoryError err) {
- bytes = new ArrayList<>();
+ bytes.clear();
+ causeGC();
}
}
if (reference.get() != null) {
throw new RuntimeException(message);
}
}
+ private static void causeGC() {
+ System.gc();
+ System.runFinalization();
+ robot.delay(1000);
+ }
+
+
private static void createSystemTrayIcon() {
final TrayIcon trayIcon = new TrayIcon(createTrayIconImage());
trayIcon.setImageAutoSize(true);
try {
< prev index next >