对象在Swift中如何从内存中删除?

huangapple go评论71阅读模式
英文:

How exactly is object removed from memory in Swift?

问题

  1. 什么是释放内存?
  2. 内存是在强引用计数器为零后立即释放,还是有某个事件触发deinit?
  3. Swift 如何知道某块内存被占用?

似乎这更多涉及操作系统而不是编程语言,但我仍然会感激任何答案。

英文:

I've read a bunch of articles about the memory management in swift. However, there is still some things which are not clear.

  1. What does it mean to free memory?
  2. Is memory freed exactly after counter for strong references is zero or is there some event which triggers deinit?
  3. How does swift knows that some piece of memory is occupied?

Seems like this is more about OS rather than language but still I would appreciate any answer here.

答案1

得分: 4

以下是翻译的内容:

  1. “释放内存”是什么意思?

    这意味着调用类似 free() 的函数,该函数接受指向分配内存的指针并将其标记为“可用”。未来的分配(例如使用 malloc())可以在下次需要新的分配时提供此内存。

  2. 在强引用计数为零后内存是否立即释放,还是有某个事件触发 deinit

    Swift 的 ARC 通过调用 swift_release(the_object) 来递减引用计数。如果递减导致引用计数达到 0 或更低,那么它会立即触发 deinit 和释放内存的代码路径。

    与垃圾回收器不同(可以无限期延迟对象释放),这是确定性的,就在对象的最后使用点。

    您可以自己查看这个过程:https://github.com/apple/swift/blob/2db4a038c3ecbbc5a9b4d90c36b684e2f56b022f/stdlib/public/SwiftShims/swift/shims/RefCount.h#L1028-L1030

  3. Swift 如何知道某个内存块是否被占用?

    实际上,它并不知道。大多数自动内存管理方案对“垃圾”的定义非常保守:只有当没有更多的强引用指向对象时,该对象才被认为是垃圾。本质上,如果我们知道没有更多的强引用指向一个对象,我们可以确定用户不再需要它(因为即使他们想要,他们也不能访问它)。

    如果它们不采用这样保守的立场,任何误报都可能意味着释放仍在使用的对象,这将是灾难性的。

    因此,就系统而言,任何具有非零引用计数的对象都被视为“正在使用”/“占用”,直到其引用计数最终达到零。

英文:

Just to clarify, when we talk about "Swift the language", it's just talking about the language syntax and semantics in the abstract, without any regards for a concrete implementation. Technically, the Swift programming language can be implemented with multiple different memory-management strategies, including conventional Garbage Collection, although some APIs (isKnownUniquelyReferenced(_:) and the CoW behaviours that depend on it) strongly imply that reference-counting is used.

So everything from here on will be talking about the main Swift implementation by Apple, and not others (like RemObjects Silver)

  1. > What does it mean to free memory?

    It means that a function like free() is called, which takes a pointer to the allocated memory and marks it as "available". Future allocations (such as with malloc()) are free to vend out this memory next time a new allocation is needed.

  2. > Is memory freed exactly after counter for strong references is zero or is there some event which triggers deinit?

    Swift's ARC decrements reference counts by calling swift_release(the_object). If the decrement causes the reference count to hit 0 or less, then it immediately triggers the deinit and deallocation code path.

    Unlike a garbage collector (which can defer object deallocation indefinitely), this happens deterministically, right at the point of last use of the object.

    You can see this for yourself: https://github.com/apple/swift/blob/2db4a038c3ecbbc5a9b4d90c36b684e2f56b022f/stdlib/public/SwiftShims/swift/shims/RefCount.h#L1028-L1030

  3. > How does Swift knows that some piece of memory is occupied?

    It doesn't, really. Most automatic memory management schemes take a very conservative definition of "garbage": An object is garbage if and only if there are no more strong references to it. In essence, if we know there's no more strong references to an object, we can be certain that the user doesn't need it anymore (because they couldn't possible access it, even if they wanted).

    If they didn't take such a conservative stance, any false positive would mean the deallocation of an object that's still in use, which would be catastrophic.

    So as far as the system is concerned, any object with a non-zero reference count is "in use"/"occupied"/"not garbage," until its reference count eventually reaches 0.

huangapple
  • 本文由 发表于 2023年6月13日 00:20:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定