In my Java code, I'm invoking Z3 in a loop to check a formula (which depends on the loop index and is different for every iteration) until the formula becomes satisfiable, as sketched in the following pseudo code fragment:
int n = 0;
do {
n += 1;
Context ctx = new Context();
Solver solver = ctx.mkSolver();
// Construct large formula b(n) (depending on n)
// with a lot of Boolean subexpressions
BoolExpr b = ...
solver.add(b);
Status result = solver.check();
solver.dispose();
ctx.dispose();
if (result == Status.SATISFIABLE) {
break;
}
} while (true);
However, I'm quickly running into memory issues (i.e., a Z3Exception
with message "out of memory" is thrown), and I feel that I'm probably not correctly disposing the created Z3Objects.
Since I did not find information of this (i just found Z3 Java API documentation or tutorial and Performance issues about Z3 for Java), my questions are:
- When calling
solver.dispose()
, are all containedBoolExpr
disposed as well or do I need to remember them somewhere and call.dispose()
on any of them? - Would it be more performant to move the creation and disposal of the context and solver out of the loop and instead use
solver.push()
andsolver.pop()
inside the loop? - What other
Z3Objects
should I dispose "manually" in the given case?