SlideShare a Scribd company logo
1 of 15
Automatic Reference
Counting
Swift
순서
1. ARC 란 ?
2. 메모리 해제 예제
3. 강한 참조
4. 강한 참조 순환
5. 약한 (weak) 참조
6. 미소유 (unowned) 참조
7. 암시적으로 드러난 옵셔널 속성
8. 클로저를 위한 강한 참조 순환
9. 클로저의 강한 참조 순환 해결
ARC 란 ?
ARC 는 프로그램 내에서 클래스 인스턴스
가 더 이상 필요하지 않을 때 자동으로 메
모리를 해제합니다 .
구조체와 열거형은 Value Type 이라 해당
사항이 없습니다 .
메모리 해제 예제
var reference1: Person?
var reference2: Person?
var reference3: Person?
reference1 = Person(name: "John")
reference2 = reference1
reference3 = reference1
reference1 = nil
reference2 = nil
reference3 = nil // 참조 카운트가 0 이 되
면 해제
강한 참조
var john: Person?
var number73: Apartment?
john = Person(name: "John Appleseed")
number73 = Apartment(number: 73)
john = nil
number73 = nil
// 참조 카운트가 0 이 되면 메모리에서 해제된다
강한 참조 순환
john!.apartment = number73
number73!.tenant = john
john = nil
number73 = nil
// 둘 다 메모리 해제되지 않는다
강한 참조 순환 #2
john = nil
number73 = nil
// 위 코드의 결과로 메모리 누수가 일어난다
서로 안놔주고 붙어 있는 꼴
약한 참조
weak var tenant: Person?
// weak 를 앞에 써주어 약한 참조 , 옵셔널 사용
약한 참조 #2
john = nil
// 강한 참조가 없어졌으므로 메모리 해제된다
약한 참조 #3
number73 = nil
// 역시 메모리에서 해제된다
미소유 참조
unowned let customer: Customer
// unowned 키워드를 사용 , 옵셔널 변수가 아니므로 항상 값이 있다는 전제로
// 사용되는 점이 약한 참조와는 다른 점이다
var john: Customer?
john = Customer(name: "John Appleseed")
john!.card = CreditCard(number: 1234_5678_9012_3456, customer: john!)
미소유 참조 #2
john = nil
// john 과 john.card 에 할당된 CreditCard 인스턴스도 함께 해제된다
암시적으로 드러난 옵셔널 속
성
let capitalCity: City!
// 암시적으로 드러난 옵셔널로 선언하면 !(unwrap) 할 필요없이
사용
var country = Country(name: "Canada", capitalName:
"Ottawa")
println("(country.name)'s capital city is called 
(country.capitalCity.name)")
let capitalCity: City?
// 옵셔널로 capitalCity 를 선언했다면 !(unwrap) 해야 한다
println("(country.name)'s capital city is called 
(country.capitalCity!.name)")
클로저를 위한 강한 참조 순환
class HTMLElement {
lazy var asHTML: () -> String = {
if let text = self.text {
return "<(self.name)>(text)</(self.name)>"
} else {
return "<(self.name) />"
}
}
...
강한 참조 순환으로 메모리에서 해제되지 않는다
클로저의 강한 참조 순환 해결
lazy var asHTML: () -> String = {
[unowned self] in
if let text = self.text {
return "<(self.name)>(text)</(self.name)>"
} else {
return "<(self.name) />"
}
} // [unowned self, weak otherObject, ...] in 이런식으로 capture list 나열가능

More Related Content

Featured

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

Automatic Reference Counting - Swift

  • 2. 순서 1. ARC 란 ? 2. 메모리 해제 예제 3. 강한 참조 4. 강한 참조 순환 5. 약한 (weak) 참조 6. 미소유 (unowned) 참조 7. 암시적으로 드러난 옵셔널 속성 8. 클로저를 위한 강한 참조 순환 9. 클로저의 강한 참조 순환 해결
  • 3. ARC 란 ? ARC 는 프로그램 내에서 클래스 인스턴스 가 더 이상 필요하지 않을 때 자동으로 메 모리를 해제합니다 . 구조체와 열거형은 Value Type 이라 해당 사항이 없습니다 .
  • 4. 메모리 해제 예제 var reference1: Person? var reference2: Person? var reference3: Person? reference1 = Person(name: "John") reference2 = reference1 reference3 = reference1 reference1 = nil reference2 = nil reference3 = nil // 참조 카운트가 0 이 되 면 해제
  • 5. 강한 참조 var john: Person? var number73: Apartment? john = Person(name: "John Appleseed") number73 = Apartment(number: 73) john = nil number73 = nil // 참조 카운트가 0 이 되면 메모리에서 해제된다
  • 6. 강한 참조 순환 john!.apartment = number73 number73!.tenant = john john = nil number73 = nil // 둘 다 메모리 해제되지 않는다
  • 7. 강한 참조 순환 #2 john = nil number73 = nil // 위 코드의 결과로 메모리 누수가 일어난다 서로 안놔주고 붙어 있는 꼴
  • 8. 약한 참조 weak var tenant: Person? // weak 를 앞에 써주어 약한 참조 , 옵셔널 사용
  • 9. 약한 참조 #2 john = nil // 강한 참조가 없어졌으므로 메모리 해제된다
  • 10. 약한 참조 #3 number73 = nil // 역시 메모리에서 해제된다
  • 11. 미소유 참조 unowned let customer: Customer // unowned 키워드를 사용 , 옵셔널 변수가 아니므로 항상 값이 있다는 전제로 // 사용되는 점이 약한 참조와는 다른 점이다 var john: Customer? john = Customer(name: "John Appleseed") john!.card = CreditCard(number: 1234_5678_9012_3456, customer: john!)
  • 12. 미소유 참조 #2 john = nil // john 과 john.card 에 할당된 CreditCard 인스턴스도 함께 해제된다
  • 13. 암시적으로 드러난 옵셔널 속 성 let capitalCity: City! // 암시적으로 드러난 옵셔널로 선언하면 !(unwrap) 할 필요없이 사용 var country = Country(name: "Canada", capitalName: "Ottawa") println("(country.name)'s capital city is called (country.capitalCity.name)") let capitalCity: City? // 옵셔널로 capitalCity 를 선언했다면 !(unwrap) 해야 한다 println("(country.name)'s capital city is called (country.capitalCity!.name)")
  • 14. 클로저를 위한 강한 참조 순환 class HTMLElement { lazy var asHTML: () -> String = { if let text = self.text { return "<(self.name)>(text)</(self.name)>" } else { return "<(self.name) />" } } ... 강한 참조 순환으로 메모리에서 해제되지 않는다
  • 15. 클로저의 강한 참조 순환 해결 lazy var asHTML: () -> String = { [unowned self] in if let text = self.text { return "<(self.name)>(text)</(self.name)>" } else { return "<(self.name) />" } } // [unowned self, weak otherObject, ...] in 이런식으로 capture list 나열가능