1 /* 2 * Copyright (c) 2013, 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 /** 25 * @test 26 * @bug 8020983 8024697 27 * @summary Test verifies that jpeg writer instances are collected 28 * even if destroy() or reset() methods is not invoked. 29 * 30 * @run main JpegWriterLeakTest 31 * @key randomness 32 */ 33 34 import java.awt.Color; 35 import java.awt.Graphics2D; 36 import java.awt.image.BufferedImage; 37 import java.io.ByteArrayOutputStream; 38 import java.io.IOException; 39 import java.lang.ref.Reference; 40 import java.lang.ref.ReferenceQueue; 41 import java.lang.ref.WeakReference; 42 import java.util.ArrayList; 43 import java.util.Random; 44 import javax.imageio.ImageIO; 45 import javax.imageio.ImageWriter; 46 import javax.imageio.stream.ImageOutputStream; 47 48 public class JpegWriterLeakTest { 49 50 public static void main(String[] args) { 51 final ReferenceQueue<ImageWriter> queue = new ReferenceQueue<>(); 52 final ArrayList<Reference<? extends ImageWriter>> refs = new ArrayList<>(); 53 54 int count = 2; 55 56 do { 57 ImageWriter writer = 58 ImageIO.getImageWritersByFormatName("jpeg").next(); 59 60 final WeakReference<? extends ImageWriter> ref = 61 new WeakReference<>(writer, queue); 62 63 refs.add(ref); 64 65 66 try { 67 final ImageOutputStream os = 68 ImageIO.createImageOutputStream(new ByteArrayOutputStream()); 69 writer.setOutput(os); 70 71 writer.write(getImage()); 72 73 74 // NB: dispose() or reset() workarounds the problem. 75 } catch (IOException e) { 76 } finally { 77 writer = null; 78 } 79 count--; 80 } while (count > 0); 81 82 83 System.out.println("Wait for GC..."); 84 85 final long testTimeOut = 60000L; 86 87 final long startTime = System.currentTimeMillis(); 88 89 while (!refs.isEmpty()) { 90 // check for the test timeout 91 final long now = System.currentTimeMillis(); 92 93 if (now - startTime > testTimeOut) { 94 System.out.println(); 95 throw new RuntimeException("Test FAILED."); 96 } 97 98 System.gc(); 99 100 try { 101 System.out.print("."); 102 Thread.sleep(1000); 103 } catch (InterruptedException e) { 104 }; 105 106 Reference<? extends ImageWriter> r = queue.poll(); 107 if (r != null) { 108 System.out.println("Got reference: " + r); 109 refs.remove(r); 110 } 111 } 112 System.out.println("Test PASSED."); 113 } 114 115 private static BufferedImage getImage() { 116 int width = 2500; 117 int height = new Random().nextInt(2500) + 1; 118 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 119 120 Graphics2D g = image.createGraphics(); 121 g.setColor(Color.blue); 122 g.fillRect(0, 0, width, height); 123 124 return image; 125 } 126 }