{"id":28725,"date":"2016-11-28T12:45:25","date_gmt":"2016-11-28T20:45:25","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=28725"},"modified":"2019-03-27T13:48:46","modified_gmt":"2019-03-27T20:48:46","slug":"say-hello-to-the-xamarin-profiler","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/say-hello-to-the-xamarin-profiler\/","title":{"rendered":"Say Hello to the Xamarin Profiler"},"content":{"rendered":"<p>As developers, we\u00a0have many tools at our disposal to help catch problems in our code (and protect our sanity); for example, IDE features\u00a0such as IntelliSense find errors as we type, a\u00a0debugger gives us a window into the app when it&#8217;s\u00a0running, and a\u00a0profiler, in turn, shows us\u00a0how our app is using resources and where it could be more effective. The Xamarin Profiler is currently the only way to profile managed (C#) code and find memory and performance issues in\u00a0Xamarin applications. It can\u00a0also be paired with native profilers, like Xcode Instruments and Android Monitor, to ensure better\u00a0app behavior and performance.<\/p>\n<p>Last week at Microsoft Connect(); we announced the first stable release\u00a0of the Xamarin Profiler, available today from the <a href=\"https:\/\/www.xamarin.com\/download\">Xamarin download page<\/a>\u00a0as part of the full Xamarin installation for MSDN Enterprise subscribers or download the standalone installer <a href=\"http:\/\/xamarin.com\/profiler\">here<\/a>. We have designed beautiful, native Profiler apps\u00a0for both Mac and Windows to help collect and interpret profiling data.<\/p>\n<p><figure id=\"attachment_28806\" aria-labelledby=\"figcaption_attachment_28806\" class=\"wp-caption aligncenter\" ><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/prof4.png\"><img decoding=\"async\" class=\"wp-image-28806\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/prof4.png\" alt=\"Profiler on Windows\" width=\"571\" height=\"343\" \/><\/a><figcaption id=\"figcaption_attachment_28806\" class=\"wp-caption-text\"><em>Profiler on Windows<\/em><\/figcaption><\/figure><\/p>\n<h2>Launching\u00a0the Profiler<\/h2>\n<p>To start a profiling session, launch an application on device or simulator with the Xamarin Profiler attached. In Visual Studio, open your Xamarin project and choose <em>Analyze &gt; Xamarin Profiler<\/em> to begin the session. In Xamarin Studio and Visual Studio for Mac, choose <em>Run &gt; Start Profiling<\/em>.<\/p>\n<p>Xamarin Profiler sessions can also be saved in the <em>mlpd<\/em> format (<em><span style=\"text-decoration: underline;\">M<\/span>ono <span style=\"text-decoration: underline;\">L<\/span>og <span style=\"text-decoration: underline;\">P<\/span>rofiler <span style=\"text-decoration: underline;\">D<\/span>ata<\/em>). Double-clicking on an <em>mlpd<\/em> file will open it in the Xamarin Profiler. These files can be moved between Windows and Mac.<\/p>\n<h2>Instruments<\/h2>\n<p>When Xamarin Profiler launches, it will ask you to select an\u00a0<em>instrument<\/em>\u00a0or group of instruments to gather data about your app.\u00a0Each instrument gathers certain\u00a0information about an application to help diagnose different\u00a0types of problems. The Xamarin Profiler comes with three instruments<em>:<\/em>\u00a0<em>Allocations<\/em>, <em>Time Profiler<\/em>, and <em>Cycles<\/em>. Let&#8217;s dive into each one to\u00a0learn how it\u00a0can help us\u00a0build better apps.<\/p>\n<h2>Allocations<\/h2>\n<p>The Allocations instrument shows us\u00a0how much memory our app is using and what it&#8217;s using it for. The allocations tab\u00a0has a list of all of the objects created during the lifetime of the app, with the details panel on the right providing additional filters, statistics, and a chart on the overall breakdown of memory usage:<\/p>\n<p><figure id=\"attachment_28752\" aria-labelledby=\"figcaption_attachment_28752\" class=\"wp-caption aligncenter\" ><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Screen-Shot-2016-11-21-at-12.24.25-PM.png\"><img decoding=\"async\" class=\"wp-image-28752\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Screen-Shot-2016-11-21-at-12.24.25-PM.png\" alt=\"Profiler on Mac\" width=\"878\" height=\"611\" \/><\/a><figcaption id=\"figcaption_attachment_28752\" class=\"wp-caption-text\"><em>Profiler on Mac<\/em><\/figcaption><\/figure><\/p>\n<p>We can further filter the list of Allocations to find objects useful to us. For example, we can search for objects from our code\u00a0by searching for our\u00a0namespace. The Xamarin Profiler marks our\u00a0objects in blue:<\/p>\n<p><figure id=\"attachment_28747\" aria-labelledby=\"figcaption_attachment_28747\" class=\"wp-caption aligncenter\" ><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Screen-Shot-2016-11-21-at-2.48.46-PM.png\"><img decoding=\"async\" class=\"wp-image-28747\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Screen-Shot-2016-11-21-at-2.48.46-PM.png\" alt=\"Profiler on Mac\" width=\"834\" height=\"570\" \/><\/a><figcaption id=\"figcaption_attachment_28747\" class=\"wp-caption-text\"><em>Profiler on Mac<\/em><\/figcaption><\/figure><\/p>\n<p>Another way to filter\u00a0Allocation data is to limit the information to a particular segment with the <em>selection tool<\/em>. The <em>selection tool<\/em> lets us highlight a section of data\u00a0to focus on. This will limit the Allocations list to\u00a0the objects allocated\u00a0during the selected time interval:<\/p>\n<p><figure id=\"attachment_28753\" aria-labelledby=\"figcaption_attachment_28753\" class=\"wp-caption aligncenter\" ><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Screen-Shot-2016-11-21-at-5.21.18-PM.png\"><img decoding=\"async\" class=\"wp-image-28753\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Screen-Shot-2016-11-21-at-5.21.18-PM.png\" alt=\"Profiler on Mac\" width=\"935\" height=\"626\" \/><\/a><figcaption id=\"figcaption_attachment_28753\" class=\"wp-caption-text\"><em>Profiler on Mac<\/em><\/figcaption><\/figure><\/p>\n<p>The Allocations instrument also features a <em>call tree<\/em>, which displays\u00a0memory usage grouped by\u00a0method call. We have the option to\u00a0view the call tree\u00a0<em>separated by thread<\/em>\u00a0as well as\u00a0<em>inverted<\/em>, which shows\u00a0memory usage\u00a0at the most granular level first and works its way up the call stack:<\/p>\n<p><figure id=\"attachment_28762\" aria-labelledby=\"figcaption_attachment_28762\" class=\"wp-caption aligncenter\" ><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Screen-Shot-2016-11-22-at-12.23.39-PM.png\"><img decoding=\"async\" class=\"wp-image-28762\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Screen-Shot-2016-11-22-at-12.23.39-PM.png\" alt=\"Profiler on Mac\" width=\"720\" height=\"250\" \/><\/a><figcaption id=\"figcaption_attachment_28762\" class=\"wp-caption-text\"><em>Profiler on Mac<\/em><\/figcaption><\/figure><\/p>\n<h3>Snapshots<\/h3>\n<p>A nice feature of the Allocations instruments is\u00a0<em>snapshots<\/em>, the ability to\u00a0list all of the objects in our\u00a0app at a point in time. We can take a snapshot\u00a0at any point during the profiling session by clicking on the camera icon in the Profiler controls on the top left. The red lines in the console mark the snapshots in the profiling session:<\/p>\n<p><figure id=\"attachment_28807\" aria-labelledby=\"figcaption_attachment_28807\" class=\"wp-caption aligncenter\" ><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/prof2-1.png\"><img decoding=\"async\" class=\"wp-image-28807\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/prof2-1.png\" alt=\"Profiler on Windows\" width=\"576\" height=\"341\" \/><\/a><figcaption id=\"figcaption_attachment_28807\" class=\"wp-caption-text\"><em>Profiler on Windows<\/em><\/figcaption><\/figure><\/p>\n<p>&nbsp;<\/p>\n<p>Double-clicking on a snapshot reveals the objects that are in memory at the time. Choosing <em>Only New\u00a0Objects<\/em> in the display options limits the objects displayed to the <em>delta<\/em> between the previous snapshot and this one. This gives us a sense of what was created between two points in time:<\/p>\n<p><figure id=\"attachment_28808\" aria-labelledby=\"figcaption_attachment_28808\" class=\"wp-caption aligncenter\" ><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/prof3.png\"><img decoding=\"async\" class=\"wp-image-28808\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/prof3.png\" alt=\"Profiler on Windows\" width=\"645\" height=\"257\" \/><\/a><figcaption id=\"figcaption_attachment_28808\" class=\"wp-caption-text\"><em>Profiler on Windows<\/em><\/figcaption><\/figure><\/p>\n<h2>Time Profiler<\/h2>\n<p>The Time Profiler uses <em>sampling<\/em> to determine where our app is spending the most time. Sampling is a lightweight performance profiling method that polls the application at regular intervals to determine what code is being executed and then forms a hypothesis for\u00a0how long different parts of our program\u00a0take to execute.<\/p>\n<p>Inside the Xamarin Profiler, the <em>call tree<\/em>\u00a0in the first tab is the same call tree as the Allocations tab, but instead of\u00a0breaking down\u00a0memory usage by method, it shows how much time was spent in each method.<\/p>\n<p>In the second tab, the\u00a0<em>sample list<\/em>\u00a0gives us a breakdown\u00a0of the sampling results:<\/p>\n<p><figure id=\"attachment_28738\" aria-labelledby=\"figcaption_attachment_28738\" class=\"wp-caption aligncenter\" ><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Screen-Shot-2016-11-21-at-2.49.56-PM.png\"><img decoding=\"async\" class=\"wp-image-28738\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Screen-Shot-2016-11-21-at-2.49.56-PM.png\" alt=\"Profiler on Mac\" width=\"759\" height=\"573\" \/><\/a><figcaption id=\"figcaption_attachment_28738\" class=\"wp-caption-text\"><em>Profiler on Mac<\/em><\/figcaption><\/figure><\/p>\n<h2>Cycles<\/h2>\n<p>The Cycles instrument gives a written and visual representation of circular references between objects, making it particularly useful for detecting <em>native reference cycles<\/em>, or circular references between objects that cross the boundary between the native and managed world. These types of cycles are important, because they prevent the objects involved from being picked\u00a0up by the garbage collector.<\/p>\n<p>The Xamarin Profiler provides a list of cycles under the <em>Roots &amp; Cycles<\/em>\u00a0tab. We are primarily concerned with cycles involving objects in our own code. If a cycle is detected, Xamarin\u00a0Profiler will list the root object in the list on the left and draw\u00a0a graph on the right of all the objects involved in the cycle. The root of the cycle is pictured in purple, with the dependencies between objects represented by arrows:<\/p>\n<p><figure id=\"attachment_28809\" aria-labelledby=\"figcaption_attachment_28809\" class=\"wp-caption aligncenter\" ><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/profiler.png\"><img decoding=\"async\" class=\"wp-image-28809\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/profiler.png\" alt=\"Profiler on Windows\" width=\"636\" height=\"353\" \/><\/a><figcaption id=\"figcaption_attachment_28809\" class=\"wp-caption-text\"><em>Profiler on Windows<\/em><\/figcaption><\/figure><\/p>\n<p>For more information on Cycles, refer to our\u00a0simple\u00a0<a href=\"https:\/\/github.com\/kallisto\/HawkinsPhoneBook\">example app for native reference cycles<\/a>, and James Clancey&#8217;s\u00a0<a href=\"https:\/\/developer.xamarin.com\/guides\/cross-platform\/deployment,_testing,_and_metrics\/memory_perf_best_practices\/\">Guide to Cross-Platform Performance with Xamarin<\/a>, which provides details on how cycles form and how to fix them.<\/p>\n<h2>Resources<\/h2>\n<ul>\n<li><a href=\"https:\/\/developer.xamarin.com\/guides\/cross-platform\/profiler\/\">Introduction to the Xamarin Profiler<\/a>: documentation from our developer portal.<\/li>\n<li><a href=\"https:\/\/developer.xamarin.com\/guides\/ios\/under_the_hood\/architecture\/\">iOS architecture<\/a>: under the\u00a0hood guide for how Xamarin.iOS works.<\/li>\n<li><a href=\"https:\/\/developer.xamarin.com\/guides\/android\/under_the_hood\/architecture\/\">Android architecture<\/a>: under the hood guide to how Xamarin.Android works.<\/li>\n<li><a href=\"https:\/\/developer.apple.com\/library\/content\/documentation\/DeveloperTools\/Conceptual\/InstrumentsUserGuide\/\">Xcode Instruments user guide<\/a>: introduction to native iOS profiling with Instruments.<\/li>\n<li><a href=\"https:\/\/developer.android.com\/studio\/profile\/android-monitor.html\">Android Monitor overview<\/a>: introduction to native Android profiling with Android Monitor.<\/li>\n<li><a href=\"https:\/\/krumelur.me\/2015\/04\/27\/xamarin-ios-the-garbage-collector-and-me\/\">Xamarin.iOS, the garbage collector, and me<\/a>: blog post with excellent explanation of iOS\u00a0native reference cycles.<\/li>\n<li><a href=\"https:\/\/bugzilla.xamarin.com\/enter_bug.cgi?product=Profiler\">Xamarin Bug Tracker<\/a>: a direct link to report issues with the Profiler.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Xamarin Profiler is currently the only way to profile managed (C#) code and find memory and performance issues in\u00a0Xamarin applications. It can\u00a0also be paired with native profilers, like Xcode Instruments and Android Monitor, to ensure better\u00a0app behavior and performance.<\/p>\n","protected":false},"author":548,"featured_media":28806,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-28725","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers"],"acf":[],"blog_post_summary":"<p>The Xamarin Profiler is currently the only way to profile managed (C#) code and find memory and performance issues in\u00a0Xamarin applications. It can\u00a0also be paired with native profilers, like Xcode Instruments and Android Monitor, to ensure better\u00a0app behavior and performance.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/28725","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\/548"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=28725"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/28725\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=28725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=28725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=28725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}