When faced with memory leak problems, our IT consultant recommends focusing on single small actions but repeating them thousands of times.
I therefore made the following test program (c#) to exercise creation and deletion of
Google.OrTools.ConstraintSolver.Solver and IntVar objects:
class Program
{
const int nloops = 10000;
const int nvars = 1000;
const long maxval = 50;
static void Main(string[] args)
{
string name = "solver";
for (int i = 0; i < nloops; i++)
{
Google.OrTools.ConstraintSolver.Solver solver = new Google.OrTools.ConstraintSolver.Solver(name);
Google.OrTools.ConstraintSolver.IntVar[] vars = new Google.OrTools.ConstraintSolver.IntVar[nvars];
for (int j = 0; j < nvars; j++)
{
vars[j] = solver.MakeIntVar(0L, maxval);
}
for (int j = 0; j < nvars; j++)
{
vars[j].Dispose();
vars[j] = null;
}
vars = null;
solver.Dispose();
solver = null;
}
}
}
The program finishes in 7 seconds on my machine, the memory allocation during the test looks like this:
The blue and maroon colors at the bottom of the graph are the "managed" memory, i.e. that which DotNet is managing, the gray area at the top is the "unmanaged" memory, i.e. that which is allocated (among other things) by the OR-Tools c++ methods for the Solver and the objects it creates.