<#6555 Support GC of object instances that use @me...
# github-notifications
q
#6555 Support GC of object instances that use @memo* Issue created by jsirois Right now, decorating a class with memoized instance methods or properties leads to the instances being retained for all time, ineligible for GC. The culprit is the memo cache which is captured in a closure linked to a method decorator attached to the owning class. This sets up a strong reference chain of
cls -> function decorator -> function cache -> instance key
. It would be nice to ensure: 1. Instances were not strongly referenced by cache keys to allow instances to be GC'd 2. cache entries themselves were not strongly referenced and could be GC'd once instances were GC'd 1 is easy to achieve post #6554 but 2 is trickier. The cache keys in question are compound, with the instance only forming the most significant "bit" and no other part of the system having a handle to the full key. This makes
WeakKeyDictionary
unlikely to be useful. A
WeakValueDictionary
would also likely lead to thrash of the cache:
Copy code
consumer1.result = memoized.result()
<consumer1 done, gc'd - memoized result also now gc'd>
consumer2.result = memoized.result() # Need to recompute
Likely leveraging the ability to register a callback with weakref is the solution. That callback could clear the cache entry associated with the weakref'd instance - although the callback having a handle on the cache itself needs careful ref cycle consideration too. Generally - tricky. pantsbuild/pants
u