Memory usage

Skip to first unread message

Erin Loy

Jan 26, 2020, 3:13:38 PM1/26/20
to NRules Users
I'm new to NRules (very cool project), and I've been playing around with a simple rule that adds two values.  I've noticed that when I use a single repository many times in a loop, the process memory usage grows indefinitely, even though I'm removing the facts after each operation.  When I recreate the repository in each loop, memory does not grow.

Here my test code that demonstrates this:

It seems to be related to executing a query against the session to retrieve the result.


2020-01-26 12_07_55-NRulesTest - Microsoft Visual Studio.png

2020-01-26 11_35_57-Clipboard.png

Sergiy Nikolayev

Feb 13, 2020, 12:24:43 AM2/13/20
to NRules Users
This is interesting. I profiled it, and it turns out it is this line in Processor.Add method:
return session.Query<Property<T>>().Single();

ISession.Query returns IQueryable, which uses Expressions. So, it gets compiled in a tight loop, creating various objects related to IL generation.
When you moved the processor inside the tight loop, it became so slow (as it needs to compile the rules and build the session factory) that you no longer noticed the footprint growing.
It also does not really leak memory, but all these objects sit in a finalizer queue. So, if memory pressure got really high, all that stuff would have gotten reclaimed.
Reply all
Reply to author
0 new messages