Created
April 12, 2026 16:34
-
-
Save prakashsellathurai/37b005653afa69d4532477dbb11602c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import weakref, sys, gc | |
| class C: | |
| def __add__(self, other): return NotImplemented | |
| def __radd__(self, other): return NotImplemented | |
| obj = type('D', (), {})() | |
| dead = weakref.proxy(obj) | |
| del obj; gc.collect() | |
| live_obj = C() | |
| live = weakref.proxy(live_obj) | |
| before = sys.gettotalrefcount() | |
| for i in range(10000): | |
| try: | |
| live + dead | |
| except ReferenceError: | |
| pass | |
| after = sys.gettotalrefcount() | |
| print(f"Leaked {after - before} refs (~{(after-before)//10000}/call)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment