解决使用BufferedImage内存溢出的问题OutOfMemoryError: Java heap space

您所在的位置:网站首页 imageioread() 解决使用BufferedImage内存溢出的问题OutOfMemoryError: Java heap space

解决使用BufferedImage内存溢出的问题OutOfMemoryError: Java heap space

#解决使用BufferedImage内存溢出的问题OutOfMemoryError: Java heap space| 来源: 网络整理| 查看: 265

业务上有一个缩放图片的操作使用了BufferedImage,但是经常发现生成图片失败。查看日志显示OutOfMemoryError: Java heap space,内存溢出错误。通过获取java hprof来分析,发现大部分内存被BufferedImage占用。难道生成缩略图后,没有自动释放?

答案是的,当释放图片后,再没有发生过OutOfMemoryError。看看如下代码:

/** * 生成缩略图写入输出流 * @throws IOException */ public void toOutputStream(OutputStream out) throws ImageResizeException { BufferedImage source = null; try { //原图 source = ImageIO.read(file); if (source == null) { throw new ImageResizeException("无法加载原始图片"); } //这里省略对图像进行处理 ImageIO.write(source , "PNG", out); } catch (IOException ex) { throw new ImageResizeException(ex.getMessage()); } finally { if (source != null) { source.getGraphics().dispose(); } } }

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3