DJS Caching Support
So I've been having a few memory issues lately and I've been taking memory snapshots. I saw that some of that data is just taken over by "Stage Channels". I was told about extra caching options but I rather speak to someone here that can assist me more than me having to deal with something I don't know.
Can I get rid of stage channels from cache because my bot cannot even join them or is there a reason to keep it? This is what I see in memory snapshots but I don't really know how this stuff works so if someone could let me know on what I could do, it would help a ton
Thanks
99 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.If needed, node version is 19.4.0
Set the
makeCache
option in the client's options.I saw it but I don't know what the best optimisation were
You can use cacheWithLimits to limit objects in the cache
If I don't mind, can you show me how it's done?
makeCache: Options.cacheWithLimits(...)
(static) Options.cacheWithLimits()
Create a cache factory using predefined settings to sweep or limit.
Isn't that bad tho, I don't know what I should cache or what I shouldn't
Caching voice channels and stage channels I guess is not needed since my bot does nothing with it I guess?
I'll just going as per this image
I don't know why djs ends up building on memory but it could be something on my end too
You could also set up sweepers
To filter out voice channels and stage channels
Would I need voice channels or stage to begin with though?
would the text channels inside a voice channel count as a voice channel?
They would, yes
because caching stage channels for me has no use as my bot won't ever join them so that's not needed I guess?
What happens if commands were ran there but they bot didn't cache that?
it would be fetched from the api directly isn't it?
Depends on what you need to do with the command ig
Send an image
that's about it
You shouldn't need it cached
If it's the interaction reply
command is ran and images or embeds are sent
yes it's an interaction reply
nothing to do with managing channels
same with users, my whole bot just replies to interactions
Yeah then it doesn't need to be cached
So I don't know if I need users cached
I need guilds bc of guild count
I don't wanna not cache something I should but I also don't wanna cache things I don't need
Oh wait
but that's why I wanna speak like this instead of actually doing anything myself and breaking everything
Actually, you can't even override channel cache
I just remembered
wdym?
Djs doesn't let you
I only use guild cache for guild count
So does your bot do anything that requires a bot user?
requires a bot user?
Or is it just interaction replies and follow ups
pretty much yes
or sending messages to webhooks
hmm
I don't think you even need to invite with bot scope
That will also prevent any channels or guilds from caching
oh?
What is counted as the bot scope then?
Well you would need the bot scope for actions that actually need permission
Like sending a message or adding a role
yeah that
the bot don't add roles either
only replying to interactions
Does it need to access channels ever?
Though future plans is to sending messages to channels and update it automatically
or members, roles, messages?
so that would require it
not really no
I mean we need access to member data
but that's inside the interaction so technically we don't
if it's from an interaction option, it's all good
yeah
yeah then no
we just do statistics so
just send images & embeds
no roles or nothing
bc atm everything works but I surely have some memory leak
you may need some refactors
and djs might be just growing in memory but I doubt since no one seems to mention it
some djs structures will become API structures
so I don't know if it's djs but I have snapshots and that image has a large size for "client"
example
GuildMember -> APIGuildMember
ah
is it normal for djs to use that much memory though?
Idk
Depends on your intents and bot size
very small bot
about 2k servers
only have guilds intent
bc of guilds create and guilds delete
You need those events?
Yeah
I log guilds created and deleted so I definitely don't wanna give up on that
Yeah I'm pretty sure the bot would need to be invited with the bot scope for that
yeah that's fine
I don't know what causes the memory overload, do you know what those snapshots mean?
from what I can understand, client takes 100mb of memory?
the
array
takes like 77mb
but I'm unsure, I've never used that before
Any idea on what I could do here?
My memory just gradually goes up and it's so hard to understand where the memory leak is coming from
Upon looking at memory snapshots, discord.js was just using a ton of it which is why I wanted to reduce it but off topic, any tips to find memory leaks?
For a bot with 2k guilds, I'd look at the memory usage to be around 400mb or so
however I sit at ~250-300mb upon startup which is fine but then it gradually increasesUnknown User•2y ago
Message Not Public
Sign In & Join Server To View
I mean yeah I do see it but then wouldn't it mean that with due time my bot would just crash due to OOM?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
if it doesn’t skyrocket and/or keep going up by large amounts over a long period then you should be fineYeah it doesn't go up by large amounts, its a very slow increase if anything yeah
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I'd say around 2 days to hit 1 gb
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
although even on day 9, it never really hits 1 gb, its just stuck at like 980mb or smth
Yeah but caching things that I dont need feels unnecessary as well
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
like why cache stage channels when my bot cannot even join them?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
What is "messages"
If a bot doesn't have message content, how does that work
via @ mention ?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
nope
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
actually, i think so
I have "Guilds" so yeah I guess it would include that
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
That's just for guild create & delete tho
yeah pretty much, my bot barely needs anything
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
works fully based on interactions
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
wdym?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
yeah thats just the index.js, i only create the client once
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
when u say odd, is it meant to be higher or lower
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I don't know
It could just not be djs either but memory snapshots legit just show arrays of stage channels
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
and for some reason 2 clients?
not all 900mb is djs obv
I do image stats so we do cache some stuff in memory to load up quicker
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
but thats nothing close to taking up 900mb anyhow + thats all done on startup
djs is discord.js... 😅
now idk if this snapshot is accurate because
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I don't know, not something I can track
I used this npm that did profiling every 6 hours but this npm just made my 300mb fly to 900mb in 30s however i didnt care much about that because without any profiling, it still goes up to 900mb over time
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I just wanted to know what is taking up so much memory
or atleast, whats piling up that I should be aware of
because if things flow in and out, the memory cannot just gradually increase
I don't cache images fully, just backgrounds and thats on startup
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
it doesn't stop but I restart the bot at around 1 gb usage everytime
but it has been online for 7d or so and been at around 900+mb without crashing
which is not something I mind but more of just whats the cause for it
This is a snapshot that was taken like 1 day into the bot being online
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
oh I just hit 2k like today
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
this has been an issue for a few months now but I've never bothered much until now
ty :)
I think its 2.5k
and whats broadcastEvals?
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
I mean I don't even mind the usage
It's just I've seen other bots with higher guilds use lower memory but then again, diff purpose bots and stuff
its not pleasing knowing ur bot's memory just gradually goes up because it removes that peace of mind that I do have to monitor it regardless of the fact that it would auto start, its just not ideal i guess
gaining 600mb over time but for what is pretty much what I want to know
Right, I'll look into that