diff --git a/discord/state.py b/discord/state.py index 622a47808..2c3021977 100644 --- a/discord/state.py +++ b/discord/state.py @@ -33,6 +33,7 @@ import logging import math import weakref import inspect +import gc from .guild import Guild from .activity import _ActivityTag @@ -117,6 +118,11 @@ class ConnectionState: self._private_channels_by_user = {} self._messages = self.max_messages and deque(maxlen=self.max_messages) + # In cases of large deallocations the GC should be called explicitly + # To free the memory more immediately, especially true when it comes + # to reconnect loops which cause mass allocations and deallocations. + gc.collect() + def process_listeners(self, listener_type, argument, result): removed = [] for i, listener in enumerate(self._listeners): @@ -210,6 +216,10 @@ class ConnectionState: del guild + # Much like clear(), if we have a massive deallocation + # then it's better to explicitly call the GC + gc.collect() + @property def emojis(self): return list(self._emojis.values())