import asyncio
import gc
from gc import get_objects
from pprint import pprint
from starlette.applications import Starlette
from starlette.responses import JSONResponse, Response
from starlette.routing import Route
from starlette.requests import Request
gc.set_debug(gc.DEBUG_LEAK)
async def homepage(request: Request):
request.state.test = [x for x in range(999999)]
return JSONResponse({'hello': 'world'})
async def collect(request: Request):
n = gc.collect()
pprint(gc.garbage)
return JSONResponse({"collected": n})
app = Starlette(routes=[Route('/', homepage), Route('/collect', collect)])
from rich.pretty import pprint
but the pprint(gc.garbage)
blocks me 4ever
So you'd do something like
class Canary: pass
o = Canary()
request.state.foo = o
del o
...
objgraph.show_chain(
objgraph.find_backref_chain(
random.choice(objgraph.by_type('Canary')),
objgraph.is_proper_module),
filename='chain.png')
```
gc.collect()
, my memory goes back to what I'd expect it be - does this say something?