spring-projects / spring-statemachine

Spring Statemachine is a framework for application developers to use state machine concepts with Spring.
1.57k stars 616 forks source link

UmlModelParser leak memory. #798

Open jliew1975 opened 5 years ago

jliew1975 commented 5 years ago

Everytime a state machine is build the code create a UmlModelParser and parse the UML to get reference to DataHolder. However, instances of StateMachineImpl within the model are not release or garbage collected when GC kicked in results in memory leak. I have a workaround on this currently where I destroy the instance of UmlModelParser when DataHolder is created which at the end clean up instances of StateMachineImpl within the model. Also to improve performance I have cache the DataHolder for subsequence build of the same UML model using resource filename as the key. Hope you can see the issue and fixed this for others so they don't struggle like I did.

jvalkeal commented 5 years ago

Would you like to show some code you have or even better create a PR?

jliew1975 commented 5 years ago

Ok will follow up on this. later and create a PR.

jliew1975 commented 5 years ago

Didn't have time to create a pull request but below is the snippet code I have for fixing the memory leak.

// clean up
if (emfResource != null) {
    try {
        Collection<StateMachine> stateMachines =
            EcoreUtil.getObjectsByType(emfResource.getContents(), 
                                 UMLPackage.Literals.STATE_MACHINE);
        stateMachines.forEach(sm -> sm.destroy());

        model.destroy();
        emfResource.unload();
    } catch (Exception e) {
    }
}