author | bae |
Fri, 13 Sep 2013 20:28:17 +0400 | |
changeset 19788 | 2899c95495a9 |
parent 19017 | 0528383260ef |
permissions | -rw-r--r-- |
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
1 |
/* |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
4 |
* |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
8 |
* |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
13 |
* accompanied this code). |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
14 |
* |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
18 |
* |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
21 |
* questions. |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
22 |
*/ |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
23 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
24 |
/** |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
25 |
* @test |
19788 | 26 |
* @bug 8020983 8024697 |
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
27 |
* @summary Test verifies that jpeg writer instances are collected |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
28 |
* even if destroy() or reset() methods is not invoked. |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
29 |
* |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
30 |
* @run main JpegWriterLeakTest |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
31 |
*/ |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
32 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
33 |
import java.awt.Color; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
34 |
import java.awt.Graphics2D; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
35 |
import java.awt.image.BufferedImage; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
36 |
import java.io.ByteArrayOutputStream; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
37 |
import java.io.IOException; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
38 |
import java.lang.ref.Reference; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
39 |
import java.lang.ref.ReferenceQueue; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
40 |
import java.lang.ref.WeakReference; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
41 |
import java.util.ArrayList; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
42 |
import java.util.Random; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
43 |
import javax.imageio.ImageIO; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
44 |
import javax.imageio.ImageWriter; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
45 |
import javax.imageio.stream.ImageOutputStream; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
46 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
47 |
public class JpegWriterLeakTest { |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
48 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
49 |
public static void main(String[] args) { |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
50 |
final ReferenceQueue<ImageWriter> queue = new ReferenceQueue<>(); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
51 |
final ArrayList<Reference<? extends ImageWriter>> refs = new ArrayList<>(); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
52 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
53 |
int count = 2; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
54 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
55 |
do { |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
56 |
ImageWriter writer = |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
57 |
ImageIO.getImageWritersByFormatName("jpeg").next(); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
58 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
59 |
final WeakReference<? extends ImageWriter> ref = |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
60 |
new WeakReference<>(writer, queue); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
61 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
62 |
refs.add(ref); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
63 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
64 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
65 |
try { |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
66 |
final ImageOutputStream os = |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
67 |
ImageIO.createImageOutputStream(new ByteArrayOutputStream()); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
68 |
writer.setOutput(os); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
69 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
70 |
writer.write(getImage()); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
71 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
72 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
73 |
// NB: dispose() or reset() workarounds the problem. |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
74 |
} catch (IOException e) { |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
75 |
} finally { |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
76 |
writer = null; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
77 |
} |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
78 |
count--; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
79 |
} while (count > 0); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
80 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
81 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
82 |
System.out.println("Wait for GC..."); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
83 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
84 |
final long testTimeOut = 60000L; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
85 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
86 |
final long startTime = System.currentTimeMillis(); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
87 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
88 |
while (!refs.isEmpty()) { |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
89 |
// check for the test timeout |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
90 |
final long now = System.currentTimeMillis(); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
91 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
92 |
if (now - startTime > testTimeOut) { |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
93 |
System.out.println(); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
94 |
throw new RuntimeException("Test FAILED."); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
95 |
} |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
96 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
97 |
System.gc(); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
98 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
99 |
try { |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
100 |
System.out.print("."); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
101 |
Thread.sleep(1000); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
102 |
} catch (InterruptedException e) { |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
103 |
}; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
104 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
105 |
Reference<? extends ImageWriter> r = queue.poll(); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
106 |
if (r != null) { |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
107 |
System.out.println("Got reference: " + r); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
108 |
refs.remove(r); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
109 |
} |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
110 |
} |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
111 |
System.out.println("Test PASSED."); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
112 |
} |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
113 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
114 |
private static BufferedImage getImage() { |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
115 |
int width = 2500; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
116 |
int height = new Random().nextInt(2500) + 1; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
117 |
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
118 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
119 |
Graphics2D g = image.createGraphics(); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
120 |
g.setColor(Color.blue); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
121 |
g.fillRect(0, 0, width, height); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
122 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
123 |
return image; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
124 |
} |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
diff
changeset
|
125 |
} |