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.