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