DocsBest practices

Reducing memory usage

Simple fixes that often halve your RAM.

Best practices 4 min read

Lower memory means a cheaper plan and fewer out-of-memory restarts. These are the fixes that make the biggest difference.

Limit caches

discord.js caches every member and message by default. On large servers this grows quickly. Restrict what is cached:

js
import { Client, Options } from 'discord.js';

const client = new Client({
  intents: [/* only what you need */],
  makeCache: Options.cacheWithLimits({
    MessageManager: 50,
    GuildMemberManager: 200,
  }),
});

Other quick wins

  • Stream large files instead of loading them fully into memory.
  • Paginate database queries rather than fetching every row.
  • Remove unused dependencies — some load a lot at startup.
  • Set NODE_ENV=production: many libraries use less memory in production mode.
A steadily rising memory graph that never goes down usually means a leak, such as event listeners added on every command.
Was this article helpful?