I am using the mongo-c-driver version 1.25.1 I have a process running inside a docker container which is using this driver.
I am attempting to sync all collections from one database to another. I dont have access to mongodump and mongorestore so i have to do it manually.
Here is roughly how i am doing this:
bson_t filter = BSON_INITIALIZER;
mongoc_cursor_t *cursor = mongoc_collection_find_with_opts(collectionRemote, &filter, NULL, NULL);
bson_destroy(&filter);
const bson_t *doc;
if (sync_individual_documents)
{
mongoc_cursor_set_batch_size(cursor,(uint32_t)1);
while(mongoc_cursor_next(cursor, &doc))
{
//do something
}
}
else
{
mongoc_cursor_set_batch_size(cursor,(uint32_t)1000);
while(mongoc_cursor_next(cursor, &doc)
{
//do something
}
}
mongoc_cursor_destroy(cursor);
return;
this block of code is basically executed on each collection in the DB in a for loop. These collections can have various document sizes, and various number of documents. I have verified this works how i want it to. I have run Valgrind and ASAN on this code too and it does not show any problems.
However, the while(mongoc_cursor_next()) clause seems to cause increased memory usage of my process, which i never get back even after the cursor destroy. As a result the memory continues to grow and grow.
I have checked on the mongoDb website for various mongo-c-driver releases, and i dont see anything about memory leaks when iterating over the cursor. At this point im not sure why this area of code is causing memory problems.
When this code is run, i expect the memory to increase, as it reads in more documents , then once it is done, decreases to what it was before the code is run.
if the collection sizes are small, this is true.
if the collection sizes are large in terms of either document size, or number of documents, this doesnt occur, the memory seems to grow and grow each time this code is run. If i trigger this code say 100 times, eventually it reaches the point where i run OOM. I have verified that this section of code is causing the problem, and nowhere else.
cdevto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.