[rules-users] Garbage collection and sliding windows (Drools 5.5.0 Final)

tai-atari p00temkin at gmail.com
Sat Feb 16 19:27:47 EST 2013


Hi laune,

Really appreciate you trying to reproduce the issue. Since you are not
experiencing the same behavior I'm obviously missing something. I've
attached a very simple example below which reproduces the scenario for the
Drools versions I have tried so far: 5.2, 5.4 and 5.5. 

Basically this example runs an infinite loop which inserts 200 events about
every second, and keeps a sliding window of 10 seconds. Every loop will
print the current wm event count, which will increase to 2000 and stay put.
This is where I expected the expired events to be available for garbage
collection (since only 2000 are concurrently active in wm). But VisualVM
shows that org.drools.common.EventFactHandle uses an increasing amount of
memory over time. 

Start.java:
==================================
package org.drools.example;

import java.util.Random;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseConfiguration;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.conf.EventProcessingOption;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.rule.WorkingMemoryEntryPoint;

public class Start {

    public static final void main(String[] args) {
        try {
        	
        	System.out.println("Init");
            KnowledgeBase kbase = readKnowledgeBase(); 
            StatefulKnowledgeSession ksession =
kbase.newStatefulKnowledgeSession(); 
            WorkingMemoryEntryPoint eventStream =
ksession.getWorkingMemoryEntryPoint("TheEventStream");
			Random randGen = new Random();
			
            while( true ) {
            
	            // Insert 200
	            int x = 0;
	            while( x < 200 ) {
	                AnEvent anEvent = new AnEvent();
	                anEvent.setSource(randGen.nextInt());
	                eventStream.insert(anEvent);
	                ksession.fireAllRules();
	                x++;
	             }
	            
                System.out.println("current event count in wm" + ": " +
eventStream.getFactCount()); 
	            Thread.sleep(1000);
            }
             
            //ksession.dispose(); 

        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
	
	private static KnowledgeBase readKnowledgeBase() throws Exception {
	
	    KnowledgeBuilder kbuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder(); 
	    kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"),
ResourceType.DRL); 
	    KnowledgeBuilderErrors errors = kbuilder.getErrors(); 
	    if (errors.size() > 0) { 
	            for (KnowledgeBuilderError error: errors) { 
	                    System.err.println(error); 
	            } 
	            throw new IllegalArgumentException("Could not parse
knowledge."); 
	    } 
	
	    final KnowledgeBaseConfiguration kbConfig =
KnowledgeBaseFactory.newKnowledgeBaseConfiguration(); 
	    kbConfig.setOption(EventProcessingOption.STREAM); 
	    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbConfig); 
	    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); 
	    return kbase; 
	}
	
}
==================================

AnEvent.java:
==================================
package org.drools.example;

public class AnEvent {

    	private Integer source;
	
	    public AnEvent () {
	    }

		public Integer getSource() {
			return source;
		}

		public void setSource(Integer source) {
			this.source = source;
		}

}
==================================

Sample.drl
==================================
package org.drools.example
import org.drools.example.AnEvent; 

declare AnEvent
	@role( event )
end

rule "Event print" 
when
	AnEvent ( $src: source ) over window:time(10s) from entry-point
TheEventStream
then
	System.out.println("---------> Received AnEvent from " + $src); 
end
==================================

Also tried using an immutable pojo as you suggested cusmaimatteo, with

AnEvent.java:
==================================
package org.drools.example;

public class AnEvent {

    	private final Integer source;
	
	    public AnEvent (Integer i) {
	    	this.source = i;
	    }

		public Integer getSource() {
			return source;
		}
}
==================================

and using instead using 

eventStream.insert(new AnEvent(randGen.nextInt()));

but with the same memory issues noted.





--
View this message in context: http://drools.46999.n3.nabble.com/Garbage-collection-and-sliding-windows-Drools-5-5-0-Final-tp4022183p4022350.html
Sent from the Drools: User forum mailing list archive at Nabble.com.


More information about the rules-users mailing list