我在应用程序中发现了非常严重的内存泄漏。当尝试使用Morphia批量写入Mongo时,性能迅速下降,并继续降级。 (即最初的100条记录需要<1秒写入,记录8899至9000需要310秒。)
使用Yourkit我可以看到比com.mongodb.BasicDBObject,com.mongodb.DBRef,com.mongodb.BasicDBList具有非常高的保留大小(41-81%)并不断报告为待定终结。
此外,如果我将datastore.save(...)更改为写入磁盘,我看不到性能下降。
我目前没有使用MongoOptions。我可以做?有哪些调整可以改善我的应用程序的性能?
我正在使用mongo java驱动程序版本2.7.0和morphia 0.99.1-SNAPSHOT。
我在一个简单的for循环中调用我的控制器方法:
@RequestMapping(value = "/reports", method = RequestMethod.POST, headers = "Accept=application/json")
public ResponseEntity<Result> add(@RequestParam("title") String title,
@RequestParam("uri") String uri,
@RequestParam(value = "imageUri", required = false) String imageUri,
@RequestParam(value = "categoryIds", required = false) List<String> categoryIds,
@RequestParam(value = "expertsIds", required = false) List<String> expertIds,
@RequestParam(value = "userId", required = false) String userIdString,
@RequestParam(value = "description", required = false) String description) {
if (imageUri == null) {
imageUri = "";
}
List<Category> categories = new ArrayList<Category>();
if (categoryIds != null && !categoryIds.isEmpty()) {
categories = categoryService.getCategoriesById(categoryIds);
}
Report report = new Report(StringEscapeUtils.escapeHtml(title), uri, imageUri, categories, expertIds, description);
UUID userID = userService.convertUserId(userIdString);
Result result = userService.addReport(userID, report);
return new ResponseEntity<Result>(result);
}
public Result addReport(UUID userID, Report report) {
Result result = reportService.add(report);
if (!result.getErrorMessages().isEmpty()) {
return result;
}
if (!doesUserExistInDb(userID)) {
createUserWithId(userID);
}
updateUserWithReport(userID, report);
return result;
}
public Result add(Report report) {
Result result = new Result();
try {
datastore.save(report);
result.addReport(report);
} catch (ConstraintViolationException ex) {
populateValidationErrors(result, ex);
}
return result;
}
public void updateUserWithReport(UUID userID, Report report) {
User user = datastore.get(User.class, userID);
user.getReports().add(report);
datastore.save(user);
}