Path: blob/master/test/jdk/sun/java2d/Disposer/TestDisposerLeak.java
41149 views
/*1* Copyright (c) 2015, 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*/2223import java.util.LinkedList;24import java.util.List;2526import sun.java2d.Disposer;27import sun.java2d.DisposerRecord;2829/**30* @test31* @bug 812945732* @summary Check Disposer disposes all objects without any memory leaks33* @run main/othervm -mx128m TestDisposerLeak34* @modules java.desktop/sun.java2d35*/36public final class TestDisposerLeak {3738private static final int COUNT = 30000;3940private static volatile boolean disposerComplete;4142private static volatile boolean readyForDispose;4344static int disposeCount = 0;4546public static void main(String[] args) throws Exception {47TestDisposerLeak test = new TestDisposerLeak();48test.testLeak();49while (!disposerComplete) {}50if (disposeCount != COUNT) {51System.err.println("dispose called " + disposeCount);52throw new RuntimeException("All objects are not disposed");53}else {54System.out.println("Test PASSED");55}56}5758public void testLeak() throws Exception {59MyDisposerRec disposerRecord = new MyDisposerRec();60for (int i = 0; i < 30000; i++) {61Disposer.addObjectRecord(new Object(), disposerRecord);62}63generateOOME();64readyForDispose = true;65Disposer.addObjectRecord(new Object(), new EndRec());66generateOOME();67}6869class MyDisposerRec implements DisposerRecord, Disposer.PollDisposable {7071public void dispose() {72while (!readyForDispose) {}73disposeCount++;74Disposer.pollRemove();75}76}7778class EndRec implements DisposerRecord, Disposer.PollDisposable {7980public void dispose() {81disposerComplete = true;82}83}8485private static void generateOOME() throws Exception {86List<Object> leak = new LinkedList<>();87try {88while (true) {89leak.add(new byte[1024 * 1024]);90}91} catch (OutOfMemoryError e) {92}93// Give the GC a chance at that weakref in case of slow systems94Thread.sleep(2000);95}96}979899