Save 37% off PRO during our Black Friday Sale! »

メモリー管理の嬉しいバイキング料理

2af8daf170a3ed693e577a9471ea1f10?s=47 Ray Fix
August 20, 2016

 メモリー管理の嬉しいバイキング料理

Swiftの初心者の中には、自動参照カウントを使えば、メモリー管理は気にしなくても良いと認識している方が多くいます。確かにSwiftは、ごみ収集システムを使うより、予測可能性は高いと言えます。しかし自動参照カウントを使用する際は、メモリー構造を意識する必要があります。ユーザーは、幾つかの方法でメモリー構造の落とし穴を避けることができます。今回のトークでは、メモリーの取り扱い方法について分かりやく説明します。

2af8daf170a3ed693e577a9471ea1f10?s=128

Ray Fix

August 20, 2016
Tweet

Transcript

  1. ϝϞϦʔ؅ཧͷඒຯ͍͠ όΠΩϯάྉཧ Ray Fix @rayfix ը૾ఏڙ Yumi Kimura (CC2)

  2. Ray Fix͸୭ʁ Source: https://en.wikipedia.org/wiki/La_Jolla

  3. RayWenderlich.com

  4. iosdc.jp Ray Fix

  5. iosdc.jp Ray Fix

  6. iosdc.jp Ray Fix ϝϞϦʔ؅ཧ

  7. iosdc.jp Ray Fix ελοΫvsώʔϓྖҬ

  8. iosdc.jp Ray Fix ελοΫ ελοΫ͸ͱʹ͔͘଎͍ʂ

  9. iosdc.jp Ray Fix ελοΫ ελοΫ͸ͱʹ͔͘଎͍ʂ func candy() { let ramune

    = 10 let pocky = 12 let gummi = 20 }
  10. iosdc.jp Ray Fix ελοΫ ελοΫ͸ͱʹ͔͘଎͍ʂ func candy() { let ramune

    = 10 let pocky = 12 let gummi = 20 } ελοΫ
  11. iosdc.jp Ray Fix ελοΫ ελοΫ͸ͱʹ͔͘଎͍ʂ func candy() { let ramune

    = 10 let pocky = 12 let gummi = 20 } ελοΫ 10 12 20
  12. iosdc.jp Ray Fix ελοΫ ελοΫ͸ͱʹ͔͘଎͍ʂ func candy() { let ramune

    = 10 let pocky = 12 let gummi = 20 } ελοΫ
  13. iosdc.jp Ray Fix ελοΫ ελοΫ͸ͱʹ͔͘଎͍ʂ func candy() { let ramune

    = 10 let pocky = 12 let gummi = 20 } ελοΫ ϩοΫ͢Δඞཁ͸શ͘ͳ͍
  14. iosdc.jp Ray Fix ώʔϓྖҬ class Dishɹ{ var name: String init(name:

    String) { self.name = name print(" Dish \(name)") } deinit { print("☠ Dish \(name)") } }
  15. iosdc.jp Ray Fix ώʔϓྖҬ

  16. iosdc.jp Ray Fix ώʔϓྖҬ let softCream = Dish(name: “Soft Cream")

  17. iosdc.jp Ray Fix ώʔϓྖҬ let softCream = Dish(name: “Soft Cream")

    Count: 1 “Soft Cream” ώʔϓ
  18. iosdc.jp Ray Fix ώʔϓྖҬ let softCream = Dish(name: “Soft Cream")

    Count: 1 “Soft Cream” ώʔϓ softCream ελοΫ
  19. iosdc.jp Ray Fix ώʔϓྖҬ let softCream = Dish(name: “Soft Cream")

    Count: 1 “Soft Cream” softCream ώʔϓ ελοΫ
  20. iosdc.jp Ray Fix ώʔϓྖҬ let softCream = Dish(name: “Soft Cream")

    let special = softCream Count: 1 “Soft Cream” softCream ώʔϓ ελοΫ
  21. iosdc.jp Ray Fix ώʔϓྖҬ let softCream = Dish(name: “Soft Cream")

    let special = softCream Count: 1 “Soft Cream” softCream ώʔϓ ελοΫ Count: 2 “Soft Cream” special
  22. iosdc.jp Ray Fix ڞ༗ softCream.name = "Choco Soft Cream" special.name

    // "Choco Soft Cream"
  23. iosdc.jp Ray Fix ڞ༗ softCream.name = "Choco Soft Cream" special.name

    // "Choco Soft Cream" ϫΦɺศརʂʂʂ
  24. iosdc.jp Ray Fix ڞ༗ softCream.name = "Choco Soft Cream" special.name

    // "Choco Soft Cream" ϫΦɺศརʂʂʂ ͔͠͠
  25. iosdc.jp Ray Fix ڞ༗ softCream.name = "Choco Soft Cream" special.name

    // "Choco Soft Cream" ϫΦɺศརʂʂʂ ͔͠͠ special.name = "ϐʔϚϯ" softCream.name // "ϐʔϚϯ"
  26. iosdc.jp Ray Fix ڞ༗ softCream.name = "Choco Soft Cream" special.name

    // "Choco Soft Cream" ϫΦɺศརʂʂʂ ͔͠͠ special.name = "ϐʔϚϯ" softCream.name // "ϐʔϚϯ" ૝ఆ֎ͷڞ༗͸ࢀরܕͷܽ఺
  27. iosdc.jp Ray Fix ղܾ๏:ఆ਺Λ࢖͏ class Dishɹ{ init(name: String) { self.name

    = name print(" Dish \(name)") } deinit { print("☠ Dish \(name)") } } var name: String
  28. iosdc.jp Ray Fix ղܾ๏:ఆ਺Λ࢖͏ class Dishɹ{ init(name: String) { self.name

    = name print(" Dish \(name)") } deinit { print("☠ Dish \(name)") } } var name: String
  29. iosdc.jp Ray Fix ղܾ๏:ఆ਺Λ࢖͏ class Dishɹ{ init(name: String) { self.name

    = name print(" Dish \(name)") } deinit { print("☠ Dish \(name)") } } let name: String
  30. iosdc.jp Ray Fix ղܾ๏:ఆ਺Λ࢖͏ class Dishɹ{ init(name: String) { self.name

    = name print(" Dish \(name)") } deinit { print("☠ Dish \(name)") } } let name: String special.name = "ϐʔϚϯ"
  31. iosdc.jp Ray Fix ώʔϓྖҬ let softCream = Dish(name: “Soft Cream")

    let special = softCream Count: 0 “Soft Cream” ώʔϓ ελοΫ Count: 2 “Soft Cream” softCream special
  32. iosdc.jp Ray Fix ώʔϓྖҬ let softCream = Dish(name: “Soft Cream")

    let special = softCream Count: 0 “Soft Cream” ώʔϓ ελοΫ
  33. iosdc.jp Ray Fix ώʔϓྖҬ let softCream = Dish(name: “Soft Cream")

    let special = softCream ώʔϓ ελοΫ
  34. iosdc.jp Ray Fix Customer orders ໰୊ɿ॥؀ࢀর Order customer dish Dish

    name
  35. iosdc.jp Ray Fix ࢀরαΠΫϧ class Customer { var orders: [Order]

    func add(order: Order) { order.customer = self orders.append(order) } } class Order { var customer: Customer? let dish: Dish }
  36. iosdc.jp Ray Fix ࢀরαΠΫϧ class Customer { var orders: [Order]

    func add(order: Order) { order.customer = self orders.append(order) } } class Order { var customer: Customer? let dish: Dish }
  37. iosdc.jp Ray Fix ࢀরαΠΫϧ let ray = Customer(name: "Ray") let

    saikoro = Dish(name: “Saikoro Steak") let order = Order(dish: saikoro) ray.add(order: order)
  38. iosdc.jp Ray Fix ࢀরαΠΫϧ let ray = Customer(name: "Ray") let

    saikoro = Dish(name: “Saikoro Steak") let order = Order(dish: saikoro) ray.add(order: order) ray saikoro order orders
  39. iosdc.jp Ray Fix ࢀরαΠΫϧ let ray = Customer(name: "Ray") let

    saikoro = Dish(name: “Saikoro Steak") let order = Order(dish: saikoro) ray.add(order: order) ray saikoro order orders
  40. iosdc.jp Ray Fix Xcode 8 Memory Visualizer

  41. iosdc.jp Ray Fix Xcode 8 Memory Visualizer

  42. iosdc.jp Ray Fix Xcode 8 Memory Visualizer

  43. iosdc.jp Ray Fix Xcode 8 Memory Visualizer

  44. iosdc.jp Ray Fix Xcode 8 Memory Visualizer

  45. iosdc.jp Ray Fix Customer orders ॥؀ࢀর Order customer dish Dish

    name
  46. iosdc.jp Ray Fix ॥؀ࢀর class Customer { var orders: [Order]

    func add(order: Order) { order.customer = self orders.append(order) } } class Order { let dish: Dish } var customer: Customer?
  47. iosdc.jp Ray Fix ॥؀ࢀর class Customer { var orders: [Order]

    func add(order: Order) { order.customer = self orders.append(order) } } class Order { let dish: Dish } var customer: Customer? weak
  48. iosdc.jp Ray Fix ॥؀ࢀর class Customer { var orders: [Order]

    func add(order: Order) { order.customer = self orders.append(order) } } class Order { let dish: Dish } var customer: Customer? weak
  49. iosdc.jp Ray Fix ॥؀ࢀর let ray = Customer(name: "Ray") let

    saikoro = Dish(name: "Saikoro") let order = Order(dish: saikoro) ray.add(order: order) ray saikoro order orders
  50. iosdc.jp Ray Fix ॥؀ࢀর let ray = Customer(name: "Ray") let

    saikoro = Dish(name: "Saikoro") let order = Order(dish: saikoro) ray.add(order: order) ray saikoro order orders
  51. iosdc.jp Ray Fix ॥؀ࢀর let ray = Customer(name: "Ray") let

    saikoro = Dish(name: "Saikoro") let order = Order(dish: saikoro) ray.add(order: order) saikoro order nil
  52. iosdc.jp Ray Fix ॥؀ࢀর let ray = Customer(name: "Ray") let

    saikoro = Dish(name: "Saikoro") let order = Order(dish: saikoro) ray.add(order: order) saikoro
  53. iosdc.jp Ray Fix ॥؀ࢀর let ray = Customer(name: "Ray") let

    saikoro = Dish(name: "Saikoro") let order = Order(dish: saikoro) ray.add(order: order)
  54. iosdc.jp Ray Fix weak ࢀর̍ ࢀর̎ ࢀর̏

  55. iosdc.jp Ray Fix weak ࢀর̍ ࢀর̎ ࢀর̏

  56. iosdc.jp Ray Fix weak ࢀর̍ ࢀর̎ ࢀর̏

  57. iosdc.jp Ray Fix weak nil nil nil ࢀর̍ ࢀর̎ ࢀর̏

  58. iosdc.jp Ray Fix weak nil nil nil ࢀর̍ ࢀর̎ ࢀর̏

    reference value ࢀর̍ nil ࢀর̎ nil ࢀর̏ nil άϩʔόϧςʔϒϧ
  59. iosdc.jp Ray Fix Swift weak 1 3 ࢀর̎ ࢀর̏ ࢀর̍

  60. iosdc.jp Ray Fix Swift weak 1 3 ࢀর̎ ࢀর̏ ࢀর̍

  61. iosdc.jp Ray Fix Swift weak 1 3 0 κϯϏ 3

    ࢀর̎ ࢀর̏ ࢀর̍
  62. ڧ͍ࢀরΧ΢ϯτ͕̌ʹ

  63. ڧ͍ࢀরΧ΢ϯτ͕̌ʹ

  64. ऑ͍ࢀরΧ΢ϯτ͕̌ʹ

  65. ऑ͍ࢀরΧ΢ϯτ͕̌ʹ

  66. unowned • unowned ͸ผͷछྨͷऑ͍ࢀর • ࢀরઌ͕ඞͣ͋Δ͜ͱ͕લఏ • ΋͠ࢀরઌ͕ͳ͍ͱɺϓϩάϥϜ͕ఀࢭ͢Δ

  67. unowned class Order { let dish: Dish } class Customer

    { var orders: [Order] func add(dish: Dish) { let order = Order(dish: dish, customer: self) orders.append(order) } } let customer: Customer
  68. unowned class Order { let dish: Dish } class Customer

    { var orders: [Order] func add(dish: Dish) { let order = Order(dish: dish, customer: self) orders.append(order) } } let customer: Customer
  69. unowned class Order { let dish: Dish } class Customer

    { var orders: [Order] func add(dish: Dish) { let order = Order(dish: dish, customer: self) orders.append(order) } } let customer: Customer unowned
  70. unowned class Order { let dish: Dish } class Customer

    { var orders: [Order] func add(dish: Dish) { let order = Order(dish: dish, customer: self) orders.append(order) } } let customer: Customer unowned
  71. iosdc.jp Ray Fix unowned 1 3 0 κϯϏ 3 ࢀর̎

    ࢀর̏ ࢀর̍
  72. iosdc.jp Ray Fix unowned 1 3 0 κϯϏ 3 ࢀর̎

    ࢀর̏ ࢀর̍ ϓϩάϥϜ͕ఀࢭ
  73. ؔ਺ͱΫϩʔδϟ΋ࢀরܕ { }

  74. ؔ਺ͱΫϩʔδϟ΋ࢀরܕ { }

  75. ྫɿण࢘԰ enum MenuItem: String { case toro, ebi, anago, uni,

    ikura, hamachi } typealias Action = ()->()
  76. ྫɿ ण࢘԰ class Sushiya { lazy var menu: [MenuItem: Action]

    = […] func prepare(_ menuItem: MenuItem) { menu[menuItem]?() } private func serve(dish: Dish) { print("Now serving \(dish.name)") } }
  77. ྫɿ ण࢘԰ menu = [ .toro: { let dish =

    Dish(name: "Toro") }, … ] serve(dish: dish)
  78. ྫɿ ण࢘԰ menu = [ .toro: { let dish =

    Dish(name: "Toro") }, … ] serve(dish: dish) self.
  79. ྫɿ ण࢘԰ menu = [ .toro: { let dish =

    Dish(name: "Toro") }, … ] serve(dish: dish) self. let sushiya = Sushiya() sushiya.prepare(.toro)
  80. ϝϞϦʔ࿙Ε

  81. ղܾ๏ɿ ΩϟϓνϟʔϦετ var value = 0 let showValue = {

    print(value) }
  82. ղܾ๏ɿ ΩϟϓνϟʔϦετ var value = 0 let showValue = {

    print(value) } showValue() // prints 0 value = 10 showValue() // prints 10
  83. ղܾ๏ɿ ΩϟϓνϟʔϦετ var value = 0 let showValue = {

    print(value) } showValue() // prints 0 value = 10 showValue() // prints 10
  84. ղܾ๏ɿ ΩϟϓνϟʔϦετ var value = 0 let showValue = {

    print(value) } showValue() // prints 0 value = 10 showValue() // prints [value] in 10
  85. ղܾ๏ɿ ΩϟϓνϟʔϦετ var value = 0 let showValue = {

    print(value) } showValue() // prints 0 value = 10 showValue() // prints [value] in 0
  86. ղܾ๏ɿ ΩϟϓνϟʔϦετ menu = [ .toro: { let dish =

    Dish(name: "Toro") self.serve(dish: dish) }, … ]
  87. ղܾ๏ɿ ΩϟϓνϟʔϦετ menu = [ .toro: { let dish =

    Dish(name: "Toro") self.serve(dish: dish) }, … ] [unowned self] in #
  88. ඇಉظͷ໰୊ private func serve(dish: Dish) { print("Now serving \(dish.name)") }

  89. ඇಉظͷ໰୊ private func serve(dish: Dish) { DispatchQueue.main.async { print("Now serving

    \(dish.name)") self.served += 1 } } private func serve(dish: Dish) { print("Now serving \(dish.name)") }
  90. ඇಉظͷ໰୊ [unowned self] in private func serve(dish: Dish) { DispatchQueue.main.async

    { print("Now serving \(dish.name)") self.served += 1 } } private func serve(dish: Dish) { print("Now serving \(dish.name)") }
  91. ඇಉظͷ໰୊ [unowned self] in private func serve(dish: Dish) { DispatchQueue.main.async

    { print("Now serving \(dish.name)") self.served += 1 } } private func serve(dish: Dish) { print("Now serving \(dish.name)") } CRASH!!!!
  92. ඇಉظͷ໰୊ private func serve(dish: Dish) { DispatchQueue.main.async { print("Now serving

    \(dish.name)") self.served += 1 } } private func serve(dish: Dish) { print("Now serving \(dish.name)") }
  93. weak private func serve(dish: Dish) { DispatchQueue.main.async { [weak self]

    in print("Now serving \(dish.name)") self?.served += 1 } }
  94. strong weak μϯε private func serve(dish: Dish) { DispatchQueue.main.async {

    [weak self] in guard let strongSelf = self else { print("Cancelled \(dish.name)") return } print("Now serving \(dish.name)") strongSelf.served += 1 } }
  95. ඒຯ͍͠SWIFTΛॻ͘ʹ͸ ϝϞϦʔͱͷ૬ੑΛ ೺Ѳ͢Δඞཁ͕͋Δ

  96. ͝ਗ਼ௌ͋Γ͕ͱ͏ ͍͟͝·ͨ͠

  97. https://www.youtube.com/watch?v=0-vWT-t0UHg ԣ඿ϦφοΫεಡॻձ̒̓ɿmalloc https://www.mikeash.com/pyblog/friday-qa-2015-12-11-swift-weak-references.html SWIFT ऑ͍ࢀরͷৄ͍͠࿩ http://bit.ly/etystamp ༑ୡͷ LINE ελϜϓʂ https://en.wikipedia.org/wiki/La_Jolla

    ೆΧϦϑΥϧχΞͷࣸਅ https://ja.wikipedia.org/wiki/ण࢘ ण࢘ͷࣸਅ http://echo-labs.com/ ECHO LABS ݦඍڸ http://raywenderlich.com/ Ray Wenderlich ϝϞϦʔνϡʔτ (Maxime Defauwஶऀ) 9݄ެ։༧ఆ ࢀߟϦϯΫ https://github.com/rayfix/MemoryDish ιʔείʔυɺεϥΠυ wwdc2016/416/ WWDC Swift Performance
  98. https://www.youtube.com/watch?v=0-vWT-t0UHg ԣ඿ϦφοΫεಡॻձ̒̓ɿmalloc https://www.mikeash.com/pyblog/friday-qa-2015-12-11-swift-weak-references.html SWIFT ऑ͍ࢀরͷৄ͍͠࿩ http://bit.ly/etystamp ༑ୡͷ LINE ελϜϓʂ https://en.wikipedia.org/wiki/La_Jolla

    ೆΧϦϑΥϧχΞͷࣸਅ https://ja.wikipedia.org/wiki/ण࢘ ण࢘ͷࣸਅ http://echo-labs.com/ ECHO LABS ݦඍڸ http://raywenderlich.com/ Ray Wenderlich ϝϞϦʔνϡʔτ (Maxime Defauwஶऀ) 9݄ެ։༧ఆ ࢀߟϦϯΫ https://github.com/rayfix/MemoryDish ιʔείʔυɺεϥΠυ wwdc2016/416/ WWDC Swift Performance