Bug 36000

Summary: Dictionary with IEqualityComparer is leaking in iOS 9.2.1.51 / Mono 4.2.1
Product: iOS Reporter: manuel
Component: Mono runtime / AOT compilerAssignee: Zoltan Varga <vargaz>
Status: VERIFIED FIXED    
Severity: normal CC: arpitj, ashley.gazich, brendan.zagaeski, chrisntr, dominic, kumpera, mono-bugs+monotouch
Priority: ---    
Version: XI 9.2   
Target Milestone: Untriaged   
Hardware: PC   
OS: Mac OS   
Tags: BZCU Is this bug a regression?: ---
Last known good build:
Attachments: testcase

Description manuel 2015-11-18 09:05:38 UTC
A Dictionary with an IEqualityComparer seems to be leaking after updating to iOS 9.2.1.51 at least in 64 bit devices

	Thread t = new Thread (new ThreadStart (delegate {
		var dataset = new double[1000][];

		for(int rec = 0; rec < dataset.Length; rec++){
			dataset[rec] = new double[2];

			double x = rec;
			var y = Math.Sin(x / 100.0 * Math.PI / 2.0);
			dataset[rec][0] = x;
			dataset[rec][1] = y;
		}

		while(true){
			var seeds = createSeeds(dataset);

		}
	}));
				
	t.Start ();



		static double[][] createSeeds(double[][] points)
		{
			int minBin = 1;

			// Create bins as suggested by (Conrad Lee, 2011):
			//
			// The dictionary holds the positions of the bins as keys and the
			// number of occurrences of a given point as the value associated 
			// with this key. The comparer tells the dictionary how to compare
			// integer vectors on an element-by-element basis.

			var bins = new Dictionary<int[], int>(new ArrayComparer<int>());
			var dimension = 2;

			// for each point
			foreach (var point in points)
			{
				// create a indexing key
				int[] key = new int[dimension];
				for (int j = 0; j < point.Length; j++)
					key[j] = (int)(point[j] / 1);

				// increase the counter in the key
				int previous;
				if (bins.TryGetValue(key, out previous))
					bins[key] = previous + 1;
				else bins[key] = 1;
			}

			// now, read the dictionary and create seeds
			// for bins which contain more than one point

			var seeds = new List<double[]>();

			// for each bin-count pair
			foreach (var pair in bins)
			{
				if (pair.Value >= minBin)
				{
					// recreate the point
					int[] bin = pair.Key;

					double[] point = new double[dimension];
					for (int i = 0; i < point.Length; i++)
						point[i] = bin[i] * 1;

					seeds.Add(point);
				}
			}

			return seeds.ToArray();
		}



	public class ArrayComparer<T> : IEqualityComparer<T[]>
		where T : IEquatable<T>
	{
		/// <summary>
		///   Determines whether two instances are equal.
		/// </summary>
		/// 
		/// <param name="x">The first object to compare.</param>
		/// <param name="y">The second object to compare.</param>
		/// <returns>
		///   <c>true</c> if the specified object is equal to the other; otherwise, <c>false</c>.
		/// </returns>
		///   
		public bool Equals(T[] x, T[] y)
		{
			for (int i = 0; i < x.Length; i++)
				if (!x[i].Equals(y[i]))
					return false;
			return true;
		}

		/// <summary>
		///   Returns a hash code for a given instance.
		/// </summary>
		/// 
		/// <param name="obj">The instance.</param>
		/// 
		/// <returns>
		///   A hash code for the instance, suitable for use 
		///   in hashing algorithms and data structures like a hash table. 
		/// </returns>
		/// 
		public int GetHashCode(T[] obj)
		{
			unchecked
			{
				int hash = 17;
				for (int i = 0; i < obj.Length; i++)
					hash = hash * 23 + obj[i].GetHashCode();
				return hash;
			}
		}

	}




Xamarin Studio
Version 5.10 (build 871)
Installation UUID: 5f421f3f-40c4-4caa-8ae3-df7a8094edf4
Runtime:
	Mono 4.2.1 (explicit/6dd2d0d)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402010102

Xamarin.Profiler
Version: 0.0.0.0
Location: /Users/nativolabs/Documents/profiler-mac-0.9-0/XamarinProfiler.Mac.app/Contents/MacOS/XamarinProfiler.Mac

Xamarin.Android
Version: 6.0.0.34 (Business Edition)
Android SDK: /Users/nativolabs/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
		5.1    (API level 22)
		6.0    (API level 23)

SDK Tools Version: 24.3.4
SDK Platform Tools Version: 23
SDK Build Tools Version: 21.1.2

Java SDK: /usr
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

Xamarin Android Player
Version: 0.2.5
Location: /Applications/Xamarin Android Player.app

Apple Developer Tools
Xcode 7.1.1 (9081)
Build 7B1005

Xamarin.iOS
Version: 9.2.1.51 (Business Edition)
Hash: 3c0ec35
Branch: master
Build date: 2015-11-12 13:05:39-0500

Xamarin.Mac
Not Installed

Build Information
Release ID: 510000871
Git revision: 4e9c5abb5ffdae12ba02ac49da83f8b2011dbb88
Build date: 2015-11-12 06:02:54-05
Xamarin addins: 55007ed0e56436f385d8e26394a45be563abc7e8
Build lane: monodevelop-lion-cycle6

Operating System
Mac OS X 10.11.1
Darwin Nativos-MacBook-Pro.local 15.0.0 Darwin Kernel Version 15.0.0
    Sat Sep 19 15:53:46 PDT 2015
    root:xnu-3247.10.11~1/RELEASE_X86_64 x86_64
Comment 1 Rodrigo Kumpera 2015-11-18 19:31:42 UTC
Hi Manuel,

When you say it's leaking, what do you mean by that? I tried locally and could not see managed heap or total memory growing when running your code.
Comment 2 Zoltan Varga 2015-11-19 00:58:01 UTC
I can reproduce this, its a leak in the generic sharing code.
Comment 3 Zoltan Varga 2015-11-19 02:01:49 UTC
Should be fixed by mono-4.3.1-branch 2c48c4df45289a6e63c1fb758d28c9b73095afaa. We should probably consider this for cycle6sr1.
Comment 4 manuel 2015-11-19 05:38:50 UTC
Hi guys

thanks for super fast correction! 2 questions, though.
1 - Although, the issue is with mono, this not seems to be happening in Android. Is this only happening in iOS due to AOT?  

2 - When cycle6sr1 will be available? 

cheers
Comment 5 Zoltan Varga 2015-11-19 06:01:18 UTC
Yes, it only affects ios.
Comment 6 Rodrigo Kumpera 2015-11-19 09:57:39 UTC
Hey Zoltan,

Agreed, this should go into SR1.
Could you make a PR for this one against 4.2 and ping Alexis?
Comment 8 Zoltan Varga 2015-11-20 11:55:53 UTC
Created attachment 13918 [details]
testcase
Comment 9 Arpit Jha 2015-11-23 08:58:29 UTC
*************************
Reproduce Status
*************************

I have checked this issue with the help of attached project in comment 8 and monotouch-9.2.1.51_3c0ec3558300faa000a42e3715ad544bcdffba8f and observe that getting memory warning in app output and app getting crashed.
App output:   https://gist.github.com/Arpit360/e534776706eab0eaae71 
                  

************************
Verify Status
************************

I have checked this issue with attached XI monotouch-9.2.0.94 and its working fine.
Observation:Not getting any memory leak warning and also app is not getting crashed with fixed build.
Screencast:  http://www.screencast.com/t/DZQbC6hRm6
App output: https://gist.github.com/Arpit360/136a75d0c1fb81478554



Environment Info:
=== Xamarin Studio ===

Version 5.10 (build 871)
Installation UUID: 3d25a767-a003-4a7d-9f5e-e57987cf6cf0
Runtime:
	Mono 4.2.1 (explicit/6dd2d0d)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402010102

=== Xamarin.Profiler ===

Version: 0.23.35.0
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Apple Developer Tools ===

Xcode 7.1 (9079)
Build 7B91b

=== Xamarin.iOS ===

Version: 9.2.0.94 (Trial Edition)
Hash: b8f38d0
Branch: master
Build date: 2015-11-21 03:06:35-0500

=== Xamarin.Android ===

Version: 6.0.0.34 (Trial Edition)
Android SDK: /Users/mac360_xamarin/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.3    (API level 10)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
		5.1    (API level 22)
		6.0    (API level 23)

SDK Tools Version: 24.4
SDK Platform Tools Version: 23.0.1
SDK Build Tools Version: 23

Java SDK: /usr
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

=== Xamarin Android Player ===

Version: 0.6.1
Location: /Applications/Xamarin Android Player.app

=== Xamarin.Mac ===

Version: 2.4.0.109 (Starter Edition)

=== Build Information ===

Release ID: 510000871
Git revision: 4e9c5abb5ffdae12ba02ac49da83f8b2011dbb88
Build date: 2015-11-12 06:02:54-05
Xamarin addins: 55007ed0e56436f385d8e26394a45be563abc7e8
Build lane: monodevelop-lion-cycle6

=== Operating System ===

Mac OS X 10.10.5
Darwin mac360-xamarins-Mac-mini.local 14.5.0 Darwin Kernel Version 14.5.0
    Wed Jul 29 02:26:53 PDT 2015
    root:xnu-2782.40.9~1/RELEASE_X86_64 x86_64