How to fix and find memory leaks in iOS?

JPC Technologies
2 min readJan 5, 2016

--

Common to all iOS developer, one among the important parts of the iOS application development is memory management and performance of iOS application. At development time although we attempt to do proper memory management, still we tend to depart some memory leaks which may lead to major issue in application at any stages.

What is a memory leak and why should I care?

A memory leak is when your program loses track of a piece of memory that was allocated. The consequence is that the “leaked” memory will ne’er be freed by the program. This mostly happens when a bit of code does a “new” or a “malloc” (or an “alloc” in Objective-C) but never does a corresponding “delete”, “free” or “release”, respectively.

When you new, malloc, or alloc, what the OS is doing is giving your program a bit of memory on the heap. The OS is states that, “here, have this block of memory at this memory address.” It expects you to hold a regard to that memory address (usually within the type of a pointer) and it’s relying on you to inform the OS when you’re finished it (by calling free, delete, or release).

Memory leaks happen when you throw away your pointer to that memory. If your program no longer is aware of where on the heap your memory is allocated, however are you able to ever free it?

So why should you care? In the most minor case, you’re wasting memory which will be freed when the user quits your app. within the worst case, you may have a memory leak that happens each frame. That’s an honest way to wind up crashing your program, specifically if a user lets it run for a long time. (Note: in iOS you can’t leak system-wide memory. within the worst-case you\’ll cause your app to run out of memory and crash, however you can’t bring the entire system down).

How do I know I’ve got a memory leak?

Some memory leaks are simple to see by viewing your code. Some are much more difficult. this can be where Instruments comes in. Instruments contains a “Leaks” tool which will tell you exactly where you’re leaking memory so you’ll get in there and fix it!

The best way to keep track of such memory leaks is to use the Instruments provided in iOS sdk. Instrument is a performance, analysis and testing tool for dynamically tracing and profiling OS X and iOS code. It’s a versatile and powerful tool that helps you to track one or a lot of processes and examine the collected information. During this way, Instruments can assist you to know the behavior of your app.

Read More >>>

--

--

JPC Technologies

JPC Technologies - Idea to Innovation. We are a highly passionate group of technologists, active in IT services, IT solutions and consulting space!