java.lang.OutOfMemoryError: Java heap space

不懂游走 2013-04-18 10:37:47
手里有些图片,几万张,想要对这些图片生成预览图,但是总是报错,麻烦各位大大帮我看下我代码里面问题出在何处,谢谢了!


package liuc.test;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;


import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;


/**
* @author Liuc
* @date 2013-4-17
*/
public class CreateBrevImg {
public static void main(String[] args) {
String filePath = "C:\\Users\\Administrator\\Desktop\\testP";
String brevFilePath = "C:\\Users\\Administrator\\Desktop\\Brev";
CreateBrevImg c = new CreateBrevImg();
c.getBrev(filePath, brevFilePath);
}


public void getBrev(String filePath, String brevFilePath){
File file = new File(filePath);
File[] files = file.listFiles();
InputStream is = null ;
String tmpBrevPicPath = "";
File tmpFile;
int i = 0;
int new_w=150;
int new_h=100;
Image src;
BufferedImage tag;
FileOutputStream newimage;
JPEGImageEncoder encoder;
try {
for (File file2 : files) {
if("Brev".equals(file2.getName())){
continue;
}
// System.out.println(file2.getAbsolutePath());
tmpBrevPicPath = brevFilePath + "\\" + file2.getName();
if(file2.getName().indexOf('.')==-1){
continue;
}
tmpFile = file2;
is = new FileInputStream(tmpFile);
// ImgUtil.brvePic(tmpBrevPicPath, is);

src = javax.imageio.ImageIO.read(is);
tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,new_w,new_h,null);
newimage=new FileOutputStream(tmpBrevPicPath);
encoder = JPEGCodec.createJPEGEncoder(newimage);
encoder.encode(tag);
newimage.close();
i++;
}
} catch (Exception e) {
e.printStackTrace();
} finally{
System.out.println("已转换" + i + "张图片");
is = null ;
files = null;
file = null;
src = null;
tag = null;
newimage = null;
tmpFile = null;
encoder = null;
}

}
}

...全文
337 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
陈砚羲 2013-04-18
  • 打赏
  • 举报
回复
内存溢出 建议一张张测试一下
Intboy 2013-04-18
  • 打赏
  • 举报
回复
for循环一次就关闭流,因为你每次for循环进去后是重新打开流的,或者中间flush以下。
不懂游走 2013-04-18
  • 打赏
  • 举报
回复
引用 5 楼 huangxiaofei 的回复:
根据报错的内容,就是虚拟机内存堆溢出了,调大JVM的内存上限吧
问题是什么我很清楚,我想知道的是问题出在哪?要是几十万张图片,几百万张呢?
huangxiaofei 2013-04-18
  • 打赏
  • 举报
回复
根据报错的内容,就是虚拟机内存堆溢出了,调大JVM的内存上限吧
不懂游走 2013-04-18
  • 打赏
  • 举报
回复
另外补充一下,图片大小为不足1MB到30+MB不等,最大的图片在单独测试的时候是可以正常生成预览图的没有人吗?
Jesse- 2013-04-18
  • 打赏
  • 举报
回复
内存溢出,虚拟机内存设置大一些
不懂游走 2013-04-18
  • 打赏
  • 举报
回复
有没有大神帮我看下
不懂游走 2013-04-18
  • 打赏
  • 举报
回复

补一下报错信息
长笛党希望 2013-04-18
  • 打赏
  • 举报
回复
操作之后要关闭流。
不懂游走 2013-04-18
  • 打赏
  • 举报
回复
引用 17 楼 jxsryecheng 的回复:
图片操作特别耗内存,所以Image用完一定要关闭。
怎么关?我应该用哪个类的那个方法?
tianma630 2013-04-18
  • 打赏
  • 举报
回复
建议分几次转
十橙心橙意 2013-04-18
  • 打赏
  • 举报
回复
图片操作特别耗内存,所以Image用完一定要关闭。
不懂游走 2013-04-18
  • 打赏
  • 举报
回复
引用 14 楼 jxsryecheng 的回复:
src.close()没调,src是图片操作,最关键的要关闭。
src 是这个类型 java.awt.Image tag是这个类型 BufferedImage 我没明白你的意思,可以说详细些吗
十橙心橙意 2013-04-18
  • 打赏
  • 举报
回复
还有tag.close()
十橙心橙意 2013-04-18
  • 打赏
  • 举报
回复
src.close()没调,src是图片操作,最关键的要关闭。
不懂游走 2013-04-18
  • 打赏
  • 举报
回复
在循环结束时关闭输入流并将循环外面声明的变量置为空,还是不行

package liuc.test;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;


import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;


/**
 * @author Liuc
 * @date 2013-4-17
 */
public class CreateBrevImg {
	public static void main(String[] args)  {
		String filePath = "C:\\Users\\Administrator\\Desktop\\testP";
		String brevFilePath = "C:\\Users\\Administrator\\Desktop\\Brev";
		CreateBrevImg c = new CreateBrevImg();
		c.getBrev(filePath, brevFilePath);
	}
	
	
	public void getBrev(String filePath, String brevFilePath){
		File file = new File(filePath);
		File[] files = file.listFiles();
		InputStream is = null ;
		String tmpBrevPicPath = "";
		File tmpFile;
		int i = 0;
		int new_w=150;
		int new_h=100; 
		Image src;
		BufferedImage tag;
		FileOutputStream newimage;
		JPEGImageEncoder encoder;
		try {
			for (File file2 : files) {
				if("Brev".equals(file2.getName())){
					continue;
				}
//				System.out.println(file2.getAbsolutePath());
				tmpBrevPicPath = brevFilePath + "\\" + file2.getName();
				if(file2.getName().indexOf('.')==-1){
					continue;
				}
				tmpFile = file2;
				is = new FileInputStream(tmpFile);
				src = javax.imageio.ImageIO.read(is);                    
			    tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
			    tag.getGraphics().drawImage(src,0,0,new_w,new_h,null);      
			    newimage=new FileOutputStream(tmpBrevPicPath);         
			    encoder = JPEGCodec.createJPEGEncoder(newimage);       
			    encoder.encode(tag);                                              
			    newimage.flush();
			    newimage.close();
			    is.close();
			    
				i++;
				file2 = null;
				tmpFile = null;
				is = null;
				src = null;
				tag = null;
				newimage = null;
				encoder = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			System.out.println("已转换" + i + "张图片");
			is = null ;
			files = null;
			file = null;
			src = null;
			tag = null;
			newimage = null;
			tmpFile = null;
			encoder = null;
		}
		
	}
}

十橙心橙意 2013-04-18
  • 打赏
  • 举报
回复
新代码粘出来看看。
不懂游走 2013-04-18
  • 打赏
  • 举报
回复
在循环结束时关闭输入流并将循环外面声明的变量置为空 依然跪啊`````` 纠结死了,每次都是2319张
GeekZFZ 2013-04-18
  • 打赏
  • 举报
回复
我做过读取几十个文本文件,每个文件从几十M到几百M不等,如果单个文件会引起内存溢出就要定期处理一次,而你的文件最大为:30M,应没问题,只是你应该在每个for循环后关闭流,我看到你并未没关闭输入流,你也可以在catch中打印下该文件的信息及总共读取了多少M的文件了,以作定位之用,希望能帮到你。
十橙心橙意 2013-04-18
  • 打赏
  • 举报
回复
你的newimage都知道在循环里面close()。 为啥你的is和tag不调用close()呢

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧