Working with buffer pool in MongoClient Java

I use mongodb-driver 4.1.1 and 4.5.1 for Java and have questions about MongoClient.
After analyzing heap dump during the work of the application I found that a lot of memory is used by com.mongodb.internal.connection.PowerOfTwoBufferPool class.

As I understand this is a buffer pool to reduce the performance hit of garbage collection (mongodb - Memory leak with Mongo Java Connection - Stack Overflow). Could you explain to me, please, is there a way to control this pool someway? My application uses all heap memory during the work and doesn’t clear it. Sometimes it causes heap space error.

Hi @Anton_Konovalov1,

Usage of PowerOfTwoBufferPool can currently be controlled via StreamFactoryFactory which may be specified via MongoClientSettings.Builder.streamFactoryFactory. The only implementation of StreamFactoryFactory provided by the driver that does not use PowerOfTwoBufferPool and supports configuring the allocation policy for byte buffers is NettyStreamFactoryFactory. It uses io.netty.buffer.ByteBufAllocator.DEFAULT by default, and allows setting a different allocator via NettyStreamFactoryFactory.Builder.allocator.

1 Like

Hi @Valentin_Kovalenko . Thank you very much!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.