I have somewhat similar concerns to @diegoolivier.
I think there should be a way to de-initialize collections so that they can be garbage collected.
My use-case is different to @diegoolivier’s though.
What @diegoolivier is doing in terms of multiple distinct variables creating distinct collection objects, but with a shared/identical collection name, seems strange and one might argue there are problems with that approach… and it’s definitely not something that I would do personally.
This is what I’m doing:
I’m building an application where collections are generated dynamically, there’s no limit to the number of collections, but the collections that need to be accessed by the server/client depends on who is logged in and what they’re doing.
So instead of instantiating “a million” collection objects at startup, I’ll instantiate the specific collection objects that are needed, when they are needed.
However, it would be my preference to destroy the collection objects with a timer when they’ve not been used for a long time (an hour, a day, etc).
But currently there is no obvious way to do that, and so collection objects that are no longer needed, can never get garbage collected.
So for example if I do this:
allCols = {};
let collectionThatWeNeedRightNow;
collectionThatWeNeedRightNow = 'foo'; // this is dynamic
allCols[collectionThatWeNeedRightNow] = new Mongo.Collection(collectionThatWeNeedRightNow);
// some time later, kill it so the garbage collector can free memory
delete allCols[collectionThatWeNeedRightNow];
// okay we need it again
collectionThatWeNeedRightNow = 'foo'; // this is dynamic, we happen to need the same collection again
allCols[collectionThatWeNeedRightNow] = new Mongo.Collection(collectionThatWeNeedRightNow);
Error: There is already a collection named "profile"
at new Collection (packages/mongo/collection.js:122:15)
So as packages/mongo/collection.js works right now, because there is no known way to delete collection objects from memory. What will eventually happen is my node.js instance will be unable to garbage collect, collections will be “opened” as needed, but never “closed”, then eventually node.js will get killed by the kernel (OOM out of memory) or self-kill based on node.js memory limits and get restarted by the process manager.
I think there’s no justifiable reason that there shouldn’t be a way to destroy a collection object when it’s not needed.