CustomNPCs MarkData Leak Fix
Fixes a memory leak in CustomNPCs-Unofficial (NeoForge 1.21.1) that can crash your server with OutOfMemoryError.
This is an unofficial fix by a third party. It is not part of CustomNPCs and has no affiliation with its authors.
Do I need this?
If you run CustomNPCs-Unofficial on NeoForge 1.21.1, yes, especially on a server.
The symptoms look like this:
- Memory use climbs for days, even with nobody online.
- Lag builds up over hours ("Can't keep up!"), and then the server crashes with
java.lang.OutOfMemoryError: Java heap space, or the watchdog kills it. - It's at its worst while pre-generating the world (Chunky and similar) or while players explore new land. We measured about 1 GB of heap per hour during a pregen.
What's wrong
CustomNPCs keeps an internal cache of "mark" data for living entities. It adds an entry every time a mob is saved, which happens on every chunk unload and every autosave, and every time a mob is shown to a player. It never removes one. Each entry holds on to the whole mob, including its AI, attributes and every other mod's data on it, so unloaded mobs can never be freed.
We found it on a 31 GB server that kept crashing during a pregen. A heap dump showed about 53,000 mobs in memory while only about 500 chunks were loaded, and every one of them was held by that cache.
What this mod does
- It makes the cache thread-safe. CustomNPCs uses it from several threads without locking.
- It drops an entry once its mob has left the world: unloaded, killed, or changed dimension. A check every 10 seconds catches anything else.
Nothing CustomNPCs could still use is lost. A mob that comes back gets a new ID and a new entry anyway. Entries for mobs that are still loaded are never touched, so their marks stay put.
Details
- Server, client or both. Clients leak the same way, just more slowly. Install it on both if you can.
- No configuration. Drop it in
mods/. - Safe without CustomNPCs: it does nothing.
/cnpcmarkfix(op level 2) shows the status, how many mobs are cached right now, and how many have been released.- Tested with CustomNPCs-Unofficial
1.21.1.20251230on NeoForge21.1.247.
Tested
This was checked on a headless NeoForge test server with the real CustomNPCs jar. 200 cows were unloaded the normal way:
| unloaded mobs still in memory | marked mob that stays loaded | |
|---|---|---|
| CustomNPCs alone | 201 of 201 | kept |
| with this fix | 0 | kept, mark intact |
Note for CustomNPCs users
While testing, we noticed that this CustomNPCs build doesn't save marks into the mob's save data. A mark on a mob is lost once that mob unloads, with or without this fix. That's CustomNPCs' own behaviour, and this mod doesn't change it.
License
MIT. Source code: see the GitHub link on this page. This mod contains no CustomNPCs code; it adjusts the cache at runtime.