Last active
April 21, 2026 20:55
-
-
Save tim-one/280ab7b88b59e9dbacbf8b68a6e2009c to your computer and use it in GitHub Desktop.
Toy cyclic gc code
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 sys | |
| if 1: | |
| import psutil | |
| from collections import deque | |
| import gc | |
| def fint(i): | |
| return format(i, '_') | |
| pending = set() | |
| class Node: | |
| def __init__(self, c, nnodes=10_000): | |
| self.c = c # creation time | |
| self.payload = bytes(nnodes) | |
| self.me = self | |
| def __del__(self): | |
| pending.remove(self.c) | |
| def chunkit(): | |
| from heapq import heapify, heappop | |
| xs = list(pending) | |
| heapify(xs) | |
| while xs: | |
| base = heappop(xs) | |
| run = 1 | |
| while xs and xs[0] == base + run: | |
| heappop(xs) | |
| run += 1 | |
| print(" ", fint(base), | |
| "for", run) | |
| lasttotalcoll = totalcoll = 0 | |
| def cb(phase, info): | |
| global totalcoll, gen | |
| if 0:#info["generation"] == 0: | |
| input("ZERO") | |
| if phase != "stop": | |
| return | |
| totalcoll += info["collected"] | |
| gen = info["generation"] | |
| #print("gen", gen, info["collected"]) | |
| d = deque(maxlen=1000) | |
| c = 0 | |
| gc.set_threshold(2000, 10) | |
| maxrss = 0 | |
| maxat = 0 | |
| lastdiap = 0 | |
| P = psutil.Process() | |
| gc.collect() | |
| gc.callbacks.append(cb) | |
| while True: | |
| c += 1 | |
| pending.add(c) | |
| d.append(Node(c)) | |
| rss = P.memory_info().rss | |
| if rss > maxrss: | |
| maxrss = rss | |
| maxat = c | |
| dcoll = totalcoll - lasttotalcoll | |
| assert dcoll >= 0 | |
| if not dcoll: | |
| continue | |
| print(fint(c), | |
| fint(rss), | |
| format(rss / maxrss, '7.2%'), | |
| fint(maxrss), | |
| fint(maxat), | |
| "gen", gen, | |
| "coll", fint(totalcoll), | |
| "delta coll", fint(dcoll), | |
| "live", fint(c - totalcoll), | |
| "delta disp", fint(c - lastdiap), | |
| ' ', | |
| ) | |
| chunkit() | |
| lastdiap = c | |
| lasttotalcoll = totalcoll |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment