Skip to content
← All Python tracks

Memory, Copies and Slow Code

Python

See when two names share one object, what keeps an object alive, and how to measure memory. Then time and profile slow code before you change it. The track ends with optional reading on the garbage collector, profilers, the JIT and native extensions.

This track uses Python. You can make it your practice mode to browse related content across the site.

15 exercises

23 articles

Explore freely. Sign in to keep your progress.

Your learning sequence

  1. 1 When Two Names Share One Object Read
  2. 2 Not solved yet. Fix a Function That Changes Its Caller's List Practice · Easy
  3. 3 When to Use is and When to Use == Read
  4. 4 Not solved yet. Fix a Version Class That Compares Strings with is Practice · Easy
  5. 5 Intern Repeated Strings, but Compare Them with == Read
  6. 6 Copy Nested Data with deepcopy Read
  7. 7 Not solved yet. Deep-Copy Only the Sections You Choose Practice · Medium
  8. 8 What Keeps an Object Alive Read
  9. 9 Not solved yet. Write a Context Manager Class Practice · Easy
  10. 10 Handle Errors and Nesting in a Context Manager Read
  11. 11 Not solved yet. Fix a Nested Context Manager That Hides Errors Practice · Medium
  12. 12 How the Garbage Collector Finds Cycles Read
  13. 13 Not solved yet. Walk an Object Graph with gc.get_referents and a Stack Practice · Medium
  14. 14 Track Objects with Weak References Read
  15. 15 Not solved yet. Build a Registry That Does Not Keep Objects Alive Practice · Medium
  16. 16 What sys.getsizeof Measures Read
  17. 17 Not solved yet. Add Up Nested Sizes, Counting Each Object Once Practice · Medium
  18. 18 Find Memory Growth with tracemalloc Read
  19. 19 Not solved yet. Measure Memory Growth and Always Stop Tracing Practice · Medium
  20. 20 Measure What slots=True Saves Read
  21. 21 Use a Set for Lookups and a Deque for Queues Read
  22. 22 Not solved yet. Remove Duplicates in Order and Keep the Latest Items Practice · Easy
  23. 23 Build Strings with join and Bytes with bytearray Read
  24. 24 Not solved yet. Build a Report as Text and UTF-8 Bytes Practice · Medium
  25. 25 Not solved yet. Fix a Slow Join and a Wasteful Top-k Practice · Medium
  26. 26 Time Your Code with perf_counter Read
  27. 27 Time Small Pieces of Code with timeit Read
  28. 28 Not solved yet. Compare Best Timings and Put the Garbage Collector Back Practice · Medium
  29. 29 Find Slow Functions with cProfile Read
  30. 30 Not solved yet. Rank Profiled Functions by Self Time or Cumulative Time Practice · Medium
  31. 31 Fix the Algorithm Before You Reach for NumPy Read
  32. 32 Not solved yet. Speed Up a Moving Average with Prefix Sums Practice · Medium
  33. 33 Check the Exact Python Version When You Debug the Collector Read
  34. 34 Keep Forked Workers Sharing Memory with gc.freeze Read
  35. 35 Profile with py-spy, Memray, Scalene and line_profiler Read
  36. 36 Sample a Running Program with Tachyon in Python 3.15 Read
  37. 37 Check Whether Your Python Has a JIT, Then Measure Read
  38. 38 When to Write a C or Rust Extension Read

Check yourself

7 questions · not saved while you're signed out

Use these questions to check your understanding and compare explanations. Your score does not affect track completion.

0 / 7 correct

What does this print?

Choose one answer to reveal the explanations. Answers are kept only while this page is open.

def add(x, acc=[]):
    acc.append(x)
    return acc

print(add(1), add(2), add(3))
Question 1 of 7