Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report on
Developer Community or GitHub with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
I'm experiencing a massive memory leak when await'ing Tasks in a PCL that uses Microsoft.Bcl.Async. It seems as though the async state machine / support code generated by the compiler keeps holding on to finished tasks and they never get garbage collected. When I use the same code in a Xamarin.iOS App exclusively (i.e. use code compiled against the Xamarin.iOS async framework libraries) the memory leak does not occur.
I have attached a project that reproduces this consistently, as well as a screenshot of a HeapShot demonstrating the Memory Leak. The sample uses a simple Trigger/Timer implemented using await Task.Delay that will cause this memory leak. In AsyncMemoryLeakViewController.ViewDidLoad you can switch between the PCL/iOS version of the Trigger class.
// When we use TriggerDefinedInApp there's no memory leak
// TriggerDefinedInPcl leaks _a lot_
// app = new TriggerDefinedInApp( d =>
pcl = new TriggerDefinedInPcl( d =>
I'm at loss to pinpoint what is at fault here - it may be Microsoft.Bcl.Async (I'll try reproducing on Windows), the Async state machine code generated by MCS or something entirely different. However, this has caused serious headaches for us and I consider this a major problem, wherever this is rooted.
Created attachment 6492 [details]
Sample Project reproducing the Memory Leak
Created attachment 6493 [details]
Fyi: I could not reproduce the bug using a WindowsFormsApplication which in turn used TriggerDefinedInPcl from the sample project, so this seems to be related to Xamarin exclusively.
I could strip down the sample even further, so basically it appears every await inside a PCL leaks the TaskAwaiter, which holds on to the task (via an Action) and eventually the whole state machine.
I have verified that the statemachines generated for the PCL and the App assembly are identical, so it seems we can rule out MCS here.
Interestingly enough, the state machine generated by the Microsoft C# compiler for the PCL looks a little different. One major difference in the state machine there is that it seems to zero out the TaskAwaiter held by the state machine when it is done with it.
So I went ahead and referenced the PCL compiled with CSC against the project (so now using the State machine that zeros out the TaskAwaiter, but got the same memory leaks. I think that means we can rule out the differences in the sate machine for causing this.
My gut feeling is that this is either something going wrong with the AOT compiler (I will check on Android to be sure), assembly unification (if that applies here) or the garbage collector (which would be pretty... bad).
Created attachment 6502 [details]
Further simplified repro.
I have checked this issue with the Sample Project attached into comment 1 and observed that we are getting memory leak in both the cases either we use "pcl = new TriggerDefinedInPcl( d => " in ViewDidLoad() method or we use "app = new TriggerDefinedInApp( d => ". To check this issue I have deployed application on iOS device and then check memory leak with "Instruments" by launching instruments.
Screencast : http://screencast.com/t/HqDr3zSF
Environment Info :
=== Xamarin Studio ===
Version 4.2.3 (build 60)
Installation UUID: 011d70a5-dede-428b-ab04-ef451c2e539d
Mono 3.2.6 ((no/9b58377)
GTK+ 2.24.23 theme: Raleigh
Package version: 302060000
=== Apple Developer Tools ===
Xcode 5.1 (5084)
=== Xamarin.iOS ===
Version: 126.96.36.199 (Trial Edition)
Build date: 2014-10-03 18:02:26-0400
=== Xamarin.Mac ===
Xamarin.Mac: Not Installed
=== Xamarin.Android ===
Version: 4.12.1 (Starter Edition)
Android SDK: /Users/MM/Desktop/android-sdk-macosx
Supported Android versions:
2.1 (API level 7)
2.2 (API level 8)
2.3 (API level 10)
3.1 (API level 12)
3.2 (API level 13)
4.0 (API level 14)
4.0.3 (API level 15)
4.1 (API level 16)
4.2 (API level 17)
4.3 (API level 18)
4.4 (API level 19)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)
=== Build Information ===
Release ID: 402030060
Git revision: 30c4afc300c2a39ec5300851357ce02e49dd217e
Build date: 2014-03-05 22:09:33+0000
Xamarin addins: f8a9589b57c2bfab2ccd73c880e7ad81e3ecf044
=== Operating System ===
Mac OS X 10.9.2
Darwin MacMini.local 13.1.0 Darwin Kernel Version 13.1.0
Thu Jan 16 19:40:37 PST 2014
I can reproduce the issue with the sample from comment #5.
From what I can see the only thing that differs is that the PCL version is calling TaskEx.Delay (from Microsoft.Threading.Tasks.dll), instead of Task.Delay, so presumably there's something different with that assembly.
I could consistently reproduce with the simplified sample from comment #5. The reproduction in comment number 6 is incorrect - I am not referring to leaks on the unmanaged heap (as reported by Instruments) but am referring to leaks in the managed heap as made evident by using the Mono Profiler (in X Studio Project - Profile - Mono).
*** This bug has been marked as a duplicate of bug 18398 ***
Not sure I agree on this - how does this explain that I get leaks with code from a PCL only and not with code compiled e.g. into an iOS assembly? Is it linking to two different implementations of Task? I thought the implementation the PCL links against is the Microsoft library, which should not have this bug. Even though, I get leaks when compiling my await code in a PCL only.
Also, can you indicate when the patch will be in xamarin stable?
You don't have to agree.
PCL async assembly still uses BCL task implementation. The fix won't make it to 7.2.1 release and will probably appear in 7.4 series earliest.
You can check Mono commits for the change
Marek, thanks for the explanation. I still don't get why the code compiled into the iOS assembly does not leak when the same code compiled into a PCL assembly does leak. I realize that the PCL is implemented using TypeForwardedTo mscorlib, so both compiled assemblies should eventually rely on the same Task implementation.
Obviously this isn't the case, since using await on a Task from a PCL does leak the Task whereas using a wait on a Task inside an iOS assembly does not leak the task. Hence my question for clarification.
I'm just questioning whether the fix referenced in bug 18398 would really resolve this issue. 7.4 probably still has a long way to go until it hits stable and I'd hate to find out the issue is not fixed once it rolls out. Following Xamarins promise of supporting async and PCLs we've started to rely heavily on that combination only to find out that it fails in production.
I'd love to run the sample with mono trunk myself, but I do currently not see how I'd be able to do so until the Brian's commit makes its way into alpha at least.
What causes the leak is ConfigureAwait (false), when you remove it or change it to ConfigureAwait (true) the leaks does not occur. ConfigureAwait is most likely implemented differently in your nuget vesion of async than in mono BCL.
I ran the test locally with the change and it does not leak for me. This has nothing to do with PCL it's just that different Mono Task code paths are executed.