0

런타임에 참조 카운트를 줄이거 나 제거하기 위해 프로그램 분석이 진행되고 있습니까? 아니면 프로그래머가 수동으로 카운트를 증가 및 감소시킬 필요가 없다는 의미에서 "자동"인가?하지만 참조가 작성되거나 사라질 때마다 발생합니다.Swift의 Automatic Reference Counting은 자동으로 무엇이 있습니까?

기본적으로 ARC를 일반적인 참조 횟수와 다른 점은 무엇입니까? 특히 프로그램 분석을 사용하는 경우 진행중인 문제에 대해 논의 된 논문이 있습니까?

+2

읽었습니까? https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html – Santosh

답변

1

런타임시 참조 카운트를 줄이거 나 삭제하기위한 프로그램 분석이 있습니까?

아니, 자동으로 는 참조 카운트를 유지 호출을 삽입 진행 프로그램 분석이있다.

아니면 프로그래머가 수동으로 증가 할 필요가 없다는 의미에서 그냥 "자동"하고 카운트를 감소 ...

정확히. ARC가 시작되기 전에 Objective-C 프로그래머는 오브젝트에 대한 참조를 유지하고자 할 때 -retain을 호출해야하며 참조를 완료하면 -release을 호출해야합니다. 이러한 메소드는 객체의 참조 횟수를 증가 및 감소 시켰습니다. ARC를 사용하면 컴파일러는 해당 호출을 추가해야하는 위치를 찾아서 삽입합니다 (또는 이와 동등한 코드). 따라서 참조 카운팅은 프로그래머의 관점에서 자동으로 수행됩니다.

1

자동 참조 계산 (ARC)은 컴파일 타임에 정적 분석을 필요로하지만 이 아닌은 런타임 분석을 수행합니다.

ARC를 개념화하는 쉬운 방법은 2 단계 프로세스입니다. 첫째, 코드를 검사하고 객체 참조가 도입되거나 범위를 벗어난 장소에 메모리 관리 코드 (의미 상으로는 retainrelease 이전 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의 기원을 살펴 보는 것이 가장 좋습니다.

관련 문제