javaspring-bootdroolskie

Drools-engine 8.29.0.Final memory issue


We use Drools in a micro service, it basically takes few rules defined by the user via UI and constructs the drl file out of a velocity template.

In this service we have below steps to fire rules. this is all in a method.

KieHelper kieHelper = new KieHelper();
kieHelper.setClassLoader(this.getClass().getClassLoader());
String drl = "drl";
kieHelper.addContent(drl, ResourceType.DRL );
Results res = kieHelper.verify();
KieBaseConfiguration kbConf = 
KieServices.Factory.get().newKieBaseConfiguration();
kbConf.setOption( EqualityBehaviorOption.EQUALITY );
KieBase kieBase = kieHelper.build( kbConf );

//above steps cached to get kiebase and then,

KieSessionConfiguration ksConf = KnowledgeBaseFactory.newKnowledgeBase().getSessionConfiguration();
      ((SessionConfiguration) ksConf).setBeliefSystemType(BeliefSystemType.DEFEASIBLE);
      ksConf.setProperty("type", "stateful");
      KieSession kieSession = kieBase.newKieSession();
     
      kieSession.setGlobal("rules", rules);
    
      Collection<KiePackage> kiePackages = kieSession.getKieBase().getKiePackages();
      
      kieSession.insert(request);
      kieSession.insert(response);
    
      try {
        kieSession.fireAllRules();
      }catch( ConsequenceException x ){
        logger.warn( "Consequence exception", x);

      }
      finally {
        kieSession.dispose();
      }

and we use below drools libs.

<dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-engine</artifactId>
            <version>${drools-engine.version}</version>
        </dependency>

        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-mvel</artifactId>
            <version>${drools-mvel.version}</version>
        </dependency>

        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-serialization-protobuf</artifactId>
            <version>${drools-serialization-protobuf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-xml-support</artifactId>
            <version>${drools-xml-support.version}</version>
        </dependency>

Now, question is,

are we following the correct steps?

are we using the right libs?

why is it taking so much memory?

there are caches in place but still its not enough. the memory is shooting up till 95% in a 20gb container.


Solution

  • are we following the correct steps?

    KieHelper is not recommended for production use. KieFileSystem is recommended. For example, "PROGRAMMATICALLY CREATE AN IN-MEMORY KJAR" in https://blog.kie.org/2021/11/drools-basic-examples.html

    are we using the right libs?

    You seem to want to use non-executable-model. Then, you should use drools-engine-classic rather than drools-engine.

    why is it taking so much memory?

    Please capture a heap dump (e.g. using Eclipse Memory Analyzer) and analyze it. This artice (https://blog.kie.org/2022/09/drools-trouble-shooting-memory-issues.html) may help.