Swift - Retain Cycle, Automatic Reference Counting, Memory Leak - iOS Interview Questions

Поделиться
HTML-код
  • Опубликовано: 28 сен 2024
  • The first topic in my series about iOS Interview questions is about Retain Cycles, Automatic Reference Counting, and Memory Leaks in Swift. It's a tricky topic to get the hang of, so leave any questions in the comments and I'll be happy to help.
    iOS Dev Courses - seanallen.teac...
    This is part of a series of videos about iOS interview questions in Swift. Check out the overview where I list and briefly discuss all the topics that are in this series:
    • iOS Interview Question...
    Project Source Code:
    www.dropbox.co...
    Apple Automatic Reference Counting Doc:
    developer.appl...
    Memory Leaks in Closures:
    / understanding-memory-l...
    iOS Dev Courses:
    seanallen.teac...
    Twitter:
    Sean Allen - / seanallen_dev
    Hired.com:
    hired.com/x/1n01g
    Book and learning recommendations that help out the channel if you decide to purchase (Affiliate Links):
    Paul Hudson's Hacking With Swift:
    gumroad.com/a/...
    Donny Wals - Combine:
    gumroad.com/a/...
    Mark Moeyken’s SwiftUI Books:
    www.bigmountainstudio.com/swiftui-views-book/fzc51
    Objc.io Books (Thinking in SwiftUI & Advanced Swift):
    gumroad.com/a/...
    Ray Wenderlich Books:
    store.raywende...
    #swift #softwaredeveloper #iosdeveloper

Комментарии • 162

  • @seanallen
    @seanallen  4 года назад +4

    Watch Next - iOS Take Home Project - Job Interview Practice - Free Preview - ruclips.net/video/MSIe2y6Fee8/видео.html

  • @heri_prieto
    @heri_prieto 4 года назад +42

    I had my interview with Postmates about an hour ago and they asked me about everything you talk about in this video. I had to stop by and thank you for allowing me to answer these questions flawlessly!

    • @seanallen
      @seanallen  4 года назад +2

      That's awesome to hear! Best of luck with the interview. Thanks for stopping by with the update 😀

  • @arjun9951
    @arjun9951 6 лет назад +18

    Finally someone explained this clearly. Keep killing it man.

    • @seanallen
      @seanallen  6 лет назад

      Thanks Arjun 👍 I appreciate it!

  • @Kayzer606
    @Kayzer606 6 лет назад +34

    This is the best explanation I've seen so far !

    • @seanallen
      @seanallen  6 лет назад +2

      Thanks! Glad you enjoyed it

  • @sasanhabibian6522
    @sasanhabibian6522 4 года назад +5

    Oh god! what a great explanation. Finally I understand what is the weak references.

    • @seanallen
      @seanallen  4 года назад

      Glad it was helpful!

  • @shiviagarwal9134
    @shiviagarwal9134 3 года назад +4

    You are always a great help Sean! Could you please also make a video on "Memory leaks in closure"? It's always easy to understand the concept from your videos rather that reading lengthy articles online.

    • @seanallen
      @seanallen  3 года назад +2

      It's on my video to-do list. It's just a VERY long list. Too many videos, too little time!

  • @swiftlandacademy3193
    @swiftlandacademy3193 4 года назад +2

    I’ve gotten a question yesterday about this during an interview! I’m glad I could explain it thanks to Sean 😄

    • @seanallen
      @seanallen  4 года назад

      Nice! Happy to hear I could be of some help! Best of luck on getting the job 😀

  • @danielniels22
    @danielniels22 Год назад

    omg thank you so much. such a simple example and explanation. im new in IOS Dev, so many keywords are intimidating me lately

  • @vigneshwaran6348
    @vigneshwaran6348 4 года назад

    This is really Wonder. great job..... you are the backbone of all swift freshers... me also... i am not having words to express my thanks.....

    • @seanallen
      @seanallen  4 года назад

      Glad you're enjoying the content, Vignesh!

  • @payalmalekar6529
    @payalmalekar6529 4 года назад +1

    Finally I understand the concept clearly.. thanks Sean for wonderful video.👍

    • @seanallen
      @seanallen  4 года назад +1

      Glad it was helpful!

  • @radoslavradev8477
    @radoslavradev8477 7 лет назад +1

    Maybe the best explanation I've saw so far! Great job man! Keep up the good work and I'm looking forward for videos bout the other topics from the "Interview question".

    • @seanallen
      @seanallen  7 лет назад

      Thanks Radoslav! I appreciate the kind words! I've got a few videos left in the "Interview Questions" series. Coming soon!

  • @iphyzll1091
    @iphyzll1091 7 лет назад +12

    Thank you so much, awesome explanation ! Keep it up :)

    • @seanallen
      @seanallen  7 лет назад

      Glad it helped you out! More are in the works!

  • @filipvabrousek6900
    @filipvabrousek6900 6 лет назад +2

    The best explanation I have seen! Thank you so much!

    • @seanallen
      @seanallen  6 лет назад

      Glad you enjoyed it, Filip.

  • @lamameme113
    @lamameme113 3 года назад

    Best explanation ever from my favorite youtuber ! Thank you for your effort ❤️

  • @RyuuzakiJulio
    @RyuuzakiJulio 4 года назад

    Quick and easy, good energy, love the graphics!! Best video!!

  • @semanurdin2777
    @semanurdin2777 3 года назад

    Thank you so much! Today Im going to interview hope that it will help.

  • @sheerazahmed9980
    @sheerazahmed9980 3 года назад

    Great piece of Information Sean. Very informative.
    Thank you!!

  • @SachinVerma-rc8ii
    @SachinVerma-rc8ii 3 года назад +1

    nice explanation!

  • @DimitrisTheo
    @DimitrisTheo 4 года назад

    Can you explain the difference between wrap and unwrap?

  • @yndrywjynsyn
    @yndrywjynsyn 6 лет назад +1

    Awesome! Quick and to the point! Should we add weak to all of our properties? Or only to half of them like you show in the video?

    • @seanallen
      @seanallen  6 лет назад

      Just the ones that will cause a retain cycle, like you see in the video. This happens when two objects reference each other.

  • @muneeburrehman6786
    @muneeburrehman6786 6 лет назад +1

    but the sequence of deallocating matters if we reverse the sequence Matilda = nil then Sean = nil , then Matilda would not be able to deallocate , right ?

  • @JayMutzafi
    @JayMutzafi 7 лет назад +5

    Forgive my ignorance, but the Person still has a strong reference to matilda. If you just set matilda to nil, wouldn't you still have that problem? or is it just because they both have strong references to each other that's the issue and as soon as you make one of them weak the problem is solved for both? Or should one make both of those weak?

    • @seanallen
      @seanallen  7 лет назад +4

      You guessed it. It's a retain cycle if there are two strong references pointing to each other. This cycle is what prevents Automatic Reference Counting from ever getting to zero, so the object can be deallocated. Setting one of them to weak will allow ARC to get to zero.

  • @natelee8671
    @natelee8671 6 лет назад +1

    Nice and neat explanation!

  • @Cdswjp
    @Cdswjp 7 лет назад +1

    Thanks a lot man. They are appreciated

  • @HarendraTiwari
    @HarendraTiwari 6 лет назад +1

    Appreciated, Nice tutorial Sean

    • @seanallen
      @seanallen  6 лет назад

      Glad you enjoyed it, Harendra!

  • @vanjhieparker972
    @vanjhieparker972 5 лет назад +2

    Hi Sean, best explanation ever! I am new developer and hoping to land jr developer gig someday. I would like to ask if how long it takes to learn coding before getting a gig. Response would highly appreciated.

    • @seanallen
      @seanallen  5 лет назад

      Happy the video was helpful. However your question is impossible to answer as it's different for everyone. Some people have gotten jobs after a few months of learning, for some people it takes a couple years... and everything in between.

  • @USNSWE
    @USNSWE 3 года назад +1

    Oh...So that’s why we put “weak var delegate: MyDelegate?” I learned something new today 🥸

    • @seanallen
      @seanallen  3 года назад

      Happy to help, Alexander.

  • @michaelluo7014
    @michaelluo7014 5 лет назад +1

    Thanks so much sir. Great videos. Really appreciate it

    • @seanallen
      @seanallen  5 лет назад +1

      Happy to help!

    • @michaelluo7014
      @michaelluo7014 5 лет назад

      @@seanallen can't wait to watch more videos from you. lol

  • @denizc1019
    @denizc1019 4 года назад +1

    Just perfect

    • @seanallen
      @seanallen  4 года назад

      Thanks Deniz. Glad you enjoyed it!

  • @Mani-xx4fz
    @Mani-xx4fz 5 лет назад +1

    Nice explanation :)

    • @seanallen
      @seanallen  5 лет назад

      Thanks! Glad you liked it.

  • @eduardodavila5213
    @eduardodavila5213 6 лет назад +1

    Thank you Sean!

    • @seanallen
      @seanallen  6 лет назад

      Happy to help, Eduardo 👍

  • @yndrywjynsyn
    @yndrywjynsyn 6 лет назад +1

    Is there swift code that prints out the number of ARCs on a class?

    • @seanallen
      @seanallen  6 лет назад

      Not that I'm aware of off the top of my head.

  • @JunaidKhan-ny8tu
    @JunaidKhan-ny8tu 3 года назад

    I've asked a question in interview that does creating AlertViewController and handling its handler method creates strong reference ?

    • @sheerazahmed9980
      @sheerazahmed9980 3 года назад +1

      yes they do, as Alert View is a class and uses completion blocks to handle its actions, it creates a retain cycle unless we use [weak self] closure list to handle them. Weak self capture list will allow the memory to deallocate the reference from the memory when its no longer needed.

  • @NikeTao
    @NikeTao 6 лет назад +1

    @seanallen Hey, Sean. I recently came across an article that says whenever you use "self" in a closure, you have to claim it as weak or unowned at the beginning of the closure [unowned self]. The article states it is because otherwise a strong reference will be created and not removed after the closure is exited. I have my doubts about it since I've never seen anyone user that kind of "self claiming" and thought the ARC takes care of such issues.
    Perhaps you could shed some light on the subject?

    • @seanallen
      @seanallen  6 лет назад

      Would you mind linking to that article so I can give an informed answer? Just curious about what it says.

    • @NikeTao
      @NikeTao 6 лет назад

      I'd love to, but unfortunately it's not in English. I have, however found a similar article on the topic (stablekernel.com/how-to-prevent-memory-leaks-in-swift-closures/ ). Apparently sometimes it should also be referred to as weak instead of unowned. What puzzles me is that's it's not always evident when it's good practice to use it and when it isn't. For instance, when writing the completion closure on a web request.

  • @hiteshchouhan4026
    @hiteshchouhan4026 5 лет назад

    Subscribed you, best explanation of ARC (please speak a little bit slow if you could :) )

    • @seanallen
      @seanallen  5 лет назад +1

      Glad you liked the video, Hitesh! This was from when I first started RUclips and wasn't fully comfortable in front of the camera. I've gotten better at slowing down over the years 😀

  • @ozsoffy
    @ozsoffy 4 месяца назад

    Thanks,
    This is a bit complicated for me, What is a good source to start with?

    • @seanallen
      @seanallen  4 месяца назад

      I don't have a specific source, but this topic only applies if you are using UIKit. SwiftUI does away with this concept (for the most part)

  • @tactteam00
    @tactteam00 7 лет назад +1

    Hey Sean, should we be putting deinit methods inside all of our classes from now on? Is there any instance where we'd have to do this in a struct?

    • @seanallen
      @seanallen  7 лет назад +1

      I wouldn't say you HAVE to put deinit methods in all of your classes. But if you wanted to be sure they were being removed from memory, you could use them. Basically, anything in that deinit method will fire when the class is removed from memory. You don't need deinit methods in structs because they are value types (copies, essentially), so there is no reference to it for a retain cycle. Check out my video on classes vs. structs for more details.

    • @tactteam00
      @tactteam00 7 лет назад +1

      Makes sense. Thanks!!!

  • @usharao5967
    @usharao5967 5 лет назад +1

    In real time do we need to make objects to nil? Or ARC handle?

    • @seanallen
      @seanallen  5 лет назад +1

      ARC will handle this the vast majority of the time. There are certain cases where you have to handle it, like in closures.

    • @usharao5967
      @usharao5967 5 лет назад +1

      Thank you

  • @mak_whisk
    @mak_whisk 2 года назад

    Thank you

  • @aliakkawi4759
    @aliakkawi4759 6 лет назад +1

    Thanks a lot

  • @MuscleTeamOfficial
    @MuscleTeamOfficial 3 года назад

    thank u!

  • @godpapel7981
    @godpapel7981 2 месяца назад +1

    u cooked fr

  • @szymonrybczak
    @szymonrybczak 4 года назад

    Thanks, for this video. It's very helpful, and btw our channel is the cool source of knowledge to the people who wants to become the iOS developer like me :D

    • @seanallen
      @seanallen  4 года назад +1

      Thanks Simon! Glad you are enjoying the channel!

  • @tusarjn
    @tusarjn 6 лет назад +1

    You are great

  • @MrEETT
    @MrEETT 5 лет назад +1

    useful video

    • @seanallen
      @seanallen  5 лет назад

      Thanks Ehsan. Glad you liked it.

  • @piyushkumarsonawane3683
    @piyushkumarsonawane3683 5 лет назад +1

    Nice explanation, but you speak too fast.

    • @seanallen
      @seanallen  5 лет назад

      Thanks. This is one of my VERY early videos. I'm improved my speaking speed greatly.

    • @piyushkumarsonawane3683
      @piyushkumarsonawane3683 5 лет назад +1

      @@seanallen you did fantastic job. Thanks for sharing good knowledge ☺️

  • @nautilus6358
    @nautilus6358 5 лет назад +1

    can you speak a bit slowly?

    • @seanallen
      @seanallen  5 лет назад

      This is one of my earlier videos. I've gotten a lot more comfortable in front of the camera, and it's better now.

  • @mohanbabu3289
    @mohanbabu3289 4 года назад

    Is anybody hit you back side. Why you r explanation is so fast . Explanation is good but very very very fast

    • @seanallen
      @seanallen  4 года назад

      This was one of my earlier videos. I've gotten a lot better on camera over the years.

  • @hansalucas
    @hansalucas 2 года назад

    I think I hate software development

    • @hansalucas
      @hansalucas Год назад

      ok four months later and after reading some theory, I get it. Thanks Sean

  • @jaskiratsingh6937
    @jaskiratsingh6937 6 лет назад +1

    please speaks slowly.

    • @seanallen
      @seanallen  6 лет назад

      I've worked on that in my recent videos. This is one of my older ones.

  • @ravindrabhati9369
    @ravindrabhati9369 4 года назад +4

    I like your video's, it's very helpful, please explain clouser with [weak self] and [unowened self] type of clouser are escaping clouser, non-esacaping clouser.
    And thankyou keep it up,👍👍

    • @seanallen
      @seanallen  4 года назад +2

      Those topics are on my "to-do" list for videos. It's just a very long list. I'll get to them eventually 😀

    • @ravindrabhati9369
      @ravindrabhati9369 4 года назад

      @@seanallen ok , I'll wait .

  • @tactteam00
    @tactteam00 7 лет назад +6

    Give this man all the subscriptions

  • @duncanwallace7241
    @duncanwallace7241 5 лет назад +3

    I am totally hooked. You’re videos are my newest must have addiction!!!! LOL!!!

    • @seanallen
      @seanallen  5 лет назад

      Haha, happy to hear you are enjoying the channel Duncan! I've got plenty of videos for ya.

  • @subinrevi1
    @subinrevi1 6 лет назад +3

    Clean and clear explanation and exactly to the point..way to go Sean !!

  • @kelcodes9701
    @kelcodes9701 6 лет назад +3

    Great video!
    Are circular references similar to retain cycles?

    • @seanallen
      @seanallen  6 лет назад +1

      Hey Coder K, I'm not super familiar with circular references (only time I've ran into that was in an Excel Spreadsheet, lol).

  • @vd853
    @vd853 6 лет назад

    Why can't Matilda *automatically* be removed if nothing else is referencing it?

  • @amirsmair71
    @amirsmair71 5 лет назад +2

    explanation with a coding example .. is the most value for developers .. Thanks sean

    • @seanallen
      @seanallen  5 лет назад

      Glad you liked it, Amir!

  • @mizech
    @mizech Год назад +1

    Best explanation by far.

  • @royce623
    @royce623 3 года назад

    I understand the concept, but what happens after setting MacBook var as weak in memory? Does it not store in memory or anything else. Plz any one can explain this. And one more for both classes person and MacBook if I set weak for both variable what happen?

  • @careercenter147
    @careercenter147 3 года назад

    How to make a reference live outside a method? Even though the references we create inside a method are strong, the Deinitializers is anyway called once the method exits. How to overcome this?

  • @hardipgajera2316
    @hardipgajera2316 4 года назад

    hello sean please make a video series for beginner to How to make ui better in xcode project

  • @monicaluo1122
    @monicaluo1122 4 года назад +1

    Thank you very much for the video - I don't know how did I survive in the past 2 years being an iOS dev without knowing this...actually I knew a little bit, but didn't see it as a serious problem due to my team leader fixed once for me and I just copied and pasted for the rest code.....

    • @seanallen
      @seanallen  4 года назад

      Happy to hear the video was helpful, Mengyi!

  • @monican7361
    @monican7361 3 года назад

    Thanks for this Good explanation

  • @ritatrang6182
    @ritatrang6182 6 лет назад +1

    I really appreciate your video. Is it better or worse in terms of MVC design pattern to put func createObjects in the model instead of the view controller? I find the MVC topic challenging.

    • @seanallen
      @seanallen  6 лет назад

      In general, you want to keep your view controller as light as reasonably possible. So logic that makes sense to be in the model, should be moved to the model.

  • @4rled
    @4rled 4 года назад

    hey just started a channel too

  • @geopadu
    @geopadu 7 лет назад +1

    Amazing dude. you won my subscription.

    • @seanallen
      @seanallen  7 лет назад

      Thanks for the sub, Sebastian! Glad you liked the video, and I hope you enjoy the rest of the channel!

  • @vsg24
    @vsg24 6 лет назад +1

    Fantastic explanation

  • @ShuklaAmbuj
    @ShuklaAmbuj 6 лет назад +1

    Hi Sean. Please write about MVVM and VIPER pattern.

    • @seanallen
      @seanallen  6 лет назад +1

      I don't have much experience using those, so I can't do a tutorial quite yet.

    • @ShuklaAmbuj
      @ShuklaAmbuj 6 лет назад

      Sean Allen no issue, thanks for responding. You are good at giving tutorials😊

  • @vovaarty1696
    @vovaarty1696 2 года назад

    Thanks Sean

  • @seanallen
    @seanallen  7 лет назад

    Have any questions on this tricky topic? Leave it in the comments. Happy to help.

    • @markonikolov9995
      @markonikolov9995 6 лет назад

      how many adidas nba t-shirts do you have? :D

    • @karlalalalalala
      @karlalalalalala 5 лет назад +1

      Hi Sean! I tried doing this but the `Person`s `deinit` is not getting called even after setting the `MacBook` owner property to `weak`. I tried initializing both objects without setting a value to each references and setting them to `nil` right away and `deinit`s are definitely triggered. Any idea why this is happening?

    • @seanallen
      @seanallen  5 лет назад

      Not sure. I include my source code for download in the description. Have you tried downloading that and seeing if our code is the same?

    • @karlalalalalala
      @karlalalalalala 5 лет назад

      @@seanallen Thanks for the reply! Really appreciate it. Here's what I found out. It was because I was using Swift's `dump` to print the objects on the console! I tried commenting out lines of code on mine and `deinit` is finally called right after I deleted those `dump`s! To be sure, I downloaded your project and tried adding `dump`s and wuzzah! `deinit` wasn't called from the `Person` class.

  • @thejask2765
    @thejask2765 3 года назад +1

    simple and clear!!..superb Sean. 👍🏻

    • @seanallen
      @seanallen  3 года назад +1

      Glad you liked it!

  • @TheOleYeller
    @TheOleYeller 7 лет назад +1

    Thank you so much for doing these!

    • @seanallen
      @seanallen  7 лет назад

      You're welcome. Glad you're getting value out of them!

  • @ecwb
    @ecwb 4 года назад +1

    Well explained great work Sean!

    • @seanallen
      @seanallen  4 года назад

      Thanks Steve. Glad you liked it. Thanks for the support 🙌

  • @EvanTempleton
    @EvanTempleton 4 года назад

    Beginner here, is it standard to end your classes and structures with a deinitialization? Thanks!

    • @seanallen
      @seanallen  4 года назад +1

      Only if you need to do something when the object is de-initialized. If you don't need to do anything, then no need to write out that function.

  • @rudolphhock1155
    @rudolphhock1155 3 года назад

    My question would be what impact would a weak reference have if it was simply to disappear ?
    I suppose one's code would be structured to expect, or handle, that situation, but data just being cut off must be a headache in itself ?

  • @multitudes389
    @multitudes389 5 лет назад +1

    well explained thanks!

    • @seanallen
      @seanallen  5 лет назад

      Happy to help, Laurence!

  • @israman30
    @israman30 7 лет назад +1

    Nice..! Thanks Sean.!

    • @seanallen
      @seanallen  7 лет назад

      Thanks again Patrick, feel free to reach out with any questions.

  • @Cdswjp
    @Cdswjp 7 лет назад +1

    From a pal out in Berkeley! Haha

    • @seanallen
      @seanallen  7 лет назад

      Hey CDMusicMusicGroup, glad you enjoyed the video! And hello, from the city!

    • @Cdswjp
      @Cdswjp 7 лет назад +1

      Have a good one brotha!

  • @j0seph672
    @j0seph672 5 лет назад

    straight forward explanation!

  • @iOS_Developer_Swift
    @iOS_Developer_Swift 5 лет назад +1

    Why u speak so fast 💨 ☺️☺️☺️

    • @seanallen
      @seanallen  5 лет назад

      This is an early video of mine. I've gotten a lot better.

    • @iOS_Developer_Swift
      @iOS_Developer_Swift 5 лет назад +1

      @@seanallen :D :D :D Thanks for tutorials

  • @dimitardimitrov7868
    @dimitardimitrov7868 3 года назад

    You speak so fast. I think not all people can understand well you.

    • @seanallen
      @seanallen  3 года назад +1

      This is a fair point on my early videos. I've worked on improving that in the past couple years (this video is from 2017).

    • @dimitardimitrov7868
      @dimitardimitrov7868 3 года назад

      @@seanallen thank you

  • @aniltodkar6199
    @aniltodkar6199 4 года назад

    A Good Explanation Sir. Thank You.

    • @seanallen
      @seanallen  4 года назад

      Happy to help, Anil.

  • @vikinggeorge7007
    @vikinggeorge7007 2 года назад +1

    This is such a nice memory management system. I want to make my own programming language like this.

  • @moosegoose1282
    @moosegoose1282 7 лет назад

    Video after video it is the same examples as shown in the apple documents. Why do you guys waste your time on this?? Call yourself programmers yet have little to no ingenuity.