Make sure result objects don't store references to corresponding solver objects #2262
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Currently, result objects store a reference to the solver object that created them. I think this is an accident. The options field of a solver is an instance of
_SolverOptions
(see here), which gets stored in the result here._SolverOptions
has a_feedback
field which is assigned an instance method of the solver and thus keeps a reference to the solver.I noticed this issue when I called
qsave
on a result object and the resulting file was ~100 MB instead of the expected ~100 KB. Settingresult.options._feedback = None
before callingqsave
reduced the file size by a factor ~2000. But also without pickling, I think that this is a memory leak where solver objects can't be garbage collected.The fix I am submitting here makes results create a copy of the options passed to it, setting
_feedback
toNone
if it exists. I considered converting_SolverOptions
objects to plaindict
s in the result class, but that would discard a little bit of information. (Note however that in many places, theoptions
passed to the results are plaindict
s anyway, for example.) I also considered making_feedback
a weak reference, but that would make pickling more complicated.I am not sure what tests to add about this, if any.