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
Watch Next - iOS Take Home Project - Job Interview Practice - Free Preview - ruclips.net/video/MSIe2y6Fee8/видео.html
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!
That's awesome to hear! Best of luck with the interview. Thanks for stopping by with the update 😀
Finally someone explained this clearly. Keep killing it man.
Thanks Arjun 👍 I appreciate it!
This is the best explanation I've seen so far !
Thanks! Glad you enjoyed it
Oh god! what a great explanation. Finally I understand what is the weak references.
Glad it was helpful!
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.
It's on my video to-do list. It's just a VERY long list. Too many videos, too little time!
I’ve gotten a question yesterday about this during an interview! I’m glad I could explain it thanks to Sean 😄
Nice! Happy to hear I could be of some help! Best of luck on getting the job 😀
omg thank you so much. such a simple example and explanation. im new in IOS Dev, so many keywords are intimidating me lately
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.....
Glad you're enjoying the content, Vignesh!
Finally I understand the concept clearly.. thanks Sean for wonderful video.👍
Glad it was helpful!
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".
Thanks Radoslav! I appreciate the kind words! I've got a few videos left in the "Interview Questions" series. Coming soon!
Thank you so much, awesome explanation ! Keep it up :)
Glad it helped you out! More are in the works!
The best explanation I have seen! Thank you so much!
Glad you enjoyed it, Filip.
Best explanation ever from my favorite youtuber ! Thank you for your effort ❤️
Quick and easy, good energy, love the graphics!! Best video!!
Thank you so much! Today Im going to interview hope that it will help.
Great piece of Information Sean. Very informative.
Thank you!!
nice explanation!
Glad you liked it!
Can you explain the difference between wrap and unwrap?
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?
Just the ones that will cause a retain cycle, like you see in the video. This happens when two objects reference each other.
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 ?
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?
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.
Nice and neat explanation!
Thanks Qasa 👍
Thanks a lot man. They are appreciated
Appreciated, Nice tutorial Sean
Glad you enjoyed it, Harendra!
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.
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.
Oh...So that’s why we put “weak var delegate: MyDelegate?” I learned something new today 🥸
Happy to help, Alexander.
Thanks so much sir. Great videos. Really appreciate it
Happy to help!
@@seanallen can't wait to watch more videos from you. lol
Just perfect
Thanks Deniz. Glad you enjoyed it!
Nice explanation :)
Thanks! Glad you liked it.
Thank you Sean!
Happy to help, Eduardo 👍
Is there swift code that prints out the number of ARCs on a class?
Not that I'm aware of off the top of my head.
I've asked a question in interview that does creating AlertViewController and handling its handler method creates strong reference ?
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.
@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?
Would you mind linking to that article so I can give an informed answer? Just curious about what it says.
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.
Subscribed you, best explanation of ARC (please speak a little bit slow if you could :) )
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 😀
Thanks,
This is a bit complicated for me, What is a good source to start with?
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)
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?
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.
Makes sense. Thanks!!!
In real time do we need to make objects to nil? Or ARC handle?
ARC will handle this the vast majority of the time. There are certain cases where you have to handle it, like in closures.
Thank you
Thank you
Thanks a lot
thank u!
u cooked fr
Thanks 🧑🍳
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
Thanks Simon! Glad you are enjoying the channel!
You are great
Thanks Tushar!
useful video
Thanks Ehsan. Glad you liked it.
Nice explanation, but you speak too fast.
Thanks. This is one of my VERY early videos. I'm improved my speaking speed greatly.
@@seanallen you did fantastic job. Thanks for sharing good knowledge ☺️
can you speak a bit slowly?
This is one of my earlier videos. I've gotten a lot more comfortable in front of the camera, and it's better now.
Is anybody hit you back side. Why you r explanation is so fast . Explanation is good but very very very fast
This was one of my earlier videos. I've gotten a lot better on camera over the years.
I think I hate software development
ok four months later and after reading some theory, I get it. Thanks Sean
please speaks slowly.
I've worked on that in my recent videos. This is one of my older ones.
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,👍👍
Those topics are on my "to-do" list for videos. It's just a very long list. I'll get to them eventually 😀
@@seanallen ok , I'll wait .
Give this man all the subscriptions
Much appreciated!
I am totally hooked. You’re videos are my newest must have addiction!!!! LOL!!!
Haha, happy to hear you are enjoying the channel Duncan! I've got plenty of videos for ya.
Clean and clear explanation and exactly to the point..way to go Sean !!
Thanks Subin!
Great video!
Are circular references similar to retain cycles?
Hey Coder K, I'm not super familiar with circular references (only time I've ran into that was in an Excel Spreadsheet, lol).
Why can't Matilda *automatically* be removed if nothing else is referencing it?
explanation with a coding example .. is the most value for developers .. Thanks sean
Glad you liked it, Amir!
Best explanation by far.
Glad it was helpful!
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?
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?
hello sean please make a video series for beginner to How to make ui better in xcode project
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.....
Happy to hear the video was helpful, Mengyi!
Thanks for this Good explanation
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.
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.
hey just started a channel too
Amazing dude. you won my subscription.
Thanks for the sub, Sebastian! Glad you liked the video, and I hope you enjoy the rest of the channel!
Fantastic explanation
Hi Sean. Please write about MVVM and VIPER pattern.
I don't have much experience using those, so I can't do a tutorial quite yet.
Sean Allen no issue, thanks for responding. You are good at giving tutorials😊
Thanks Sean
Have any questions on this tricky topic? Leave it in the comments. Happy to help.
how many adidas nba t-shirts do you have? :D
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?
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?
@@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.
simple and clear!!..superb Sean. 👍🏻
Glad you liked it!
Thank you so much for doing these!
You're welcome. Glad you're getting value out of them!
Well explained great work Sean!
Thanks Steve. Glad you liked it. Thanks for the support 🙌
Beginner here, is it standard to end your classes and structures with a deinitialization? Thanks!
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.
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 ?
well explained thanks!
Happy to help, Laurence!
Nice..! Thanks Sean.!
Thanks again Patrick, feel free to reach out with any questions.
From a pal out in Berkeley! Haha
Hey CDMusicMusicGroup, glad you enjoyed the video! And hello, from the city!
Have a good one brotha!
straight forward explanation!
Why u speak so fast 💨 ☺️☺️☺️
This is an early video of mine. I've gotten a lot better.
@@seanallen :D :D :D Thanks for tutorials
You speak so fast. I think not all people can understand well you.
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).
@@seanallen thank you
A Good Explanation Sir. Thank You.
Happy to help, Anil.
This is such a nice memory management system. I want to make my own programming language like this.
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.