Immersive Engineering

Immersive Engineering

134M Downloads

[1.12] FakePlayer cache leaks worlds

bs2609 opened this issue ยท 0 comments

commented

1ba6025 added a Map<World, FakePlayer> cache to FakePlayerUtil here:

private static Map<World, FakePlayer> fakePlayerInstances = new WeakHashMap<>();

The issue here is that while a WeakHashMap is used, that is not sufficient handling to prevent memory leaks as each FakePlayer holds its own reference to the world. Note this excerpt from the WeakHashMap Javadoc:

Implementation note: The value objects in a WeakHashMap are held by ordinary strong references. Thus care should be taken to ensure that value objects do not strongly refer to their own keys, either directly or indirectly, since that will prevent the keys from being discarded.

One way to deal with this is by additionally wrapping the FakePlayer in a WeakReference when storing them.

It would also be ideal to listen to WorldEvent.Unload and remove worlds from the map there, rather than just relying on GC-based eviction.