# How do closures work (memory management)

**URL:** <https://forums.swift.org/t/how-do-closures-work-memory-management/47512>\
**Category:** Using Swift\
**Tags:** swift, closure, escapingclosure\
**Created:** [April 17, 2021, 3:52pm UTC](https://forums.swift.org/t/how-do-closures-work-memory-management/47512 "2021-04-17T15:52:41Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![lukasa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lukasa/32/1232_2.png) [@lukasa](https://forums.swift.org/u/lukasa)\
**Post date:** [April 18, 2021, 10:13am UTC](https://forums.swift.org/t/how-do-closures-work-memory-management/47512/2 "2021-04-18T10:13:01Z")

</div>

Closures are actually conceptually a bit more like this:

```swift
struct Closure {
    var functionPointer: UnsafeRawPointer
    var closureContext: AnyObject?
}

```

That is, they are two pointers wide, where the first pointer is a function pointer pointing to the code that implements the closure, and the second pointer optionally points to a reference counted object containing the closed-over state.

So, yes, the capture list is heap allocated, and it is reference counted. However, I don’t think it is an actual Swift object per se, it behaves somewhat more unusually than that. I’m afraid I can’t help more than this.

---

_[View the full topic](https://forums.swift.org/t/how-do-closures-work-memory-management/47512)._
