-
Notifications
You must be signed in to change notification settings - Fork 46
Closure vs Garbage collection
Shunning Jiang edited this page Dec 29, 2017
·
2 revisions
The way we implement update block in component s, is to fold all local variables in init into the closure of update block free function, instead of passing them as arguments.
The problem is that block.func_closure will contain a cell object that points to s, and this prevents garbage collection from collecting s. It seems like the purpose of closure is to prevent garbage collection, so now I don't have a good way to totally delete the object upon API call to free up memory ...
Edit:
Now I figured out a way to do this https://github.com/cornell-brg/pymtl-v3/commit/9c2a149d61da837067189f5afbf32f1f20431b73#diff-7b0df1a9e0d56c7e4b1495ebcdb8cd90R604
Two caveats:
- Delete references that are easy to forget
- Call gc.collect() directly at outer scope. Here I need to have a nested function that deletes the component, and then call gc.collect() at the outer scope.