【发布时间】:2014-10-26 17:04:19
【问题描述】:
我有一些效率问题。我正在开发一个部署在 jboss EAP 6.1 服务器上作为 EAR 存档的企业应用程序。我在 while 循环中基于实体创建新对象并将它们写入文件。我以有限的数量(例如每个步骤 2000 个)获得这些实体(在 EJB DAO 的帮助下)。问题是我需要处理数百万个对象,前一百万个进行得非常顺利,但进一步的循环运行得越慢。谁能告诉我为什么随着循环的进展,这个工作越来越慢?我怎样才能让它一直顺利运行?以下是代码的一些关键部分:
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);
}
}
感谢您的帮助!
【问题讨论】:
-
您查看过堆统计信息吗?
-
@HotLicks 我想过但老实说我不太确定该怎么做。
-
indexWriter是什么?似乎您正在向其中添加所有文档,并且它将保留对它们的引用,将它们保存在内存中。 -
@FlorentBayle 它是一个 Apache Lucene 类。我想我会尝试移动 commitIndex();正如其他人建议的那样,在 while 循环内。
标签: java performance while-loop lucene large-data