使用JPA循环遍历大表时的java.lang.OutOfMemoryError

时间:2013-02-13 06:02:47

标签: java jpa playframework

List<Picture> pictures = Picture.findAll(); //Around 2GB of data (uses JPA/play framework)

for (Picture picture : pictures) {
  byte[] b = picture.image.data; //Image raw data is stored in a different table than Picture table
  System.out.println(picture.file_name + " " + b.length);
}

上面的代码在运行时会导致java.lang.OutOfMemoryError,因为图片存储在数据库中而不是数据库中的文件引用。

无论如何,一旦对象完成处理,我可以“刷新”内存吗? 我试过了picture.em().flush()Iterator.remove(), System.gc(),但都没有效果。 请帮忙。

2 个答案:

答案 0 :(得分:2)

似乎我找到了解决方案。每张图片后清除实体管理器。

List<Picture> pictures = Picture.findAll();
for(int i=0;i<pictures.size();i++) {
   Picture picture = Picture.findById(pictures.get(i).id);
   byte[] b = picture.image.data; 
   System.out.println(picture.file_name + " " + b.length);
   //Do extra stuff with image data

   picture.em().clear(); //clear JPA entity manager
}

答案 1 :(得分:1)

尝试创建文件名列表而不是像以下图片:

List<string> picturefiles = Picture.*findAllFilenames*(); //Around 2GB of data (uses JPA/play framework)

for (String picturefile : picturefiles ) {
  picture  o = new picture(picturefile)
  byte[] b = picture.image.data; //Image raw data is stored in a different table than Picture table
  System.out.println(picture.file_name + " " + b.length);
}