Hi everyone,
I believe that there is a memory leak when using WorkingMemoryLoggers with statelessSessions. This was raised against 5.1.1 but I think it will still exist in the latest as well.
Our code basically has the following pattern:
KnowledgeBase kb ==;
while(nextEvent()) {
StatelessKnowledgeSession session = kb.newStatelessKnowledgeSession();
auditor = new WorkingMemoryInMemoryLogger(session);
session.execute(getEvent());
dumpAuditLog(auditor);
session = null; //no dispose it’s stateless
}
We were getting Out of Memory Exception on site and the stack dump showed a high number of MemoryLoggers on the heap.
Having a look at the code I think the problem is in the WorkingMemoryLogger constructor:
} else if (session instanceof StatelessKnowledgeSessionImpl) {
((StatelessKnowledgeSessionImpl) session).workingMemoryEventSupport.addEventListener( this );
((StatelessKnowledgeSessionImpl) session).agendaEventSupport.addEventListener( this );
((StatelessKnowledgeSessionImpl) session).ruleFlowEventSupport.addEventListener( this );
((StatelessKnowledgeSessionImpl) session).getRuleBase().addEventListener( this );
}
The last statement gets the knowledge base and attaches the event listener to it.
As far as I can see there is no way to dispose of the WorkingMemoryLogger and you don’t dispose stateless sessions (and it doesn’t know about the listener anyway). This means that even after the session is gone the rule base still has a
reference to the logger.
The work around is to make the logger a singleton and clear it in each loop rather than creating a new one each time, however this obviously doesn’t scale to multiple threads.
Have I missed some fundamental in the api or should I raise a Jira for this?
Thomas