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