我有一些旧代码,直到最近仍在工作,但是现在看来已经讨厌了,因为它可以在使用OpenJDK 6而不是Java SE 6的新服务器上运行.
这个问题似乎与JAI.create有关.我有jpeg文件,可以缩放并转换为png文件.这段代码曾经可以正常工作,但现在已经转移到运行OpenJDK的盒子上,文件描述符似乎永远都不会关闭,而且我看到越来越多的tmp文件堆积在服务器的tmp目录中.这些不是我创建的文件,因此我认为是由JAI完成的.
另一个原因可能是新服务器上的堆大小更大.如果JAI在完成时清理干净,但是GC发生的频率降低了,那么文件可能因此而堆积起来.减小堆大小不是一种选择,并且随着ulimit的增加,我们似乎遇到了不相关的问题.
这是运行此文件时泄漏文件的示例:
/tmp/imageio7201901174018490724.tmp
一些代码:
// Processor is an internal class that aggregates operations
// performed on the image, like resizing
private byte[] processImage(Processor processor, InputStream stream) {
byte[] bytes = null;
SeekableStream s = null;
try {
// Read the file from the stream
s = SeekableStream.wrapInputStream(stream, true);
RenderedImage image = JAI.create("stream", s);
BufferedImage img = PlanarImage.wrapRenderedImage(image).getAsBufferedImage();
// Process image
if (processor != null) {
image = processor.process(img);
}
// Convert to bytes
bytes = convertToPngBytes(image);
} catch (Exception e){
// error handling
} finally {
// Clean up streams
IOUtils.closeQuietly(stream);
IOUtils.closeQuietly(s);
}
return bytes;
}
private static byte[] convertToPngBytes(RenderedImage image) throws IOException {
ByteArrayOutputStream out = null;
byte[] bytes = null;
try {
out = new ByteArrayOutputStream();
ImageIO.write(image, "png", out);
bytes = out.toByteArray();
} finally {
IOUtils.closeQuietly(out);
}
return bytes;
}
我的问题是:
>有人遇到并解决了吗?由于创建的tmp文件不是我的,因此我不知道它们的名称,因此无法对其进行任何操作.
>有哪些用于调整图像大小和格式的库?我听说了Scalr-还有什么我需要研究的?
我现在不愿重提旧代码,但是如果没有其他选择的话…
谢谢!