D.js bot using 8gb ram
A few months ago I'm rewriting my old bot in python for discord.js
I made the sharding system according to the guide, always using all functions to search for data between shards.
I tried to migrate the bot today and my server crashed because the bot exceeded 8gb of memory.
I believe that a bot with 48 shards, 50.000 guilds should not be using so much memory.
If you have any ideas where I went wrong? please let me know
25 Replies
• What's your exact discord.js
npm list discord.js
and node node -v
version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.Using [email protected] and node v16.17.1
By default discord.js utilises quite a lot of cache, but you have the ability to control this
Can you tell me what exactly I should limit? Messages, guilds?
What is making each shard to use 500mb, where can see that?
Not Guilds. Messages yes, members, users, presences
Node has memory profiling tools
I limited several managners and still got a bad result (7.5g), any idea why?
No, I don't really deal with any large bots. I recommend doing the memory profiling
GuildMemberManager would be 250 per guild, that could be it
Reactions are per message
Changed all 250 to 50 and get same result
Tomorrow will try make memory profiling
I had some free time to development the bot again, I didn't get a solution
Today I tried to apply a heavier cache and I didn't get good results
Seems to have no effect, to be honest
New cache policy:
I wonder if I'm putting the configuration in the right place, this is the file that creates the client
If anyone has any ideas beyond the cache, it would be most welcome.
That looks fine, probably stricter than needed. If that isn't solving the issue with RAM, then it might be coming from somewhere else in your code? Are you storing a lot of data in variables, storing duplicate data, or repeating operations frequently maybe?
I would start dissecting your code to trouble shoot
Then again, I have a bot running on a single server using 150MB memory on average. So 8GB for a bot your size might not be too unrealistic
I may be confusing things in discord.py, I'm migrating from there due to the end of discord.py
In d.py with 50k servers and with heavy usage I get 4.4gb of ram
I'm scared to get double in discord.js (50k with idle bot (no players uses), aprox 7gb and goes up quickly)
Do you recommend any specific tools to help me with this? there doesn't seem to be anything wrong with the code
i tryed heapdump, but it seems obsolete and not work
Doubling the memory usage does seem excessive for sure. But hard to say as there are so many differences in the way the libraries are implemented and the programming languages they are written in.
I've not really used heapdump so I can't give advice there. You could try the --inspect flag in node.js?
A good way to test if it is a memory leak would be to temporaily increase your server's memory capacity substantially, maybe 32gb and see if the process still has issues. If you see it flatten at around 8-12gb or something then you can sort of rule out a memory leak
But yeah, without doing a proper deep dive into your entire project, would almost be impossible to diagnose it
i have results after run with inspect, I left it running for a few moments, with no user interaction, and saw usage go up from 6.8gb to 7.2gb in around 20 minutes (test locally in my computer)
Your theory and idea sounds good, I'll do a better test on a server with more ram.
this is probably the bot receiving more presences (if you have presences enabled), or updated voice states, messages, etc..
oh wait you have a cacheFactory is probably the caches being filled from different channels/guilds
also, you are using v13 client options for v14, sweepers were moved to the sweepers option
Miscellaneous: Sweeping caches
To change the sweep behavior, you specify the type of cache to sweep (SweeperKeyopen in new window) and the options for sweeping (SweepOptionsopen in new window). If the type of cache has a lifetime associated with it, such as invites, messages, or threads, then you can set the lifetime option to...
read more
Hi @Gui, did you ever figure this out? I have bot with relatively the same amount of guilds and have been running into similar issues with RAM usage. Any insight is greatly appreciated 🙏
Basically, you have two tools at your side:
- Limit cache of your bot (like this https://discord.com/channels/222078108977594368/1091587729894154260/1093318511289434113)
- discord-hybrid-sharding (https://www.npmjs.com/package/discord-hybrid-sharding)
Check hybrid-sharding, it saved me 4gb of memory. Play with the maximum number of clusters and shards, this also influences 😉
Discord
Discord - A New Way to Chat with Friends & Communities
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
npm
discord-hybrid-sharding
The first package which combines sharding manager & internal sharding to save a lot of resources, which allows clustering!. Latest version: 2.1.9, last published: 13 days ago. Start using discord-hybrid-sharding in your project by running
npm i discord-hybrid-sharding
. There are 8 other projects in the npm registry using discord-hybrid-sharding.Thanks for the response. Luckily, I'm already using discord-hybrid-sharding in combination with limit cache options. However, I'm still facing issues with RAM increasing when the bot is online for greater periods of time, in which I've set it up so it does a rolling restart to try and reduce the RAM. Still, it hasn't been working super well, and it ends up doing a hard restart sometimes presumably do to running out of RAM (though I've still been trying to debug why exactly this occurs). Have you faced any similar issues with RAM increasing over time, and do you have any suggestions with how to go about resolving this? Or is this just expected behavior with a bot in roughly 55k guilds? My bot gets up to roughly 6.2gb of RAM and that's the cutoff I've set for the RAM for a rolling restart to be triggered. Perhaps I just need more RAM 🤷♂️
Let me know your thoughts :)
Oh, and I have 6 clusters with 10 shards per cluster currently
Here is what my current limit cache looks like:
My bot also uses voice features (hence why VoiceStateManager is left untouched).
I believe that DiscordJs is terrible at using RAM, due to its high cache dependency. Even with the limits that we can change (cacheWithLimits), we still have a lot of problems.
Today I have 80k guilds and bot is using 12gb of ram, I believe you have a cool result, considering that we are on discord js.
Unfortunate yet expected. I appreciate the additional info. Out of curiosity, how many shards do you have per cluster?
60 shards in 6 clusters, 10 shards per clusters
check your dm please
Good to know