3 Different Techniques to Find Memory Leaks in iOS | by Vitor Ferraz Varela | Apr, 2022

When growing for iOS or another platform, it is rather essential to take into account that your buyer expectations are very excessive to your software to carry out very nicely. They shouldn’t fear if the app responds slowly or if the interface is sluggish or any sort of drawback.

Think about that your clients are paying their payments and immediately the app crashes? That isn’t the expertise they’re anticipating and on this case, it’s even worse as a result of they couldn’t pay their payments.

That’s why after we are coping with reminiscence administration, it is rather essential to watch out with reminiscence leaks.

Reminiscence leaks are very associated to how swift offers with reminiscence administration and it happens when a decided object in reminiscence couldn’t be recovered by the Computerized Reference Counting (ARC).

The ARC is liable for monitoring and managing your app’s reminiscence utilization and likewise releasing up the reminiscence utilized by class situations when these situations are not wanted.

A retaining cycle is among the most typical issues that may trigger reminiscence leaks in your apps.

Each variable that you simply create has a robust reference by default.

While you create a variable inside a category referencing one other class, you have got a robust relationship between the 2 courses.

The next instance illustrates the thought:

So each time that we create situations of these courses we’re printing that the objects have been initialized and likewise once they have been deallocated from reminiscence.

After we create a relationship between these objects and attempt to set these objects to nil and verify the output we don’t see the deinit print.

The way to repair it?

We will break this robust retain cycle by marking one of many properties as weak or unowned.

The situations can then refer to one another with out creating a robust reference cycle. As in comparison with robust, weak doesn’t increment the reference rely and ARC can launch the objects from reminiscence.

We mounted the issue with objects having a bidirectional relationship, however there’s one other widespread approach to create reminiscence leaks and it’s after we are utilizing closures.

Closure captures variables and constants from its surrounding scope. This creates a robust reference to it and the worth wanted by the closure.

It’s fairly potential to have hundreds of closures in our mission and checking each one in every of them for reminiscence issues could possibly be very tough.

Xcode has a function that enables us to watch for reminiscence leaks, you simply have to open Devices e search for the choice Leaks

after that choose the simulator that you’re testing and the applying goal.

For our instance let’s check out the code beneath:

After we run the devices we are able to see that now we have a reminiscence leak and the objects with the leak.

Checking our code we discover that our consumer and server have a robust relationship with one another and to interrupt that robust reference we are able to use weak or unowned as the instance beneath:

We will repair the reminiscence leak and alter the property to weak or unowned:

Operating once more the devices, we are able to see that there aren’t any reminiscence leaks and all deinitmessages are being printed within the console.

SecondViewController object was deallocated
Server object was deallocated
Shopper object was deallocated

One other tip is to the debug navigator and verify the reminiscence used. When there’s a leak, the reminiscence will not be deallocated inflicting the reminiscence utilization to extend over time.

Xcode Devices is a good device for locating potential reminiscence leaks however typically it’s not straightforward to determine the issue. We additionally have to run it each time to verify for a possible leak and monitor the reminiscence utilization.

We will use one other approach to repeatedly monitor for potential reminiscence leaks. Xcode permits us to create a symbolic breakpoint, on this case, we are able to create a breakpoint to take heed to the view controller’s dealloctechnique and verify if the view controller was deallocated from reminiscence.

We will additionally detect potential reminiscence leaks by writing unit checks for our courses, and that’s the finest and extra scalable approach to forestall any potential reminiscence leaks.

After fixing the retaining cycle between the objects, we run the checks once more:

Lastly, we are able to write an extension on the XCTestCase and use it to check reminiscence leaks on each object.

An important rule of thumb is to at all times use weak or unowned when coping with closures or delegates.

Create a robust code model to your mission, this manner the absence of a weak self will likely be noticeable.

Lastly use some sort of linter, like SwiftLint, an amazing device that enforces you to stick to a code model. You possibly can detect points at compile time and it helps automate code model to your workforce.

I hope you’ve got loved studying this text and found new methods to seek out reminiscence leaks in your code.

Need to Join?Linkedin | Instagram | Twitter

More Posts