简体   繁体   English

使用 MongoDb 驱动程序的 Open Shift Container 中的 Java 内存堆空间问题

[英]Java Memory Heap Space Issue in Open Shift Container with MongoDb driver

Below Java syntax is throwing Java memory Heap issue in Openshift while invoking the data from the Mongodb database.Is there any way to know where the memory leakage is happening.Is memory will be reduced if we use Java 8 for each streams instead of for loop .Can anyone help me with Java8 for each synatx with the Pojo class .Here Amount is the POJO class.下面的 Java 语法是在调用来自 Mongodb 数据库的数据时在 Openshift 中抛出 Java 内存堆问题。任何人都可以用Java8 帮助我使用Pojo 类的每个synatx。这里Amount 是POJO 类。 Or Any other suggestions also can be appreciated to reduce the memory for too many requests.或者任何其他建议也可以减少太多请求的内存。

public String getAmountDetails( @RequestParam("system") @Valid String system,
                                 @RequestParam("Claims") BigDecimal Claims,
                                 @RequestParam("Bills") BigDecimal totalOfCommissions,
                                 @RequestParam("taxes") BigDecimal totalOfSurcharges,
                                 @RequestParam("userDetails") BigDecimal userDetails,
                               ) throws Exception {

        Query Query = new Query();
       
        if (system != null && !system.isEmpty())
           Query.addCriteria(Criteria.where("system").is(system));
        List<Amount> amount = new ArrayList<>();
       
        amount = mongoOps.find(Query, Amount.class);
        amount.addAll(Amount);
       
        totalAmount = new Amount();
        if (Amount.isEmpty()) {
            totalAmount.setSystem(system);
            totalAmount.setClaims(BigDecimal.ZERO);
            totalAmount.setBillsBigDecimal.ZERO);
            totalAmount.setTaxes(BigDecimal.ZERO);
            totalAmount.setuserDetails(BigDecimal.ZERO);
            totalAmount.setStatus("Success");
            return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(totalAmount);
        }
        BigDecimal claims = new BigDecimal(0);
        BigDecimal bills = new BigDecimal(0);
        BigDecimal taxes = new BigDecimal(0);
        BigDecimal userDeatils = new BigDecimal(0);
      
       
        for (Amount amount : getAmount) {
            if (null != amount.getClaims()) {
                claimsAmt = claimsAmt.add(amount.getclaimsAmt().bigDecimalValue());
            }
            if (null != amount.getBillsAmt()) {
                billslAmt = billsAmt.add(amount.getBillsAmt().bigDecimalValue());
            }
            if (null != amount.gettaxesAmt()) {
                taxesAmt = taxesAmt.add(amount.getTaxesAmt().bigDecimalValue());
            }
            totalAmount.setClaims(claimsAmt);
            totalAmount.setbills(billsAmt);
            totalAmount.settaxesAmt(taxesAmt);
        }
       
    }

To start with - your foreach loop is ok, besides the fact that you haven't shown what is getAmount over which you're iterating.首先 - 你的 foreach 循环没问题,除了你没有显示你正在迭代的getAmount是什么。 So assume its a typo and basically you ask mongo to bring you all the "Amount" objects by some criteria and all this only to calculated the sum of some values.所以假设它是一个错字,基本上你要求 mongo 根据一些标准为你带来所有的“Amount”对象,而这一切只是为了计算一些值的总和。

If there are many objects in the database, this will be:如果数据库中有很多对象,这将是:

  • Slow减缓
  • Memory Consuming内存消耗

I wouldn't say there is a leak here, just an ineffective flow.我不会说这里有泄漏,只是无效的流程。 In OpenShift you might work with some "serious" environment and if there are many requests running on a big amount of objects simultaneously you might end up with OutOfMemory indeed.在 OpenShift 中,您可能会在一些“严重”的环境中工作,如果同时在大量对象上运行许多请求,您最终可能会遇到 OutOfMemory。

To start with the verification of this theory you may log a number of objects that you get from mongo (size of the amount list).为了验证这个理论,您可以记录从 mongo 获得的许多对象( amount列表的大小)。 If you have pretty big numbers, continue reading.如果您有相当大的数字,请继续阅读。

Now in terms of solution:现在在解决方案方面:

So maybe you should ask mongo to calculate a sum for you instead of doing it in Java.所以也许你应该让 mongo 为你计算一个总和,而不是用 Java 来计算。 Mongo will handle that even if there are many documents in the collection.即使集合中有很多文档,Mongo 也会处理这个问题。

I haven't used mongo for a lot of years, but it has an aggregation framework with $sum that should be used.我已经很多年没有使用 mongo,但它有一个聚合框架,应该使用$sum

Read This SO thread as they provide an example of how to achieve a similar task in mongo.阅读 此 SO 线程,因为它们提供了如何在 mongo 中实现类似任务的示例。

Also you can Read this thread for even slightly more complicated example that you did.您也可以阅读此线程,了解您所做的更复杂的示例。

Here is a link to mongo documentation that can come handy as well这是 mongo 文档的链接,也可以派上用场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM