Embercode
✕
References and MemoryQuick quiz

Ownership and leaks

6 questions. Covers Leaks and detection in Python.

Questions 1 to 5 of 6

  1. 1. What does this program print? ``python import weakref class Image: pass ref = weakref.ref(Image()) print(ref()) ``
  2. 2. The program below prints True. What does that True tell you? ``python import gc import weakref class Session: def __init__(self, id): self.id = id registry = [] item = Session(7) probe = weakref.ref(item) registry.append(item) item = None gc.collect() print(probe() is not None) ``
  3. 3. What does this program do when it runs? ``python import weakref class Image: def __init__(self, name): self.name = name cache = {} cache['hero'] = weakref.ref(Image('hero')) print(cache['hero']().name) ``
  4. 4. Each request calls the function below, and eventually the program fails with MemoryError. Why does calling gc.collect() not help? ``python import gc cache = [] def handle_request(): cache.append(bytearray(1_000_000)) gc.collect() ``
  5. 5. True or false: after del obj, a WeakValueDictionary entry for obj is removed at once, with no gc.collect() call, if nothing else holds obj and it is not in a reference cycle.

5 question(s) left on this page.