Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/List/ListGarbageCollectionTest/AwtListGarbageCollectionTest.java
38828 views
/*1* Copyright (c) 2014, 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* @bug 804007626* @summary AwtList not garbage collected27* @run main/othervm -Xmx100m AwtListGarbageCollectionTest28*/2930import java.awt.*;31import java.awt.event.WindowAdapter;32import java.awt.event.WindowEvent;33import java.lang.ref.WeakReference;3435public class AwtListGarbageCollectionTest {36public static void main(String[] args) {37Frame frame = new Frame("List leak test");38try {39test(frame);40} finally {41frame.dispose();42}43}4445private static void test(Frame frame) {46WeakReference<List> weakListRef = null;47try {48frame.setSize(300, 200);49frame.setVisible(true);5051List strongListRef = new List();52frame.add(strongListRef);53strongListRef.setMultipleMode(true);54frame.remove(strongListRef);55weakListRef = new WeakReference<List>(strongListRef);56strongListRef = null;5758//make out of memory to force gc59String veryLongString = new String(new char[100]);60while (true) {61veryLongString += veryLongString;62}63} catch (OutOfMemoryError e) {64if (weakListRef == null) {65throw new RuntimeException("Weak list ref wasn't created");66} else if (weakListRef.get() != null) {67throw new RuntimeException("List wasn't garbage collected");68}69}70}71}727374