Skip to content

Instantly share code, notes, and snippets.

@trilliwon
Created December 12, 2021 06:49
Show Gist options
  • Save trilliwon/35963e2e79f5b27d257748c7f773acd9 to your computer and use it in GitHub Desktop.
Save trilliwon/35963e2e79f5b27d257748c7f773acd9 to your computer and use it in GitHub Desktop.

Why is ARC?

  • Real-time 으로 메모리 관리를 한다.
  • background process 가 없기 때문에 더 efficient 하다. mobile 에 최적이다.

But

  • Cannot cope with retain cycles.

What is ARC?

  • Clang Compiler 에서 지원하는 메모리 관리기능이다. Objective-C, Swift 를 지원한다.
  • 클래스의 인스턴스가 생성되면 ARC 는 인스턴스의 정보를 저장하기 위해 메모리를 할당한다.
  • 인스턴스가 더이상 필요하지 않게 되면 메모리를 해제 한다. 하지만 사용되고 있다면 ARC 는 메모리를 해제 하지 못한다.
  • 그럼 사용되고 사용되지 않는 것은 어떻게 알수 있는가? Reference Counting 을 이용한다.
  • 어떤 인스턴스에 대해 Strong Reference가 존재 하면 ARC 는 메모리를 해제 하지 않는다. Strong Reference 가 0이 되면 그 때는 인스턴스가 더이상 필요하지 않다고 판단 하고 메모리를 해제한다.

Strong Reference Cycle

  • 인스턴스 사이 Strong References
  • 클로져에 대한 Strong References
  • 문제가 하나 있는데 Strong Reference Cycle 이 발생할 수 있다는 것이다.
  • weak (Optional Type), unowned
  • "weak references" referred to references that were not retaining
  • closure 에서는 캡쳐리스트에 weak 혹은 unowned 를 사용함으로써 해결할 수 있다.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment