在几千条推文之后,我的应用程序从流API中收集Twitter4J的推文会收到OutOfMemory错误。
收到状态后,我的代码执行:
- 将状态转换为我自己的TwitterStatus
对象。原因是Twitter4J返回的Status
是一个接口,无法在MongoDB中序列化。
- 将此状态添加到列表中
- 如果列表大小超过25或100(取决于推文的接收速度),请保存到db。
所以这一切都非常简单,我没有在本地存储任何东西但是我得到了这个OutOfMemory错误。我有什么想法可以保持低内存?
代码:
StatusListener listener;
listener = new StatusListener() {
@Override
public void onStatus(Status status) {
nbTweets++;
//the Status returned by Twitter4j is an interface, not serializable. I convert it into my own TwitterStatus object: same fields, serializable.
twitterStatus = convertStatus.convertOneToTwitterStatus(status);
twitterStatus.setJobId(jobUUID);
twitterStatuses.add(twitterStatus);
statusesIds.add(status.getId());
timeSinceLastStatus = System.currentTimeMillis() - timeLastStatus;
//**************************************
//adjusting the frequency of saves to DB, function of number of statuses received per second
if (timeSinceLastStatus < 200) {
sizeBatch = 100;
} else {
sizeBatch = 25;
}
timeLastStatus = System.currentTimeMillis();
progressLong = (Long) ((System.currentTimeMillis() - startDateTime.getMillis()) * 100 / (stopTime - startDateTime.getMillis()));
if (statusesIds.size() > sizeBatch || progressLong.intValue() > progress) {
//**************************************
//saving statuses to the db.
dsTweets.save(twitterStatuses);
twitterStatuses = new ArrayList();
//**************************************
//updating list of status ids of the job.
opsJob = dsJobs.createUpdateOperations(Job.class).addAll("statuses", statusesIds, true);
dsJobs.update(updateQueryJob, opsJob);
statusesIds = new ArrayList();
//updating progress.
System.out.println("progress: " + progressLong);
progress = progressLong.intValue();
opsJobInfo = dsJobsInfo.createUpdateOperations(JobInfo.class).set("progress", progress).set("nbTweets", nbTweets);
dsJobsInfo.update(updateQueryJobInfo, opsJobInfo);
}
}
}
答案 0 :(得分:0)
知道了。
从v.2.6,MongoDB's default write concern is "acknowledge"开始,而不是未确认。这大大减缓了操作
只需将WriteConcern.UNACKNOWLEDGED
添加到所有数据库写入操作即可解决问题。