retain 혹은 release 등의 오브젝트 코드를 추가하고 런타임에 레퍼런스 카운트를 올리거나 내림. 이 카운트가 0이 되면 해당 인스턴스를 메모리에서 해제함// 이랬던걸
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
}
// reference: https://stackoverflow.com/a/39379126
가비지 컬렉터라는 런타임 프로그램이 사용 중인 객체들을 마킹하고, 메모리의 처음부터 끝까지 훑으면서 마킹된 객체가 있는 공간을 제외하고는 모두 해제시킴.
Java, C#, Go 등의 프로그래밍 언어에서 사용하는 메모리 관리 기법
일반적으로 같이 거론되는 GC 동작 알고리즘들: Naive mark-and-sweep / tri-color marking
장점:
단점:
P.S. OS X Mountain Lion 에서 GC를 deprecate 했다는 내용이 있다는 걸로 봐서, 애플도 GC를 사용하다가 ARC로 완전히 입지를 굳힌걸로 보임 (레퍼런스)