1 /*
   2  * Copyright (c) 2015, 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 import java.awt.BorderLayout;
  25 import java.awt.Dimension;
  26 import java.awt.EventQueue;
  27 import java.awt.GraphicsEnvironment;
  28 
  29 import javax.swing.JFrame;
  30 import javax.swing.JLabel;
  31 
  32 import sun.java2d.SunGraphicsEnvironment;
  33 
  34 /**
  35  * @test
  36  * @bug 8041654
  37  * @key headful
  38  * @modules java.desktop/sun.java2d
  39  * @run main/othervm -Xmx80m DisplayListenerLeak
  40  */
  41 public final class DisplayListenerLeak {
  42 
  43     private static JFrame frame;
  44     private volatile static boolean failed = false;
  45 
  46     private static void createAndShowGUI() {
  47         Thread.currentThread().setUncaughtExceptionHandler((t, e) -> {
  48             e.printStackTrace();
  49             failed = true;
  50         });
  51         frame = new JFrame();
  52         JLabel emptyLabel = new JLabel("");
  53         emptyLabel.setPreferredSize(new Dimension(600, 400));
  54         frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
  55         frame.pack();
  56         frame.setVisible(true);
  57     }
  58 
  59     public static void main(final String[] args) throws Exception {
  60         GraphicsEnvironment ge =
  61                 GraphicsEnvironment.getLocalGraphicsEnvironment();
  62         if (!(ge instanceof SunGraphicsEnvironment)) {
  63             return;
  64         }
  65         EventQueue.invokeAndWait(() -> createAndShowGUI());
  66         SunGraphicsEnvironment sge = (SunGraphicsEnvironment) ge;
  67         final long startTime = System.nanoTime();
  68         while (!failed) {
  69             if (System.nanoTime() - startTime > 60_000_000_000L) {
  70                 break;
  71             }
  72             System.gc(); // clear all weak references
  73             EventQueue.invokeAndWait(() -> {
  74                 frame.setSize(frame.getHeight(), frame.getWidth());
  75                 frame.pack();
  76             });
  77             EventQueue.invokeAndWait(sge::displayChanged);
  78         }
  79         EventQueue.invokeAndWait(frame::dispose);
  80         if (failed) {
  81             throw new RuntimeException();
  82         }
  83     }
  84 }