mockserver icon indicating copy to clipboard operation
mockserver copied to clipboard

MemoryLeak in LRUCache

Open eric6iese opened this issue 2 years ago • 1 comments

Describe the issue The field org.mockserver.cache.LRUCache.allCaches poses at least a memory leak, perhaps even a threadsafety issue.

What you are trying to do We have a long running testsuite where we create and stop multiple ClientAndServer Instances in Java. There is never more than two or three active instances around, but the memory was increasing. In VisualVM i could dected that this class holds even the stopped HttpState-Objects in the LRUCache, which held a lot of LogEntry instances, even though the EventLog in MockServer was cleared. The reason for this is the internal disruptor which keeps hold of these instances - this is by design.

The leak in LRUCache is the private static field allStates. It is used for nothing else than a static method LRUCache.clearAllCaches(), but it does not dected if those instances are even in use anywhere. Also I am not sure if this is even threadsafe, it isn't if multiple constructors of LRUCache could be called in parallel.

MockServer version 5.14, but I see no changes in the current implementation.

To Reproduce Simple - create multiple ClientAndServer instances, do some interaction, stop them and watch what happens in the LRUCache with the debugger

**Fix ** The most simple fix would be to change the declaration to

private static final Set<LRUCache<?, ?>> allCaches = Collections.newSetFromMap(new WeakHashMap<>());

In addtion, synchronized blocks would be needed around the interactions.

eric6iese avatar Jan 20 '23 08:01 eric6iese

I see it's fixed by https://github.com/mock-server/mockserver/pull/1648 Is the release date known?

asolntsev avatar Feb 13 '24 18:02 asolntsev