How to reduce your Discord bot's memory usage
Lower RAM use for discord.js and discord.py bots with fewer intents, cache limits, sweepers and leak checks, so they fit Schokr's free resource pool.
Updated · 2 min read
Most of a Discord bot's memory goes to its cache of servers, members, messages and presences. Requesting fewer intents and limiting caches is the biggest win. A small bot often runs in 100 to 200 MB, so if yours uses far more, something is being cached or leaked.
Check actual usage first
Open your server's Resources view on Schokr and watch memory over a day:
- Flat after startup: normal. Set the server limit a bit above that.
- Climbs steadily and never drops: a leak or unbounded cache. Read on.
- Spikes on certain commands: that command loads too much at once (big files, images, full member lists).
If memory reaches the server's limit, the process is killed and the bot goes offline. See bot keeps going offline.
1. Request only the intents you use
Each intent means more events and more cached data. The presence and members intents are the most expensive. If you don't use them, remove them. See privileged intents.
2. discord.js: limit caches and add sweepers
const { Client, GatewayIntentBits, Options } = require('discord.js');
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
makeCache: Options.cacheWithLimits({
...Options.DefaultMakeCacheSettings,
MessageManager: 50,
ReactionManager: 0,
PresenceManager: 0,
GuildMemberManager: {
maxSize: 200,
keepOverLimit: (member) => member.id === member.client.user.id,
},
}),
sweepers: {
...Options.DefaultSweeperSettings,
messages: { interval: 3600, lifetime: 1800 },
},
});
Don't set the cache for core structures like guilds or channels to zero; discord.js relies on them. If code needs an uncached member or message, fetch it with guild.members.fetch(id) or channel.messages.fetch(id).
3. discord.py: limit the message cache and member chunking
intents = discord.Intents.default()
bot = commands.Bot(
command_prefix="!",
intents=intents,
max_messages=200, # default is 1000; None disables the message cache
chunk_guilds_at_startup=False, # don't download every member list on startup
member_cache_flags=discord.MemberCacheFlags.from_intents(intents),
)
With the members intent on, chunk_guilds_at_startup=False stops the bot loading every member of every server at login, which is a big saving in large servers. Fetch members on demand instead.
4. Find leaks in your own code
Common culprits:
- Arrays, Maps or dicts that only ever grow (cooldowns, "seen users", per-message state). Delete entries when done or use a size limit.
- Event listeners added inside a command, which pile up each time it runs.
setIntervalortasks.loopstarted every timereadyfires. Useoncein discord.js and start loops insetup_hookor a cog in discord.py.- Loading whole files, images or API responses into memory when you only need a part.
5. Node.js heap limit
Node.js sets its own heap limit, which may not match your server's memory. If your server has 512 MB, you can keep Node below it so it collects garbage earlier instead of being killed:
node --max-old-space-size=384 index.js
Leave headroom (Node uses memory outside the heap too). Set this in your start script or your server's Startup settings if the template allows a custom command.
6. Give it the right amount
Sometimes the bot simply needs more. Your team shares one 6 GB pool, so you can move memory from a server that doesn't need it. Store upgrades add 1 GB permanently for 450 credits.