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
d.js toolkit
d.js toolkit2y ago
• 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.
Gui
GuiOP2y ago
Using [email protected] and node v16.17.1
Gui
GuiOP2y ago
monbrey
monbrey2y ago
By default discord.js utilises quite a lot of cache, but you have the ability to control this
d.js docs
d.js docs2y ago
guide Miscellaneous: Cache customization read more
Gui
GuiOP2y ago
Can you tell me what exactly I should limit? Messages, guilds? What is making each shard to use 500mb, where can see that?
monbrey
monbrey2y ago
monbrey
monbrey2y ago
Not Guilds. Messages yes, members, users, presences Node has memory profiling tools
Gui
GuiOP2y ago
I limited several managners and still got a bad result (7.5g), any idea why?
const client = new Client({
makeCache: Options.cacheWithLimits({
ReactionManager: 250,
UserManager: 250,
GuildMemberManager: 250,
MessageManager: 250,
MessageContent: 250,
GuildEmojiManager: 250,
ThreadManager: 0,
PresenceManager: 0,
VoiceStateManager: 0,
AutoModerationRuleManager: 0,
BaseGuildEmojiManager: 0,
GuildBanManager: 0,
GuildInviteManager: 0,
GuildTextThreadManager: 0,
ReactionUserManager: 0
}),
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
allowedMentions: { parse: ['users', 'roles'] }
});
const client = new Client({
makeCache: Options.cacheWithLimits({
ReactionManager: 250,
UserManager: 250,
GuildMemberManager: 250,
MessageManager: 250,
MessageContent: 250,
GuildEmojiManager: 250,
ThreadManager: 0,
PresenceManager: 0,
VoiceStateManager: 0,
AutoModerationRuleManager: 0,
BaseGuildEmojiManager: 0,
GuildBanManager: 0,
GuildInviteManager: 0,
GuildTextThreadManager: 0,
ReactionUserManager: 0
}),
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
allowedMentions: { parse: ['users', 'roles'] }
});
monbrey
monbrey2y ago
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
Gui
GuiOP2y ago
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:
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
allowedMentions: { parse: ['users', 'roles'] },
messageEditHistoryMaxSize: 0,
messageCacheMaxSize: 0,
messageCacheLifetime: 21600,
messageSweepInterval: 21600,
makeCache: Options.cacheWithLimits({
ReactionManager: 25,
UserManager: 25,
GuildMemberManager: 25,
MessageManager: 25,
MessageContent: 25,
GuildEmojiManager: 25,
ThreadManager: 0,
PresenceManager: 0,
VoiceStateManager: 0,
AutoModerationRuleManager: 0,
BaseGuildEmojiManager: 0,
GuildBanManager: 0,
GuildInviteManager: 0,
GuildTextThreadManager: 0,
ReactionUserManager: 0
}),
});
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
allowedMentions: { parse: ['users', 'roles'] },
messageEditHistoryMaxSize: 0,
messageCacheMaxSize: 0,
messageCacheLifetime: 21600,
messageSweepInterval: 21600,
makeCache: Options.cacheWithLimits({
ReactionManager: 25,
UserManager: 25,
GuildMemberManager: 25,
MessageManager: 25,
MessageContent: 25,
GuildEmojiManager: 25,
ThreadManager: 0,
PresenceManager: 0,
VoiceStateManager: 0,
AutoModerationRuleManager: 0,
BaseGuildEmojiManager: 0,
GuildBanManager: 0,
GuildInviteManager: 0,
GuildTextThreadManager: 0,
ReactionUserManager: 0
}),
});
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.
probablyraging
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
Gui
GuiOP2y ago
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
probablyraging
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
Gui
GuiOP2y ago
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)
Gui
GuiOP2y ago
Your theory and idea sounds good, I'll do a better test on a server with more ram.
Kevinnnn
Kevinnnn2y ago
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 Kek 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
d.js docs
d.js docs2y ago
guide 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
dzlandis
dzlandis8mo ago
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 🙏
Gui
GuiOP8mo ago
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.
dzlandis
dzlandis8mo ago
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:
makeCache: Options.cacheWithLimits({
MessageManager: 0,
GuildInviteManager: 0,
GuildEmojiManager: 0,
GuildStickerManager: 0,
GuildBanManager: 0,
GuildScheduledEventManager: 0,
ReactionUserManager: 0,
BaseGuildEmojiManager: 0,
AutoModerationRuleManager: 0,
ThreadManager: 0,
ThreadMemberManager: 0,
GuildTextThreadManager: 0,
GuildForumThreadManager: 0,
GuildMemberManager: {
maxSize: 30,
keepOverLimit: member => member.id === member.client.user.id
}
}),
sweepers: {
...Options.DefaultSweeperSettings,
users: {
interval: 3600, // Every hour...
filter: () => (user: User) => user.id !== user.client.user.id // Remove all users except for bot.
}
}
makeCache: Options.cacheWithLimits({
MessageManager: 0,
GuildInviteManager: 0,
GuildEmojiManager: 0,
GuildStickerManager: 0,
GuildBanManager: 0,
GuildScheduledEventManager: 0,
ReactionUserManager: 0,
BaseGuildEmojiManager: 0,
AutoModerationRuleManager: 0,
ThreadManager: 0,
ThreadMemberManager: 0,
GuildTextThreadManager: 0,
GuildForumThreadManager: 0,
GuildMemberManager: {
maxSize: 30,
keepOverLimit: member => member.id === member.client.user.id
}
}),
sweepers: {
...Options.DefaultSweeperSettings,
users: {
interval: 3600, // Every hour...
filter: () => (user: User) => user.id !== user.client.user.id // Remove all users except for bot.
}
}
My bot also uses voice features (hence why VoiceStateManager is left untouched).
Gui
GuiOP8mo ago
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.
dzlandis
dzlandis8mo ago
Unfortunate yet expected. I appreciate the additional info. Out of curiosity, how many shards do you have per cluster?
Gui
GuiOP8mo ago
60 shards in 6 clusters, 10 shards per clusters check your dm please
dzlandis
dzlandis8mo ago
Good to know
Want results from more Discord servers?
Add your server