TagKey creation leads to memory leak
The TagKey class has a memory leak when creating new TagKey s using the create method. When create is called, a new TagKey is added to VALUES, which will always hold onto the new reference. This is an issue because loading worlds with lots of custom recipes, tags, loot tables, etc. with custom tags will begin to fill this Interner up, never being dereferenced. Some commands can take a tag as a parameter, allowing many TagKey s to be created and begin to fill up memory.
Replacing VALUES with Interners.newWeakInterner() should remove this memory leak.
Test:

This is the result of the Minecraft server GUI when adding around 65536 new tags via commands. Each tag had the format execute if block 0 0 0 #minecraft:fake_tag0 as @s run say hi, with 0 being replaced by a unique number so that no two tags with the same name existed. The memory graph clearly shows a increase when the commands were being run, with level sections before and after. While the consumed memory was not much, this slight memory leak does exist.
Created Issue:
TagKey creation leads to memory leak
The TagKey class has a memory leak when creating new TagKey s using the create method. When create is called, a new TagKey is added to VALUES, which will always hold onto the new reference. This is an issue because loading worlds with lots of custom recipes, tags, loot tables, etc. with custom tags will begin to fill this Interner up, never being dereferenced. Some commands can take a tag as a parameter, allowing many TagKey s to be created and begin to fill up memory.
Replacing VALUES with Interners.newWeakInterner() should remove this memory leak.
Test:
This is the result of the Minecraft server GUI when adding around 65536 new tags via commands. Each tag had the format execute if block 0 0 0 #minecraft:fake_tag0 as @s run say hi, with 0 being replaced by a unique number so that no two tags with the same name existed. The memory graph clearly shows a increase when the commands were being run, with level sections before and after. While the consumed memory was not much, this slight memory leak does exist.
A
The bug
All tags that are created or loaded during the current game session are retained until the game is restarted. This includes tags present in one world and not another, as well as tags that have had their associated file moved/deleted/renamed in the current datapack after a reload. Possibly related to MC-248621.
How to reproduce
- Create a new entity_tag file
- Reload the datapack in game using /reload
- Delete or rename the tag file
- Reload the datapack with /reload again
→
Notice the now-missing tag is still suggested as an option when using the type selector and that the output log mentions that the the tag is not present in the datapack. - Exit the current world and open another one
→
Notice that the bug persists even in this separate world