2
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()没有奏效。请帮忙。

4

2 回答 2

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
}
于 2013-02-14T01:19:01.440 回答
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);
}
于 2013-02-13T06:12:35.257 回答