2

The code below causes presumably memory leak:

public int reregister(ReregisterDTO reregisterDTO) {
    AtomicInteger count = new AtomicInteger(0);

    StreamUtils.createStreamFromIterator(
        mongoTemplate
            .stream(createQuery(reregisterDTO), Shipment.class))
            .forEach(shipment -> this.reregisterShipment(shipment, count)
    );

    return count.get();
}

It has fetched 20000 entities from DB but appears that memory is not released. With even bigger volume JVM ran out of memory completely. How do I free up memory and avoid it? could it be that underlying srping-mongodb code holds reference somewhere?

Memory footprint

UPDATE & FIX:

So apparently I mixed up parentheses here, has to be in this order:

    StreamUtils.createStreamFromIterator(
        mongoTemplate.stream(
            createQuery(reregisterDTO),
            Shipment.class
        )
    ).forEach(shipment -> this.reregisterShipment(shipment, count));

Credits to Qingfei Yuan's answer below.

Cmyker
  • 2,318
  • 1
  • 26
  • 29

1 Answers1

3

Seems you need create stream by StreamUtils.createStreamFromIterator(Iterator<T>) instead of use stream directly.

Please refer to Java 8 stream support in MongoTemplate

Qingfei Yuan
  • 1,196
  • 1
  • 8
  • 12
  • Oh my, such a silly mistake, apparently .forEach(shipment -> this.reregisterShipment(shipment, count) has to be after StreamUtils.createStreamFromIterator()! Thank you for pointing to that! – Cmyker Jun 13 '19 at 15:09