Bug 37249 - We need to go through at least 2 phases of GC.Collect() before collection an Activity
Summary: We need to go through at least 2 phases of GC.Collect() before collection an ...
Alias: None
Product: Android
Classification: Xamarin
Component: General (show other bugs)
Version: 5.1
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Jonathan Pryor
Depends on:
Reported: 2015-12-22 19:50 UTC by Alexandre Emond
Modified: 2016-01-13 16:45 UTC (History)
4 users (show)

See Also:
Is this bug a regression?: ---
Last known good build:

Xamarin and WPF Android sample (64.06 KB, application/x-zip-compressed)
2015-12-22 19:50 UTC, Alexandre Emond

Description Alexandre Emond 2015-12-22 19:50:25 UTC
Created attachment 14386 [details]
Xamarin and WPF Android sample


The Xamarin Garbage Collector is not reacting the same way as in .Net. When using activities which use a lot of memory (eg.: taking pictures), it react too late and can crash our app with OutOfMemory unless we force a serie of GC.Collect() at random interval, which cause performance degradation.

One on those difference is we need to call the GC.Collect() many times before an Activity is collected.

** Steps to reproduce with the Xamarin Sample ** 

- Click "Go To Sub" to navigate to the SubActivity (Create a new instance of an Activity)
- Go back from the sub activity (Release the reference of the Activity)
- Click "GC.Collect()"
- Click "GC.Collect()" a 2nd time
- Click "GC.Collect()" a 3rd time


- GC.Collect ()
- SubActivity JavaFinalize ()
- 1 managed SubActivity alive
- GC.Collect ()
- 0 managed SubActivity alive
- GC.Collect ()
- SubActivity Finalizer called
- SubActivity Dispose (False)
- 0 managed SubActivity alive

** Steps to reproduce with WPF to compare the GC behavior **

- Click "Create instance" (Create a new instance of TestA an release the reference right after)
- Click "GC.Collect()"
- Click "GC.Collect()" a 2nd time
- Click "GC.Collect()" a 3rd time


- GC.Collect()
- TestA Finalizer called
- TestA Dispose (False)
- 0 managed TestA alive
- GC.Collect()
- 0 managed TestA alive
- GC.Collect()
- 0 managed TestA alive

** Actual Results **

- In the Xamarin app, the Activity is still alive (not collectable) after the first GC.Collect()
	- the Activity is still using the memory (use MemoryAnalyzer.exe https://eclipse.org/mat/downloads.php to analyse captured  memory as describe on android page : http://developer.android.com/tools/debugging/debugging-memory.html)
- In the Xamarin app, the Activity finalizer is called only after the 3rd full GC.Collect()
- The second GC.Collect() seems to be queing the object in the Finalizer queue, but the thread processing the queue doesn’t seems to resume until the next GC.Collect()

** Expected Results **

- The Activity finalizer is no longer alive after the first GC.Collect(), or shortly after (Finalizer thread should not be "paused")
- The Activity finalizer is called shortly after the first GC.Collect()
- The Activity memory is collected shortly after calling the first GC.Collect() (no longer visible form the MemoryAnalyzer)

** Additional Information , Build Date & Hardware **

In the WPFSample, you’ll see the TestA class that implement the IDisposable pattern correctly. It simulate the Java.Lang.Object (which Activity inherits from) implementation, but without the handling the JNI (unmanaged). The particularity of that last one is it can ReRegisterForFinalize in case the Java handle is non-zero to ensure the cleanup.
-	protected virtual void Dispose(bool disposing) : Invoked by an Explicit Dispose() or by the Finaliser
-	public void Dispose() : Which call Dispose(bool disposing) + suppress the Finalizer call
-	~TestA() : Which call Dispose(bool disposing)

We tested the same kind of navigation with a pure Java Android project, and the SubActivity memory is freed as soon as we quit the SubActivity.

Visual Studio 2015 (14.0.24720.00 Update 1) on Windows 10
Xamarin   3.11.1594.0 (a06d6d5)
Xamarin.Android (53fce3730830417896a42f365a5ba35f1ee58d9d)
Comment 2 Abhishek 2016-01-13 16:45:56 UTC
I have tried this issue and able to reproduce this issue at my end.

I have followed the steps with the attached sample given in the bug description.

Screencast for Android sample: http://www.screencast.com/t/sLqrveAVINm
Screencast for Wcf sample:http://www.screencast.com/t/ljCo3dsZZS
Ide Log: https://gist.github.com/saurabh360/9f9be91557c9756af969

application output for android sample: https://gist.github.com/saurabh360/68b669db5481aa7bddd8

application output for wcf sample:https://gist.github.com/saurabh360/a09c113ad1cd9374464b

adb logcat: https://gist.github.com/saurabh360/b5c3647cbf2f462d3789

Environment Info:

Microsoft Visual Studio Enterprise 2015
Version 14.0.24720.00 Update 1
Microsoft .NET Framework
Version 4.6.01055

Installed Version: Enterprise

Architecture and Modeling Tools   00322-80000-00000-AA906
Microsoft Architecture and Modeling Tools
UML® and Unified Modeling Language™ are trademarks or registered trademarks of the Object Management Group, Inc. in the United States and other countries.

Visual Basic 2015   00322-80000-00000-AA906
Microsoft Visual Basic 2015

Visual C# 2015   00322-80000-00000-AA906
Microsoft Visual C# 2015

Visual C++ 2015   00322-80000-00000-AA906
Microsoft Visual C++ 2015

Application Insights Tools for Visual Studio Package   1.0
Application Insights Tools for Visual Studio

ASP.NET and Web Tools 2015 (RC1 Update 1)   14.1.11120.0
ASP.NET and Web Tools 2015 (RC1 Update 1)

ASP.NET Web Frameworks and Tools 2012.2   4.1.41102.0
For additional information, visit http://go.microsoft.com/fwlink/?LinkID=309563

ASP.NET Web Frameworks and Tools 2013   5.2.30624.0
For additional information, visit http://www.asp.net/

Azure App Service Tools v2.7.1   14.0.11112.0
Azure App Service Tools v2.7.1

Common Azure Tools   1.5
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Microsoft Azure Mobile Services Tools   1.4
Microsoft Azure Mobile Services Tools

NuGet Package Manager   3.3.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

PreEmptive Analytics Visualizer   1.2
Microsoft Visual Studio extension to visualize aggregated summaries from the PreEmptive Analytics product.

SQL Server Data Tools   14.0.50616.0
Microsoft SQL Server Data Tools

TypeScript for Microsoft Visual Studio

Xamarin (21aad62)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android (50280e4)
Visual Studio plugin to enable development for Xamarin.Android.

Xamarin.iOS (122b860)
Visual Studio extension to enable development for Xamarin.iOS.

Note You need to log in before you can comment on or make changes to this bug.