[JAVA-2460] Memory leaks when using NettyStream and CommandListener Created: 03/Mar/17  Updated: 14/Sep/23  Resolved: 28/Apr/17

Status: Closed
Project: Java Driver
Component/s: Async
Affects Version/s: 3.4.2
Fix Version/s: 3.5.0

Type: Bug Priority: Major - P3
Reporter: Sergey Polovko Assignee: Ross Lawley
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to JAVA-2839 Memory leaks when using NettyStream a... Closed

 Description   

I found some strange behaviour when NettyStream is used in async driver with a CommandListener configured. See next code example: https://gist.github.com/jamel/957aab95a8385ef43768f378fb405d25

There I enabled high level of Netty resource leak detector to see any problems with resource leakage. When line

.addCommandListener(new NopCommandListener())

is commented all works just fine (see normal_run.log). But when that line is uncommented resource leak detector found some leaks (see buggy_run.log).

After playing with this bug a little bit I found that root cause of these leaks is ByteBufBsonDocument class. When instance of this class is created it takes ownership of given buffer (because bsonOutput.getByteBuffers() increments reference counter in each returning sub buffer). Unfortunately ByteBufBsonDocument class never releases its buffer and currently there is no way to do that.



 Comments   
Comment by koji lin [ 23/Apr/18 ]

ok, I created JAVA-2839

Comment by Jeffrey Yemin [ 23/Apr/18 ]

Hi, kojilin can you open up a new bug for the issue you're seeing?

Thanks,
Jeff

Comment by koji lin [ 23/Apr/18 ]

Hi I met this problem even upgrade to 3.6.3
When we use command listener, I found our code will use ByteBufBsonDocument#createList at
https://github.com/mongodb/mongo-java-driver/blob/0aa2ec20d5215c0ac727602dd2cd891c22c69ba8/driver-core/src/main/com/mongodb/connection/ByteBufBsonDocument.java#L43
and maybe duplicate here needs to be release?
https://github.com/mongodb/mongo-java-driver/blob/0aa2ec20d5215c0ac727602dd2cd891c22c69ba8/driver-core/src/main/com/mongodb/connection/ByteBufBsonDocument.java#L51

Comment by Githook User [ 28/Apr/17 ]

Author:

{u'username': u'rozza', u'name': u'Ross Lawley', u'email': u'ross.lawley@gmail.com'}

Message: Ensure that the ByteBufferBsonOutput releases all on close

JAVA-2460
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/9c3d9fa66ffddb0a8bab03519fc0048987b2f422

Comment by Ross Lawley [ 26/Apr/17 ]

PR: https://github.com/rozza/mongo-java-driver/pull/191

Comment by Ross Lawley [ 25/Apr/17 ]

Hi jamel,

Thank you for the ticket and gist that reproduces the error, also apologies for the lack of response. Rest assured this issue is on our radar and we'll update once we have determined a suitable fix.

Ross

Generated at Thu Feb 08 08:57:17 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.