Python · Memory
Python bot "Killed" (exit code 137)
No traceback, no error, just "Killed". The operating system stopped your bot because it used more memory than it is allowed. Python never got the chance to say anything.
What you are seeing
Console
KilledExit status
exited with code 137What 137 means
Exit code 137 is 128 plus 9, and 9 is SIGKILL: the process was ended from outside. On a server with a memory limit, that is almost always the limit being reached. Because the process is stopped rather than failing, there is no Python exception to catch.
In a discord.py bot
discord.py caches the last 1,000 messages by default, and with the members intent it downloads and keeps every member of every server at startup. On a bot in a few large servers, that is most of the memory. Turn off what you do not use.
discord.py
import discord
client = discord.Client(
intents=discord.Intents.default(),
max_messages=100, # or None to keep no message cache
member_cache_flags=discord.MemberCacheFlags.none(),
chunk_guilds_at_startup=False,
)Everywhere else
Reading a whole file or API response into memory, a list that is appended to and never cleared, and heavy libraries imported for one function (pandas is the usual example) are the other common causes. Python's tracemalloc module shows which lines allocate the most; log its top entries every few minutes and look for the one that keeps growing.
A small Python bot fits comfortably in the 256 MB our free servers have. If yours has genuinely outgrown it, the paid plan gives it more room on the same server.
FAQ
Questions
Why does it happen after hours rather than at once?
Something grows with time: a cache, a list, or messages from more and more servers. Memory that climbs steadily and never falls is the thing to find.
Can I catch it and restart?
Not from inside Python, because the process is stopped outright. Our servers restart a crashed bot automatically, but it will be killed again at the same point until the memory use is fixed.