我在我的数据存储中有大约1200个实体。我想将它们添加到Google搜索索引中。当我有大约800个时,它没有问题,但现在当我尝试将它们添加到搜索索引时,我遇到了Java内存不足错误。最终我希望有10万到50万个需要进入索引的实体..所以我需要以正确的方式做到这一点..从AppEngine Objectify到Google搜索索引 - OutOfMemory
我重构了我的代码,从数据存储使用对象只有50个实体时间,将这些添加到索引,然后拉另一个50.这一切都是在一个for循环中完成的,一次只能处理50个,直到1200 ..但是我仍然发现内存错误。
有没有更好的方法?
int entitiesPerFetch = 50;
for(int i=0;i<numTotalEntities;i=i+entitiesPerFetch){
List<MyEntity> ents = ofy().load().type(MyEntity.class).filter("year >", 200).order("-year").offset(i).limit(entitiesPerFetch).list();
for(int g=0;g<ents.size();g++){
try {
MyEntity myent = ents.get(g);
String docID = myent.getID();
Document doc = Document.newBuilder()
.setId(docID) // Setting the document identifer is optional. If omitted, the search service will create an identifier.
.addField(Field.newBuilder().setName("title").setText(myent.getTitle()))
.build();
index.put(doc);
}
catch(Exception e){
continue;
}
}
}
啊,我被指向Objectify.clear()清除本地会话缓存。我会明天尝试,并回答这个问题,如果解决这个问题,我怀疑它会。 – 2014-09-06 12:57:24