Sachini Wickramaratne
Sachini Wickramaratne

Reputation: 599

Spring state machine UML stays in memory

I've been working with Spring State machines for over a year now trying different ways to implement according to my requirements, and I've come across a serious issue when I use UML.

I use papyrus to draw the UML and I have many UML stored in a certain location. The one I need to use is selected dynamically. That has been done successfully. Now I have come across a serious problem. Below is the code on how I have called the UML.

Resource resource = new FileSystemResource(stmDir+"/"+model+".uml");

        UmlStateMachineModelFactory umlBuilder = new UmlStateMachineModelFactory(resource);
        umlBuilder.setStateMachineComponentResolver(resolveActionConfig(model));
        StateMachineModelFactory<String, String> modelFactory = umlBuilder;

        Builder<String, String> builder = StateMachineBuilder.builder();
        builder.configureModel().withModel().factory(modelFactory);
        builder.configureConfiguration().withConfiguration().beanFactory(new StaticListableBeanFactory());

        stateMachine = builder.build();

And as you can see I use new UmlStateMachineModelFactory(resource);

UmlStateMachineModelFactory Class has the following code

@Override
public StateMachineModel<String, String> build() {
    Model model = null;
    try {
        model = UmlUtils.getModel(getResourceUri(resolveResource()).getPath());
    } catch (IOException e) {
        throw new IllegalArgumentException("Cannot build build model from resource " + resource + " or location " + location, e);
    }
    UmlModelParser parser = new UmlModelParser(model, this);
    DataHolder dataHolder = parser.parseModel();
    // we don't set configurationData here, so assume null
    return new DefaultStateMachineModel<String, String>(null, dataHolder.getStatesData(), dataHolder.getTransitionsData());
}

and everytime I create one UmlStateMachineModelFactory, it in turn creates one UmlModelParser.

This class has

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.uml2.uml.Activity;
import org.eclipse.uml2.uml.Constraint;
import org.eclipse.uml2.uml.Event;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.OpaqueBehavior;
import org.eclipse.uml2.uml.OpaqueExpression;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.Pseudostate;
import org.eclipse.uml2.uml.PseudostateKind;
import org.eclipse.uml2.uml.Region;
import org.eclipse.uml2.uml.Signal;
import org.eclipse.uml2.uml.SignalEvent;
import org.eclipse.uml2.uml.State;
import org.eclipse.uml2.uml.StateMachine;
import org.eclipse.uml2.uml.TimeEvent;
import org.eclipse.uml2.uml.Transition;
import org.eclipse.uml2.uml.Trigger;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.Vertex;

These remain in my memory causing it to use up a large amount of memory and doesn't get collected by the garbage collector. This is causing a lot of trouble as we are using this for a large scale application and many instances are created every few minutes.

Please suggest a workaround.

EDIT- I managed to create a singleton wrapper for this problem but regardless of that, it persists. My colleague had found out that the loaded resources do not unload. so everytime I call builder.build(),

 ResourceSet resourceSet = new ResourceSetImpl();
    resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
    resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
    resourceSet.createResource(modelUri);
    Resource resource = resourceSet.getResource(modelUri, true);

this is called. I wonder is this is causing the heap build-up. Please help

Upvotes: 0

Views: 310

Answers (1)

Janne Valkealahti
Janne Valkealahti

Reputation: 2646

I pushed some fixes per gh572 to master and 1.2.x. Hopefully those work for you. At least I was able to see garbage collection to work better. I'm planning to create releases later this week.

Upvotes: 1

Related Questions