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.
If you run CustomNPCs-Unofficial on NeoForge 1.21.1, yes, especially on a server.
The symptoms look like this:
java.lang.OutOfMemoryError: Java heap space, or the watchdog kills it.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.
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.
mods/./cnpcmarkfix (op level 2) shows the status, how many mobs are cached right now, and how many have been released.1.21.1.20251230 on NeoForge 21.1.247.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 |
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.
MIT. Source code: see the GitHub link on this page. This mod contains no CustomNPCs code; it adjusts the cache at runtime.