Xamarin Memory and Performance
General Performance Optimization
- Original article here
IDisposable
- Good to use on images
- Can be called either directly or implicitly by wrapping an object tin a using statement, as shown below:
using (var image = UIImage.FromFile ("image.png")){
Draw (image);
}
Use the Linker
The linker provides three different settings to control its behavior:
Don’t Link
- No code will be linked away. Use only for debugging
Link SDK Assemblies Only
- reduces size of assemblies that are shipped by Xamarin. User code unaffected
Link All Assemblies
- aggressive optimization that will target the SDK assemblies and user code.
- If the testing does reveal that the linker has incorrectly removed a class or method it is possible to mark types or methods that are not statically referenced but are required by the application by using one of the following attributes:
MonoTouch.Foundation.PreserveAttribute
Android.Runtime.PreserveAttribute
- For instance, if you instantiate types dynamically, you may want to preserve the default constructor of your types. If you use XML serialization, you may want to preserve the properties of your types.
Android Specific
Threading
- Use the Task Parallel Library for threading
Object Pools
- Avoid Garbage Collection in Tight Loops
- Use object pools to avoid creating garbage that would trigger the garbage collector at runtime.
IOS Specific
Keeping Size Down
- If your code is generic-heavy then the final binary can get quite large since it will need to natively compile every generic possibility.
- Set the linker option to Link SDK assemblies.
- Make sure you're building for a single architecture (ARMv7). FAT binaries (e.g. ARMv7 and ARMv7s) are built twice need twice the space.
- Make sure you have not enabled the Debug build. Check the box for Release build
- Make sure you're using the LLVM compiler. It takes more time to compile but generates better (and smaller) code.
- Reduce managed code size. The easy way to do this is the enable the linker on every assembly, i.e. Link all assemblies in your project options.
- TBC