Avoiding common memory pitfalls
Memory on mobile devices is certainly not an unlimited commodity. Because of this, memory usage in your application can be much more important than on desktop applications. At times, you might find the need to use a memory profiler or improve your code to use memory more efficiently.
The following are the most common memory pitfalls:
The garbage collector (GC) is unable to collect large objects fast enough to keep up with your application
Your code inadvertently causes a memory leak
A C# object is garbage collected, but is later attempted to be used by native code
Let's take a look at the first problem, where the GC cannot keep up. Let's say we have a Xamarin.iOS application with a button for sharing an image on Twitter, as follows:
twitterShare.TouchUpInside += (sender, e) => { var image = UImage.FromFile("YourLargeImage.png"); //Share to Twitter };
Now let's assume the image is a 10 MB image from the user's camera roll. If...