#Why is on_presence_update causing high memory usage?

1 messages · Page 1 of 1 (latest)

craggy cloud
#

Recently, my bot killed itself multiple times as it ran out of memory. My VPS has about 4GB of RAM and my bot was nearly using 70% of it.

After looking into all my active tasks, I found out that sometimes the bot is running thousands of on_presence_update TASKS inside the bot.

Is this code-related or intended when running the function? While going through my code, I didn't encounter a create_task function or else...

I am caching all the server (channels to send a message to and roles to give out) and user settings (whether someone opted-out or not) so I can't really understand this weird behavior lately.

Here's a short snippet:

spotify_settings = await self.get_spotify_settings(after.guild.id)

        spotify_channel = None if not spotify_settings else spotify_settings.get('spotify_channel')
        spotify_channel = before.guild.get_channel(spotify_channel) if spotify_channel else None

        spotify_role = None if not spotify_settings else spotify_settings.get('spotify_role')
        spotify_role = before.guild.get_role(spotify_role) if spotify_role else None
        ###############################################################

        twitch_settings = await self.get_twitch_settings(after.guild.id)

        twitch_channel = None if not twitch_settings else twitch_settings.get('twitch_channel')
        twitch_channel = before.guild.get_channel(twitch_channel) if twitch_channel else None

        twitch_role = None if not twitch_settings else twitch_settings.get('twitch_role')
        twitch_role = before.guild.get_role(twitch_role) if twitch_role else None

What it basically does is checking if the guild is within a defined dict at the beginning and if not, perform one DB query (which is not visible here)

late trench
#

If you have the intents the bot will do presence handling for you

#

It's a known resource consumer and so if you don't need em I'd disable the intents for it

craggy cloud
#

So better not use the Presence intent + displayed event at all? (This would destory the purpose of one of my modules though)

late trench
#

Or if you need it for your bot, then this is simply expected behaviour

#

You can install the extra speedups to help I spose but ram wise yea

#

Go do custom caching or somethin

primal siren
#

could also use cython

craggy cloud
#

Well, then I better get rid of it completely if it's that RAM consuming

#

Thanks for the heads-up