1

Is there a solution to avoid memory leak in a simple expression evaluation like this ?

        inter.SetVariable("tick", tick++);
        if (inter.Eval<bool>("(tick%2)==1"))
        {
            odd++;
            if ((odd % 100) == 0)
                System.GC.Collect();
        }
        else
            even++;

I'm running this code periodically in a WinForm application on a Linux machine with Mono (5.0.1.1) and the memory usage continuously increase. Tested on Windows the Process.WorkingSet64 was increasing with a lower rate than Linux. The GC.GetTotalMemory is always stable.

Ezio R.
  • 13
  • 2

1 Answers1

0

If possible, it is better to use the Parse method and then just Invoke the expression multiple times.

Something like:

// One time only
string expression = "(tick%2)==1";
Lambda parsedExpression = interpreter.Parse(expression, new Parameter("tick", typeof(int)));

// Call invoke for each cycle...
var result = parsedExpression.Invoke(tick++);

But from my previous tests I don't have see any Memory Leak, are you sure that is this the problem?

Davide Icardi
  • 11,919
  • 8
  • 56
  • 77
  • I did the test again with my posted code and after 30K cycle I lost 17MB. After changing the code with your suggestion, I have no memory leak with the same number of cycles. My problem now is that I have a set of variable and a set of expression and I don't know which variable are used in the expression. That was perfect solved by the SetVariable/Eval duo: do you have any suggestion on how to use Invoke in this case ? – Ezio R. Aug 18 '17 at 07:45
  • @EzioR. The set of variables are fixed or dynamic? In the first case you can just create the expression with all the parameters that you have. In the second case maybe you can add a single parameters of type Dictionary that will be populated according? So you can write something like: `vars["a"]` . Where `vars` is your dictionary? – Davide Icardi Aug 18 '17 at 09:40