Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

variables map grows in size #10

Closed
zevlg opened this issue Mar 18, 2015 · 6 comments
Closed

variables map grows in size #10

zevlg opened this issue Mar 18, 2015 · 6 comments

Comments

@zevlg
Copy link

zevlg commented Mar 18, 2015

In dynamic environment, when constraints added and removed frequently, solver's variables map grows in size. It is because variable never removed from map even if every constraint it is used in are removed. Here is the code:

from kiwisolver import *

solver = Solver()
for i in range(10):
    v = Variable("var%d"%i)
    c = v == 100;
    solver.addConstraint(c)
    solver.removeConstraint(c)
solver.dump()

I expected no variables, but got:

Variables
---------
var0 = v1
var3 = v7
var7 = v15
var2 = v5
var9 = v19
var6 = v13
var8 = v17
var5 = v11
var1 = v3
var4 = v9
@sccolbert
Copy link
Member

In general, a variable can exist in more than one constraint, and tracking when a variable no longer appears in any constraints would be expensive. If you want to fully clear a solver, you can call solver.reset() which will remove everything.

@zevlg
Copy link
Author

zevlg commented Mar 18, 2015

Yes, but uncontrollable growing is sometimes more troblesome than expenses due to variables cleanup. That is exactly my case, long run solver with frequently changing constraints set eventually leaks all the RAM.

Can you please provide some hints how to implement variables cleanup functionality?

@sccolbert
Copy link
Member

In Enaml, such unbounded growth could occur by continuously adding and removing new widgets from a layout, but Enaml deals with this case by resetting the solver when it gets new layout items and re-adding all relevant constraints.

https://github.com/nucleic/enaml/blob/master/enaml/layout/layout_manager.py#L295

I would suggest you follow a similar model and pick a reasonable time to reset the solver and then re-add all of your still-valid constraints. Checking whether a variable is still contained in the solver requires a linear scan over all rows in the solver. For a constraint with M variables, that would be an O(M x N) operation. Scanning for all empty variables would be O(N^2), at which point it's just cheaper to reset.

@sccolbert
Copy link
Member

Alternatively, you can reuse your old variables in your new constraints. i.e. manage your own free variable pool.

@zevlg
Copy link
Author

zevlg commented Mar 18, 2015

Thanks! Periodically reset and re-add constraints should work for me.

@jyaif
Copy link

jyaif commented May 4, 2018

thread keyword to allow people like me to find it more easily:
memory leak

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants