Memory leak in discord.js MessageCollector when not stopped
0 reputation · 11 Jun 2021, 17:22 UTC
Memory leak in MessageCollector
The goal is to determine whether a MessageCollector that is never stopped retains references to all collected messages, causing heap growth. In discord.js v14, collectors keep an internal Collection that is only cleared when collector.stop() is invoked. When many collectors are created for user prompts and never stopped, the process memory usage rises proportionally to the number of collected messages.
Constraints and uncertainty
• The leak is suspected only when collector.stop() or collector.dispose() is omitted.
• It is unclear if the built‑in timeout option fully releases the cache or if a manual stop is required.
• The impact on long‑running bots that spawn thousands of collectors over time is not quantified.
Specific questions for investigation
- Does invoking
collector.stop()after a successful collection guarantee that the internal Collection is garbage‑collectable, or are there still hidden references? - When relying on the collector’s timeout, does the
endevent automatically clear the cache, or muststop()be called explicitly? - What is the observed difference in
process.memoryUsage().heapUsedbetween a collector that is never stopped and one that is stopped in a finally block?