Xamarin.Android Garbage Collection Improvements

Rodrigo Kumpera

Mobile operating systems continue to evolve year over year, delivering great APIs for developers to integrate into their apps to create delightful experiences. However, a side effect of this is that the size of the mobile apps that developers are creating on Xamarin is growing. When we designed Xamarin.Android, our Garbage Collector integration was built to handle small to medium sized apps.

Garbage-Sorting-RobotIn large applications this integration, also known as the GC Bridge, was brought to our attention as a source of longer than expected pauses. We took time to dive into the problem and remedy it by introducing two new bridge modes in Xamarin.Android 4.14, currently in the stable channel.

The GC Bridge is responsible for bringing two worlds together: Java and C#. It takes care of the objects that exist on both sides, such as Activity, and makes their lifetime correct for both the Java garbage collector and Mono’s.

Let’s take a look at a benchmark test that creates objects and randomly connects them, which is similar to deep object models we’ve seen in real world apps.

GC Performance 1

As you can see, performance improvements in the new GC bridges are quite drastic. Performance improves 2x to 10x between what we used to get with 4.12 and the new Tarjan mode.

The GC Bridges

Before diving into more details and performance benchmarks, I want to take a moment to describe the different GC Bridges.

    • Old – The default implementation, considered the most stable of the three. This is the bridge that an application should use if the GC_BRIDGE pauses are acceptable.
    • New – A major overhaul of the original code, fixing two instances of quadratic behavior but keeping the core algorithm, based on Kosaraju’s algorithm for finding strongly connected components.
    • Tarjan – A completely new design of the GC Bridge based on Robert Tarjan’s algorithm and backwards reference propagation. It does perform the best under our simulated workloads but has a larger share of experimental code.
    • You can learn more about the Garbage Collector in Xamarin.Android in our Android documentation.

Better, Faster, Stronger

Let’s take a look at a few more performance benchmarks to really see how these new GC Bridges increase the overall app performance.

GC Performance 2

This second benchmark simulates the case where a very popular object is referenced by many objects, which happens frequently when the GUI has lots of callbacks to a central backend object. As you can see, it’s not just faster, but scalable, as the number of incoming links barely affects performance.

GC Performance 3

This last benchmark creates a structure where half of the objects point to a single object that in turn points to the other half of the objects. It’s not common in practice, but it shows how both new bridge modes are necessary – each one shines when used on different workloads, which can only be discovered by trying both.

Enabling this new functionality is as easy as adding an environment.txt file to your project with the BuildAction set to AndroidEnvironment. Then you can specify which GC_BRIDGE option your application should use by passing bridge-implementation=old, bridge-implementation=new, or bridge-implementation=tarjan to the MONO_GC_PARAMS environment variable. Here is an example:

MONO_GC_PARAMS=bridge-implementation=tarjan

Enabling Garbage collection

Our awesome documentation explains in detail how to enable the new bridges so you can make your apps more responsive today.

Learn More

Xamarin Evolve 2014 banner with dates (October 6-10) and location (Atlanta, Georgia)To learn even more about GC Bridges, be sure to join me at Xamarin Evolve 2014, where I will be deep diving into GC and performance on both iOS and Android during multiple sessions.

0 comments

Discussion is closed.

Feedback usabilla icon