On this page

You own the memory now๏ƒ

๐ŸŸข Beginner ยท Lesson 41

YOU own the memory now - not the GC. A DI runtime HOLDS what it builds: your unique instance lives in the conduit's creations store, so dropping YOUR variable frees nothing - Python's garbage collector cannot collect what the world still references. This is the trade every DI-style tool makes: the runtime manages your objects, so YOU must end their lifecycles. cleanup() is not ceremony - it is how memory comes back. This lesson PROVES it with a weakref (a watcher that answers "is the object still alive?" without keeping it alive).

Before you run๏ƒ

Use the Beginner guide for prerequisite concepts. Run from a checkout with Melder installed and Python 3.14 free-threading selected. The collection download includes the level's local helper modules.

Run the saved script๏ƒ

python UX_and_AIX_experiences/01_beginner/41_you_own_the_memory_now.py
py -3.14t UX_and_AIX_experiences/01_beginner/41_you_own_the_memory_now.py

Download this collection ยท Source on GitHub

Public surface๏ƒ

the lifecycle law - meld, del, cleanup, and who really holds the object

Code๏ƒ

 1"""
 2TIER: beginner (41)
 3GOAL: YOU own the memory now - not the GC. A DI runtime HOLDS what it
 4      builds: your `unique` instance lives in the conduit's creations
 5      store, so dropping YOUR variable frees nothing - Python's garbage
 6      collector cannot collect what the world still references. This is
 7      the trade every DI-style tool makes: the runtime manages your
 8      objects, so YOU must end their lifecycles. cleanup() is not
 9      ceremony - it is how memory comes back.
10      This lesson PROVES it with a weakref (a watcher that answers
11      "is the object still alive?" without keeping it alive).
12SURFACE EXERCISED: the lifecycle law - meld, del, cleanup, and who
13                   really holds the object
14"""
15import gc
16import weakref
17
18import melder as md
19
20
21class HeavyThing:
22    pass
23
24
25def main() -> None:
26    book = md.Spellbook()
27    book.bind(spell=HeavyThing, existence="unique")
28    conduit = book.conjure()
29
30    thing = conduit.meld(spell=HeavyThing)
31    watcher = weakref.ref(thing)   # watches WITHOUT holding
32
33    # Drop OUR reference. In plain Python this object would now die...
34    del thing
35    gc.collect()
36    print("after del, object alive?", watcher() is not None)
37    assert watcher() is not None   # ...but the RUNTIME still holds it.
38
39    # cleanup() releases the world's grip - THIS is what frees memory.
40    conduit.cleanup()
41    gc.collect()
42    print("after cleanup, object alive?", watcher() is not None)
43    assert watcher() is None
44
45    print("the lifecycle law: the runtime holds what it builds;")
46    print("cleanup() is how you give memory back")
47
48
49if __name__ == "__main__":
50    main()

Check the outcome๏ƒ

The script contains its own assertions or demonstrated refusal paths. Run it to evaluate those checks against your installed version. The code above is taken directly from the saved file; no run output is invented here.

More beginner examples ยท Level guide