자동 참조 계산 (ARC)은 컴파일 타임에 정적 분석을 필요로하지만 이 아닌은 런타임 분석을 수행합니다.
ARC를 개념화하는 쉬운 방법은 2 단계 프로세스입니다. 첫째, 코드를 검사하고 객체 참조가 도입되거나 범위를 벗어난 장소에 메모리 관리 코드 (의미 상으로는 retain
및 release
이전 ARC ObjC 호출과 동일)를 삽입합니다. 그래서이 :
func doSomething() -> Object {
let thing1 = Object(name: "foo")
var thing2 = Object(name: "bar")
thing2 = Object(name: "baz")
return thing2
}
... 이런 (일부 컴파일러 내부 중간 형태)이된다 뭔가 :
func doSomething() -> Object {
let thing1 = Object(name: "foo")
__retain(thing1) // increment reference count so thing1 sticks around
var thing2 = Object(name: "bar")
__retain(thing2) // increment reference count so thing2 sticks around
let oldThing2 = thing2 // thing2 gets replaced on the next line
thing2 = Object(name: "baz")
__release(oldThing2) // get rid of the thing that got overwritten
__retain(thing2) // hold onto the new thing that replaced it
__release(thing1) // end of scope, so nothing else will use thing1
return __autorelease(thing2) // relinquish ownership of thing2,
// but only upon handing ownership over to our caller
}
시간 객체 참조가 도입 그들은 범위 밖으로 이동 시간 또는 (return
문을 통해) 발신자에게 전달되는 메시지는 회선에서 프로그램 의미를 충족시키는 데 최소 요구 사항이지만이 분석 수준에서 중지하면 중복 메모리 관리가 발생합니다. (. thing1
이 생성 한 후 사용되지 않습니다 것을 참고 예를 들어) 그래서 ARC가하는 다음 일은 더 같이 우리의 예를 들어 의사 뭔가를하고, 중복을 제거하고 다른 최적화를 수행 할 수있는 흐름 분석입니다 :
func doSomething() -> Object {
_ = Object(name: "foo") // formerly thing1
// Object.init(name:) is called in case it has side effects,
// but the result is immediately discarded because it's not used
_ = Object(name: "bar") // formerly thing2
// again we call the initializer but discard the result,
// because the original thing2 is immediately overwritten
let thing2 = Object(name: "baz")
// no retain, because nothing happens between here and return
// that could affect the memory lifetime of thing2
return __magic_autorelease(thing2)
// and there's other voodoo we can do to ensure handoff to the caller
// without getting into retain count tracking or autorelease pools
}
이가 분명히 매우 손이 깔려있는 개요입니다. 실제 구현은이 간단한 두 단계 프로세스에 맞지 않지만 개념적으로는 매우 유사합니다.
이들의 첫 번째 두 목표 - C에서 ARC에 대한 있습니다
하지만 ARC : 좀 더 자세한 설명은 좀 봐 스위프트에서 매우 밀접한 관련이 있으므로 ARC가 실제로 어떻게 작동하는지 알고 싶다면 ObjC의 기원을 살펴 보는 것이 가장 좋습니다.
읽었습니까? https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html – Santosh