In Minecraft Java Edition 1.21.5, memory usage increases steadily during long AFK sessions at mob grinders in both the Overworld and Nether. This behavior is likely not linked to the mob grinders but I mention it because I have not tested it elsewhere and both mob grinders use entity cramming with 24 minecarts and basic hopper collection system but are otherwise dissimilar. Over time, memory reaches high thresholds (~95–97%) and the game begins stuttering or freezing. This world was originally created in 1.21.4 and updated to 1.21.5. This issue appears to be new in 1.21.5 and did not occur under the same conditions in 1.21.4.
Critically, this memory is not released until the player travels to The End, at which point memory usage drops back to normal levels (~25–79%) and gameplay becomes smooth again. Simply moving away from loaded areas, reloading chunks, or moving from Overworld to Nether or vise versa is not enough. Quitting to the main menu and reloading the world also clears the issue.
I plan to test going AFK without loading the mob grinders and in a new world after publishing this. It is my opinion that the same memory issue will reveal itself during regular gameplay for similar length gaming sessions assuming the player never goes to The End. This world was originally created in 1.21.4 and updated to 1.21.5
UPDATE: The problem is the minecarts. The Minecarts cause this behavior regardless of if they are stacked together or separated. Steps to reproduce have been updated below.
Steps to Reproduce:
Create a new Survival world in 1.21.5.
Set up a basic AFK spot in the Overworld or Nether.
Place 24 minecarts down, either on a single rail or with each minecart having it’s own rail.
Remain AFK for several hours (e.g., 12-18 hours).
Open the F3 debug screen and observe memory usage and stuttering.
Optional: Teleport to The End and observe memory immediately drop.
Return to the Overworld and observe normal performance restored.
Code Analysis by Exopandora found [here|MC-296337 ]
Environment
Minecraft Java Edition 1.21.5 (vanilla, no mods)
Observed in both the Overworld and Nether (AFK behavior causes buildup in both)
JVM tuned for garbage collection (G1GC) and fixed heap size
Issue did not occur in the same world setup under 1.21.4
Operating System: Windows 10 64-bit
Java Version: 21.0.3
Attachments
Comments 6
UPDATE: I think this does have something to do with the mob grinders after all. I spent significant time AFK in other areas of the world with other things running and did not experience the memory creep. Then I went back to one of the mob grinders to AFK and observed the same memory creep behavior as outlined above. This proves that nothing inherent about AFKing or my world or spawn chunks is causing this, therefore it must be with whatever is consistent between my two mob grinders. They are both old Ilmango designs.
Zombified Piglin farm: https://youtu.be/P6TanacO3cA?si=S_1VllpPN7rrogiJ
Witch farm: https://youtu.be/Y-rr8I7MI1o?si=LYz4AxuNKj3sohU2
The only similarities is that they kill hostile mobs, use a modest number of hoppers, only 8 of which are open to the air in either design, and they use entity cramming with 24 minecarts (see table below)
Zombified Piglin | Witch | Similar? | |
Dimension | Nether | Overworld | FALSE |
Movement Method | Player aggro | Shifting floor + water stream | FALSE |
Item Collection | Hopper to Crafter to chest | Hopper to Shulkerbox Loader | FALSE |
Excess Item Disposal | Dropper to soulsand fire | None | FALSE |
Kill Mechanism | 24 Minecart entity cramming | 24 Minecart entity cramming | TRUE |
In order to test this further I plan to isolate the variables. Set up stacks of 24 minecarts to see if these cause the issue, and set up a mob grinder with a different, non-entity cramming killing mechanism and see what happens. I doubt the modest number of hoppers are affecting anything as I have gone AFK at other systems that use hoppers, so I feel that I have already tested those and I will only test those further if these other tests come up emptyhanded.
I have confirmed that the bug has to do with the stacked minecarts. After removing all of the minecarts, the memory started gradually going down.
Running another test to see if I get the same results with 24 minecarts that are not stacked.
Test results are in. I have the same exact memory creep problem with 24 minecarts in a stack as I have with 24 minecarts spaced apart as seen in the image below. So it isn’t a problem with minecarts colliding against each other.
[media]It seems like there is some memory issue regarding minecarts generally. I think this is the extent of my testing abilities and I leave it in other’s hands now. I will, however, update the original post to reflect the real problem.
I also faced this issue on my server. It is caused by constantly loaded minecarts (in spawn chunks for example).
Technical explanation:
The problem is that movement updates of minecarts are being appended to a list (Entity.movementThisTick
) each tick, but they are never processed or cleared. This results in ever-increasing memory usage of minecarts that are loaded for a longer time periods, eventually causing the jvm to run out of memory. The issue was introduced somewhere in the snapshot cycle of 1.21.4 to 1.21.5. Here is some pseudocode explaining the issue (using official mappings):
class Entity {
private List movementThisTick;
void move() {
// appends new entries to movementThisTick
}
void applyEffectsFromBlocks() {
// clears movementThisTick
}
void applyEffectsFromBlocks(Vec3, Vec3) {
// does not clear movementThisTick
}
}
class AbstractMinecart extends Entity {
override void move() {
if (legacy movement) {
super.move()
this.applyEffectsFromBlocks()
} else {
// experimental movement code
}
}
override void applyEffectsFromBlocks() {
if (legacy movement) {
super.applyEffectsFromBlocks(vec, vec)
} else {
super.applyEffectsFromBlocks()
}
}
}
Destroying or unloading the minecart will release the list for garbage collection. The more minecarts are loaded, the faster the memory will fill up, since the problem is per entity instance.
Suggested fix:
Insert a call to this.removeLatestMovementRecordingBatch()
in method applyEffectsFromBlocks()
of AbstractMinecart
after super.applyEffectsFromBlocks(vec, vec)
:
class AbstractMinecart extends Entity {
override void applyEffectsFromBlocks() {
if (legacy movement) {
super.applyEffectsFromBlocks(vec, vec)
+ this.removeLatestMovementRecordingBatch()
} else {
super.applyEffectsFromBlocks()
}
}
}
This removes the latest movement update batch that got added by Entity.move()
. This should be fine because the updates are not used by the legacy minecart movement code anyway.
I made a simple fabric mod to fix the issue: https://github.com/Exopandora/MinecartMemoryLeakFix
After leaving the game for close to 24 hours I got this. I sent the crash report moments before this comment.
[media]