Path: blob/master/test/jdk/javax/imageio/plugins/jpeg/JpegWriterLeakTest.java
41152 views
/*1* Copyright (c) 2013, 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 8020983 802469726* @summary Test verifies that jpeg writer instances are collected27* even if destroy() or reset() methods is not invoked.28*29* @run main JpegWriterLeakTest30*/3132import java.awt.Color;33import java.awt.Graphics2D;34import java.awt.image.BufferedImage;35import java.io.ByteArrayOutputStream;36import java.io.IOException;37import java.lang.ref.Reference;38import java.lang.ref.ReferenceQueue;39import java.lang.ref.WeakReference;40import java.util.ArrayList;41import java.util.Random;42import javax.imageio.ImageIO;43import javax.imageio.ImageWriter;44import javax.imageio.stream.ImageOutputStream;4546public class JpegWriterLeakTest {4748public static void main(String[] args) {49final ReferenceQueue<ImageWriter> queue = new ReferenceQueue<>();50final ArrayList<Reference<? extends ImageWriter>> refs = new ArrayList<>();5152int count = 2;5354do {55ImageWriter writer =56ImageIO.getImageWritersByFormatName("jpeg").next();5758final WeakReference<? extends ImageWriter> ref =59new WeakReference<>(writer, queue);6061refs.add(ref);626364try {65final ImageOutputStream os =66ImageIO.createImageOutputStream(new ByteArrayOutputStream());67writer.setOutput(os);6869writer.write(getImage());707172// NB: dispose() or reset() workarounds the problem.73} catch (IOException e) {74} finally {75writer = null;76}77count--;78} while (count > 0);798081System.out.println("Wait for GC...");8283final long testTimeOut = 60000L;8485final long startTime = System.currentTimeMillis();8687while (!refs.isEmpty()) {88// check for the test timeout89final long now = System.currentTimeMillis();9091if (now - startTime > testTimeOut) {92System.out.println();93throw new RuntimeException("Test FAILED.");94}9596System.gc();9798try {99System.out.print(".");100Thread.sleep(1000);101} catch (InterruptedException e) {102};103104Reference<? extends ImageWriter> r = queue.poll();105if (r != null) {106System.out.println("Got reference: " + r);107refs.remove(r);108}109}110System.out.println("Test PASSED.");111}112113private static BufferedImage getImage() {114int width = 2500;115int height = new Random().nextInt(2500) + 1;116BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);117118Graphics2D g = image.createGraphics();119g.setColor(Color.blue);120g.fillRect(0, 0, width, height);121122return image;123}124}125126127