{"id":13948,"date":"2014-09-08T12:50:53","date_gmt":"2014-09-08T16:50:53","guid":{"rendered":"http:\/\/blog.xamarin.com\/?p=13948"},"modified":"2014-09-08T12:50:53","modified_gmt":"2014-09-08T16:50:53","slug":"xamarin-android-garbage-collection-improvements","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/xamarin-android-garbage-collection-improvements\/","title":{"rendered":"Xamarin.Android Garbage Collection Improvements"},"content":{"rendered":"<p>\t\t\t\tMobile operating systems continue to evolve year over year, delivering great APIs for developers to integrate into their apps to create delightful\u00a0experiences. However, a side effect of this is that the size of the\u00a0mobile apps that developers are creating on Xamarin\u00a0is\u00a0growing. When we designed Xamarin.Android, our Garbage Collector integration was built\u00a0to handle small to medium sized apps.<\/p>\n<p><img decoding=\"async\" class=\"alignright size-medium wp-image-14042\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/Garbage-Sorting-Robot-300x192.jpg\" alt=\"Garbage-Sorting-Robot\" width=\"300\" height=\"192\" \/>In 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.<\/p>\n<p>The GC Bridge is responsible for bringing two worlds together: Java and C#. It takes care of the\u00a0objects that exist on both sides, such as Activity, and makes their lifetime correct for both the Java garbage collector and Mono&#8217;s.<\/p>\n<p>Let&#8217;s take a look at a benchmark test that creates objects and randomly connects them, which is similar to deep object models we&#8217;ve seen in real world apps.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-13949\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/GC-Performance-1.png\" alt=\"GC Performance 1\" width=\"600\" height=\"371\" \/><\/p>\n<p>As you can see, performance improvements in the new GC bridges are quite drastic. Performance improves 2x to 10x between\u00a0what we used to get with 4.12 and the new Tarjan mode.<\/p>\n<h2>The GC Bridges<\/h2>\n<p>Before diving into more details and performance benchmarks, I want to take a moment\u00a0to describe the different GC Bridges.<\/p>\n<ul>\n<ul>\n<li><b>Old<\/b> &#8211; 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.<\/li>\n<li><b>New<\/b> &#8211; A major overhaul of the original code, fixing two instances of quadratic behavior but keeping the core algorithm, based on <a href=\"http:\/\/en.wikipedia.org\/wiki\/Kosaraju's_algorithm\" title=\"Kosaraju's Algorithm\">Kosaraju&#8217;s algorithm<\/a> for finding strongly connected components.<\/li>\n<li><b>Tarjan<\/b> &#8211; A completely new design of the GC Bridge based on <a href=\"http:\/\/en.wikipedia.org\/wiki\/Tarjan's_strongly_connected_components_algorithm\" title=\"Tarjan's\">Robert Tarjan&#8217;s algorithm and backwards reference propagation<\/a>. It does perform the best under our simulated workloads but has a\u00a0larger share of experimental code.<\/li>\n<li>You can learn more about the <a href=\"http:\/\/developer.xamarin.com\/guides\/android\/advanced_topics\/garbage_collection\/#GC_Bridge_Options\" title=\"GC Documentation\">Garbage Collector in Xamarin.Android<\/a> in our Android documentation.<\/li>\n<\/ul>\n<\/ul>\n<h2>Better, Faster, Stronger<\/h2>\n<p>Let&#8217;s take a look at a few more performance benchmarks to really see how these new GC Bridges increase the overall app performance.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-13950\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/GC-Performance-2.png\" alt=\"GC Performance 2\" width=\"600\" height=\"371\" \/><\/p>\n<p>This second benchmark simulates the case where a very popular object is referenced by many objects, which\u00a0happens frequently when the GUI has lots of callbacks to a central backend object. As you can see, it&#8217;s not just faster, but scalable, as the number of incoming links barely affects performance.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-13951\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/GC-Performance-3.png\" alt=\"GC Performance 3\" width=\"600\" height=\"371\" \/><\/p>\n<p>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&#8217;s not common in practice, but it shows how both new bridge modes are necessary &#8211; each one shines when used on different workloads, which can only be discovered\u00a0by trying both.<\/p>\n<p>Enabling this new functionality is as easy as adding an <b>environment.txt<\/b> file to your project with the BuildAction set to <b>AndroidEnvironment<\/b>. Then you can specify which GC_BRIDGE option your application should use by passing <code>bridge-implementation=old<\/code>, <code>bridge-implementation=new<\/code>, or <code>bridge-implementation=tarjan<\/code> to the <b>MONO_GC_PARAMS<\/b> environment variable. Here is an example:<\/p>\n<pre class=\"lang:csharp decode:true\">MONO_GC_PARAMS=bridge-implementation=tarjan<\/pre>\n<p><a href=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/Screen-Shot-2014-09-05-at-10.24.55-AM.png\"><img decoding=\"async\" class=\"aligncenter size-medium wp-image-13981\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/Screen-Shot-2014-09-05-at-10.24.55-AM-300x193.png\" alt=\"Enabling Garbage collection\" width=\"300\" height=\"193\" \/><\/a><\/p>\n<p>Our awesome documentation explains in detail <a href=\"http:\/\/docs.xamarin.com\/guides\/android\/advanced_topics\/garbage_collection\/#GC_Bridge_Options\" title=\"GC Documentation\">how to enable the new bridges<\/a> so you can make your apps more responsive today.<\/p>\n<h2>Learn More<\/h2>\n<p><img decoding=\"async\" class=\"alignleft size-full wp-image-12709\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/Untitled-1-120x120.png\" alt=\"Xamarin Evolve 2014 banner with dates (October 6-10) and location (Atlanta, Georgia)\" width=\"120\" height=\"120\" \/>To learn even more about GC Bridges, be sure to join me at\u00a0<a href=\"http:\/\/evolve.xamarin.com\" title=\"Xamarin Evolve\">Xamarin Evolve 2014<\/a>, where I will be deep diving into GC and performance on both iOS and Android during multiple sessions.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mobile operating systems continue to evolve year over year, delivering great APIs for developers to integrate into their apps to create delightful\u00a0experiences. However, a side effect of this is that the size of the\u00a0mobile apps that developers are creating on Xamarin\u00a0is\u00a0growing. When we designed Xamarin.Android, our Garbage Collector integration was built\u00a0to handle small to medium [&hellip;]<\/p>\n","protected":false},"author":1926,"featured_media":39167,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4],"class_list":["post-13948","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>Mobile operating systems continue to evolve year over year, delivering great APIs for developers to integrate into their apps to create delightful\u00a0experiences. However, a side effect of this is that the size of the\u00a0mobile apps that developers are creating on Xamarin\u00a0is\u00a0growing. When we designed Xamarin.Android, our Garbage Collector integration was built\u00a0to handle small to medium [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/13948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/1926"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=13948"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/13948\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=13948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=13948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=13948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}