[JAVA-2713] Reduce memory usage of QueryResult Created: 20/Dec/17  Updated: 27/Oct/23  Resolved: 20/Dec/17

Status: Closed
Project: Java Driver
Component/s: Internal, Performance
Affects Version/s: 3.6.0
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Vincent GILLES Assignee: Ross Lawley
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

QueryResult uses a list to store items, which results in excessive memory usage and unnecessary CPU load.

Indeed, the MongoIterable API is streaming oriented with the functions forEach and iterator, it does not require the use of a list in the background.

In addition, this prevents the use of reusable objects in a ThreadLocal to limit the creation of these and further improve the memory footprint.



 Comments   
Comment by Ross Lawley [ 20/Dec/17 ]

Hi vgilles,

Thanks for the ticket. The reason a QueryResult has to be backed by a list is because of how MongoDB works. When running a query, MongoDB executes the query and returns the first batch of results, along with the cursor id. As the cursor (MongoIterable) is iterated, the driver calls for the next batch in the results from MongoDB via the getMore command. The QueryResult class is just a container for a batch of results from MongoDB.

As you mention the MongoIterable interface provides some functional methods that aim to add flexibility in consuming the data, however, they just encapsulate how the underlying iterator is consumed.

Because of the batching nature of a MongoDB cursor, I'm closing this ticket as "Works as Designed".

All the best,

Ross

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