Memory leaks in iOS

VictorConnerVictorConner SEMember, University ✭✭


I recently attended the Memory management best practice class which i thought was really good and introduced a few important topics including a topic which i thought was the most important one, memory leaks.

After attending this class I wanted to check out if my app had any memory leaks, which it unfortunately had.

I have been building my iOS app following the same architecture as the Xamarins shirt-store app which i downloaded from your webpage.

I spent about 2hours with the profiler trying to resolve my memory leaks before i thought about testing the shirt store app itself.

From what I could make out this app had alot of memory leaks aswell and after clicking around in it for about 5 minutes it occupied 500mb of my iOS simulators memory and had over a hundred references to different custom created objects that never got cleaned up.

I kinda of assumed that the projects(specially the bigger one's) from your webpage and github repo were all created using best practise for building apps with Xamarin.

This has left me with quite a few questions such as; How do you guys deal with memory leaks? Do you manually call dispose everywhere? How do you troubleshoot it? Is overriding dispose and adding a console.writeline a good approach to make sure your objects are being garbage collected?


  • rene_ruppertrene_ruppert DEXamarin Team, University, XamUProfessors Xamurai

    I could argue now that we made the Shirt Store app like that on purpose so that you would use it to spot the memory issue it contains...but, that would be a too obvious feint.

    Seriously: I have checked the Shirt App on iOS and indeed it will eventually run out of memory and I will investigate it further, to see what's going on there. I will share my findings here.

    How to deal with memory issues generally cannot be answered. But let me first point out that we are (most probably) not leaking memory here. We're dealing with abandoned memory. That means, some objects are still referenced that should not. This problem also exists in many desktop and server apps that are built with .NET - however there you hardly will notice: there's tons of memory available and desktop OS offers swapping to disk. iOS is not very forgiving: not releasing memory will lead to your app being killed. A good strategy is to constantly check memory usage and use automated UI tests to perform repeated tasks.

  • VictorConnerVictorConner SEMember, University ✭✭

    @rene_ruppert Thanks a lot please share if you make any findings. As of now I bought a book on Swift and will learn iOS programming that way. At least for now. I spend alot of time just running the profiler over and over trying to figure out why my app's are running out of memory. Ive done everything that was taught in the Memory management class; Unsubscribing to events, not referencing other native objects and so on. Still my app does run out of memory and never seem to get any back.

    As of now it feels like Im over thinking this and spending more time profiling my app than i am developing which kinda sucks. Asking other people this seem like a common scenario and a lot of people seem to think that most Xamarin iOS apps will have leaks, and thats that. What are your thoughts on this? Is that the truth or is it just bad practice and a lack of knowledge from us new mobile developers?

    I really like xamarin and c# but the lack of documentation and answers to this issue is kinda of forcing me towards Xcode and swift, something i really dont want to do. :(

  • VictorConnerVictorConner SEMember, University ✭✭

    @rene_ruppert Did you come up with anything?

  • rene_ruppertrene_ruppert DEXamarin Team, University, XamUProfessors Xamurai

    The app really has some problem. Most of them come from not unsubscribing from events. The fact that each managed iOS object has an unmanaged counterpart can lead to cycles that prevent garbage collection. The problems of the app are part of the reason why I created this blog post:
    Please note that we are not talking about "leaks"; what we see here is caused by not using Xamarin.iOS and events correctly! A good read is probably this article:,_testing,_and_metrics/memory_perf_best_practices/#iOS_Specific_Memory_Considerations

    Hope that helps!

  • SebastianSeidel.9226SebastianSeidel.9226 DEInsider, University ✭✭✭✭

    Hi @rene_ruppert you said

    A good strategy is to constantly check memory usage and use automated UI tests to perform repeated tasks.

    I often thought of that too but did not find a way to combine the tools given. Do you know a way of combining the Profiler or Instruments with Xamarin.UITest? That would be a perfect match to profile any leaked or a abandoned memory.

  • rene_ruppertrene_ruppert DEXamarin Team, University, XamUProfessors Xamurai

    Xamarin Profiler is your friend. Take Snapshots and investigate the references.

Sign In or Register to comment.