Intermediate Swift Podcast Pilot — Memory Management

Tiago Martinho
7 min readMay 9, 2018

--

Last Sunday me and Lewis decided to record our first podcast.

This podcast was targeted at iOS Developers that already did an online course, and maybe shipped one app into the Store, but they still want to learn this.

We decided to talk about a different problem we were facing each week regarding the applications we are working on.

For the first episode we decided to talk about a problem that Lewis was facing regarding Out of Memory (OOM) Terminations of his application.

You can find the podcast here:

Transcript:

Hi Lewis! Hi Tiago! we are discussing today problem i’ve been having memory management in my iOS app the problem that i’ve got is i’ve been getting a crash in crashlitcs makes absolutely no sense to me and i’ve googled around a bit i found out it’s a memory issue there usually in crash licks the crashes come up and they say where in the application in the code that crash wass but in this case he’s completely random on dh it says instead of having like a reason for the cross it says bob access current involved address okay and google that and it’s come up with this is a memory management problem yeah, so a bit of background with kind of a where i marked with memory management i’ve done in the course is in the swift book that i took in the past i read about memory management i understand the theory of retain cycles but i don’t like really like i’ve never practically applied it okay ? Okay ? And so that’s where the crash actually is like affecting my life has been about fifty crashes for users with this so it is quite a fishing problem so i’m trying to solve that now all right ? So the press that you’re seeing about axis colonel involve address it’s it seems like a that there is a memory leak because this is you know, when the memory keeps on increasing so i’m familiar with the concept ofthe stark and heap like hey i i i haven’t i did what they are placed like i learned the ferry when i was kind of like everyone learning very of like building ops but i’ve never practically applied that knowledge because even though i know what a stock in a heap is, i don’t really know like why this is a ok basically went when the memory gets full like he starts to corrupt yeah the memory place where your application is your actual code so the objects that the memory the optics on the allocating over write the code and then you get these weird crash because you’re trying to point to some part of your program that now we got it corrected so that by corrupted you’re saying something that was in memory has now been replaced yeah exactly and then to be sure like in the fabric of other crash reporting cannot see how much left iran wass this could be like indicated to see ok it’s definitely american week if it is really low and you just before we came on you know as i opened the fabric on my computer you pointed out that guy’s very low yeah, you should go to different users and see how free round they had that could be a way to say this is definitely memory week yeah like that never ok aziz, you can see definitely like yeah three percent run free it’s really it’s pointing toe german really ? You can look at the statistics but they’re now how you go about solving it the other way is using the instrument so besides excluding huh ? Thieves took of instruments that has set off profilers is basically a profiler that you can use toe attacked locations off memory and see for europe is releasing it while it is is consuming that that is like save the ap away and advanced way and for backed it will be really helpful to see like them the dc videos they tell you how toe running like one hour it and start is in them but sometimes they can be complicated to use especially people don’t have experience are it’s a double life i have tried to be oh then the easiest way it’s simply you put like a prince statement in the cave man dividing where you put a print statement in the initializing i saw you off all your view controllers you like you can you override the the the need my mittens and in there you would have been set him like ok, the issue controller looking you control was the allocator after i did the logging that should be the case asked was it so i’ve heard of that, but why specifically would you do it from view controllers ? You people go yeah we’re assuming that is of your controller that is leaking but it’s not guarantee usually the the and the most travel things unless you don’t have about off objects that are very constantly the view controls are the most costly things they’re set of the view and everything but that will be an easy way to do like putin it i actually found a memory week in maria in my application doing that because what was released i put that in eating the player so when someone opened a channel they will see the player when they close it i will not see that you need so i knew what the problem was in in the players you control because i was presenting in business ing it that could be a first like easy way to get to get something done on d c there is also thiss project indeed typical lifetime tracking from priest off on dh what what it does it monitors each time that a memory with happens is basically like a helper that you can put on europe that is the same as instrument does but in a more easy way i think this is instruments put a completely different kind of yeah but in europe like your city’s bottom bar and you keep like is europe and see if it creates memory weak and when it gets very leaking identified in the act open be using it sire bombard that’s an actual you know i think that when i’m people be my app i can see what’s causing it yeah ok that’s if i had to pull it out like come on news cases where memory is not weak one is what you make enclosures if you’re using closures in safety and they’re not using when you’re raising a closure on juries in variables from the surrounding and in its two captured you need to yourself like a compiler tells you never hear from you yes so in there you’re capturing self and if like the closure leaves after the object they it’ll break everything cycle you so it started that this happens you should use the weak self or unknown self keywords to retain the south and in the closure that sylvia way to do another fee that i saw commonly doing memory weeks was in a delegation if there is an allegation pattern yeah different okay and in that case like if your object as a reference to the delegate and for some reason there like it has also referenced back to these object there is an early leak in this case your protocol developed it should be declared us class on do you put the week keyword in the balance so i can think of instances in miami where i’ll be using a table on dh take the youth the view controller will be that will like how many extensions the table view delegate okay on potentially i know this is by design i will have referenced the table view from the delegate ok case i don’t know because it’s exacting the only other delegating kiss off listen out of all the events from the movies like the weekly did yet data sources yeah that is that there will be not a problem because they will leave the same property so we have the same life yeah because when mommy goes out the other also is not even ok in that case it will be okay i’m seeing more like imagine that you have like a view any reference from the view controller the view but want to get a fight as well from some events in this case the hugeness of the cloud as weak and that’s the reason why you create like a reference from the storyboard no i be the outlets and everything they are declared this week that’s the reason this is this is a is a good example of what we’re trying to do with the podcast because how many people would have in its sorry stop don’t i mean a week yes that’s what that’s what to do and i was also told you that i never fall through like why the reason is that you control are the reference to the view but he doesn’t have to be notified in tow the view so they have a residence to each other so why why isn’t every variable cloud is a weak variable. Is there any downside to declare it as we’ve learned, you first, extra key word. Okay, really, with, but also, like, if you, if you want to be sure, because if you did quite as weak in swift, every viable needs to be educated, and it cannot be new. If it is, it is, it can be knew this to be declared a resumption of you. So in that case, if you declared this week, you will need to declare the viable as optional. Ok. In that case, if you are sure that the valuable is there, then you can declare it, as in policing.

--

--

Tiago Martinho

Indie Developer | Swift Peer Lab Barcelona | I love to create apps | Studying Machine Learning