我有一些效率問題。我正在開發一個作爲EAR歸檔部署在jboss EAP 6.1服務器上的企業應用程序。我在while循環中基於實體創建新對象並將它們寫入文件。我以有限的量獲得這些實體(在EJB DAO的幫助下)(例如,每個步驟2000)。問題是我需要處理數以百萬計的對象,前一百萬行很順利,但進一步的循環越慢越好。誰能告訴我爲什麼這個工作越來越慢,隨着循環的進展?我怎樣才能讓它工作順利?這裏是代碼的一些關鍵部分:lucene在while循環中創建文檔的速度越來越慢
public void createFullIndex(int stepSize) {
int logsNumber = systemLogDao.getSystemLogsNumber();
int counter = 0;
while (counter < logsNumber) {
for (SystemLogEntity systemLogEntity : systemLogDao.getLimitedSystemLogs(counter, stepSize)) {
addDocument(systemLogEntity);
}
counter = counter + stepSize;
}
commitIndex();
}
public void addDocument(SystemLogEntity systemLogEntity) {
try {
Document document = new Document();
document.add(new NumericField("id", Field.Store.YES, true).setIntValue(systemLogEntity.getId()));
document.add(new Field("resource", (systemLogEntity.getResource() == null ? "" : systemLogEntity
.getResource().getResourceCode()), Field.Store.YES, Field.Index.ANALYZED));
document.add(new Field("operationType", (systemLogEntity.getOperationType() == null ? "" : systemLogEntity
document.add(new Field("comment",
(systemLogEntity.getComment() == null ? "" : systemLogEntity.getComment()), Field.Store.YES,
Field.Index.ANALYZED));
indexWriter.addDocument(document);
} catch (CorruptIndexException e) {
LOGGER.error("Failed to add the following log to Lucene index:\n" + systemLogEntity.toString(), e);
} catch (IOException e) {
LOGGER.error("Failed to add the following log to Lucene index:\n" + systemLogEntity.toString(), e);
}
}
我希望你的幫助!
你看過你的堆統計數據嗎? – 2014-09-02 12:18:31
@HotLicks我想過,但說實話,我不太清楚該怎麼做。 – AjMeen 2014-09-02 12:22:27
什麼是'indexWriter'?看來你正在將所有的文檔都添加到它,並且它會保留對它們的引用,並將它們保存在內存中。 – 2014-09-02 12:41:05