yuvaraju
yuvaraju

Reputation: 167

How to fix memory leaks in iOS applications?

I have found few memory leaks when I am running my application. For your reference, I am sharing the screenshots of Instrument debug logs and also Xcode Debugg memory graph tool. I am not getting what is going wrong here. Please help me to resolve memory leaks.

Memory debugger graph

enter image description here

Please help me to fix the memory shows in the image. Thank you.

Upvotes: 3

Views: 5906

Answers (4)

gnasher729
gnasher729

Reputation: 52530

In this case, just because some tool says you’ve got a memory leak doesn’t mean you have one. The amount of data seems to be less than 1MB, that’s nothing. Your tool suspects there’s a leak because data was allocated and not released, but these are single objects. Quite possible that memory is going to be released or reused later.

Use your software for hours and check if memory usage gets larger.

Upvotes: 0

Rob
Rob

Reputation: 437382

I know this is an old question, but a few observations.

  1. When you have a lot of objects that are leaking, focus on high level objects (especially your own classes). We do not care why the array was not released. We care about why the item that is keeping a strong reference to it was not released. XTubeManager and GlueTubeManager are probably good places to start searching, but I don't know what other high level objects appear in the panel on the left.

  2. When you have leaks, it is very useful to use the “Malloc Stack Logging” feature. So edit your scheme (command+<; or “Product” » “Scheme” » “Edit”) and go to the “Diagnostics” section and temporarily turn on “Malloc Stack Logging”:

    enter image description here

    Then, when you select a leaked object, you can see where it was instantiated but using the memory inspector panel on the right:

    enter image description here

    Look for entries in that stack trace that are white (your code), as opposed to all the system items that are in gray. When you hover items in that stack trace, there is even a little arrow that lets you jump to the code in question. In my example, the leaked object was instantiated in viewDidLoad.

    This will not tell you why it leaked, but you will be able to see where the object in question was instantiated in your code, and you can start your investigation from there, diagnosing why an object created at that point in your code still has lingering strong references.

    Remember to turn off the “Malloc Stack Logging” feature when you are done with your diagnostics.


As an aside, when we see URLSession objects that were not released, that begs the question of whether you instantiated a URLSession object (rather than using the shared instance) and neglected to call finishTasksAndInvalidate. Ideally, you would have a single URLSession and reuse it for all network activity (or use the shared instance), but if you must instantiate sessions, make sure to invalidate them when you are done.

Upvotes: 3

mfaani
mfaani

Reputation: 36257

You do NOT need to use Instruments. That's the old way. Use Xcode itself.

See Visual Debugging with Xcode - 24:45

Watching the video is a MUST, but the summary of the video is as such:

There are two type of memory problems. You just have to repeat a flow in your app 2-3 times to be certain the memory graph has caught it

  • Leaks. Xcode will annotate this with purple icon. Possible are: delegates, closures
  • Abandoned memory. Xcode will not annotate this. But it's still increases your memory footprint. possible examples are: A repeating timer that is never invalidated, NotificationCenter, A never ending DispatchWorkItem

For Leaks the memory graph is a loop ie two way.

For Abandoned memory the graph is NOT two way. It's just an object one that Apple categorizes as 'root path' referencing your object and never letting it go. For more on this see here

Upvotes: 0

Akagbusi Precious
Akagbusi Precious

Reputation: 1

Memory on mobile devices is a shared resource. Apps that manage it improperly run out of memory, crash, and suffer from drastically decreased performance. so to fix it follow this steps Open Xcode and build for profiling. Launch Instruments. Use the app, trying to reproduce as many scenarios and behaviors as possible. Watch for leaks/memory spikes. Hunt down the source of the memory leaks. Fix the problem.

Upvotes: -1

Related Questions