Skip to content
Learn Netverks

Lesson

Step 28/36 78% through track

memory-arc

Memory and ARC

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Memory and ARC: the syntax, patterns, and safety habits you need before advancing in Swift.

Automatic Reference Counting manages heap objects—retain cycles with closures are a common interview topic.

You will apply Memory and ARC in contexts like: iPhone/iPad/Mac apps, server-side Swift (niche), and Apple toolchain projects.

Write Swift in main.swift with print(), click Run on server—the dev runner swiftc compiles and runs the binary (requires Swift toolchain, typically macOS; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Swift manages heap memory for class instances with Automatic Reference Counting (ARC)—deterministic deallocation when reference count hits zero, unlike tracing GC in Java or Go.

Reference cycles

class Node {
    var next: Node?
    deinit { print("deinit") }
}

Strong references between classes can leak—break cycles with weak or unowned references, especially in closures capturing self.

Value types and ARC

Structs and enums do not use ARC for their own storage—copy-on-write collections may share buffers until mutated.

Important interview questions and answers

  1. Q: weak vs unowned?
    A: weak is optional and zeroes when target deallocates; unowned assumes target outlives the reference—crash if wrong.
  2. Q: Struct on stack?
    A: Small value types often live on the stack or inline; classes always heap-allocate instance data with ARC tracking.

Self-check

  1. What memory model do Swift classes use?
  2. How do closures cause retain cycles?

Pitfall: Closures capturing self strongly can create retain cycles—use [weak self] in class contexts.

Interview prep

weak vs unowned?

weak optional and zeroes; unowned assumes target outlives reference.

Retain cycles?

Strong references between classes or closures capturing self strongly.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Retain cycle fix?
  • weak vs unowned?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump