Drools insert fact 引起的内存溢出

Table of Contents

0 环境

1 调用方法

2 修改代码,自己管理插入的fact


0 环境
 

compile "org.drools:drools-templates:7.17.0.Final"

 

1 调用方法

KieSession kieSession = kieSessionsPool.newKieSession();
for (Object fact: facts){
    kieSession.insert(fact);
}
kieSession.fireAllRules();

// return session into pool
kieSession.dispose();

使用spark跑十几分钟以后,GC时间达到15分钟,过一会儿就OOM。

查了一圈文档发现,原来插入进去的fact不进行管理。只有在STREAM模式下才管理,默认启用CLOUD模式。

https://docs.jboss.org/drools/release/7.1.0.Final/drools-docs/html_single/#_memory_management_for_events

The automatic memory management for events is only performed when running the engine in STREAM mode. Check the Event Processing Mode section for details on how the STREAM mode works. 

 

2 修改代码,自己管理插入的fact

public  Collection invokeRules(Identity identity, Object... facts) throws Exception {

    KieSessionsPool kieSessionsPool = kieCache.get(identity);
    KieSession kieSession = kieSessionsPool.newKieSession();
    List factHandles = new ArrayList<>(facts.length);
    for (Object fact: facts){
        factHandles.add(kieSession.insert(fact));
    }
    kieSession.fireAllRules();

    Class resultClass = resolveResultClass(identity);
    Collection results = (Collection)kieSession.getObjects(obj -> resultClass.isInstance(obj));
    List list = handleResult(results, identity, facts);

    //ATENTION: after run ten minutes, fact object accumulated too many, so delete by manually.
    for (FactHandle factHandle: factHandles){
        kieSession.delete(factHandle);
    }
    // return session into pool
    kieSession.dispose();

    log.info("execute identity: {}, output: {}", identity, list);
    return list;
}

你可能感兴趣的:(drools)