Bug 54443 - Memory leak occurs only on ios 10.x devices, but fine on ios 9.3
Summary: Memory leak occurs only on ios 10.x devices, but fine on ios 9.3
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: XI 10.5.99 (Xcode 8.3 previews)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2017-04-04 13:31 UTC by Ilnur
Modified: 2017-07-27 16:44 UTC (History)
3 users (show)

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

visual studio for mac system info (1.84 KB, text/plain)
2017-04-04 13:34 UTC, Ilnur

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and 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 Links.

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.

Related Links:

Description Ilnur 2017-04-04 13:31:14 UTC
In our legacy project we are struggling with a strange problem: there is a memory leak which occurs on iOS 10.x (at first memory warning signal received then app crashes), but everything is fine on iOS 9.3. Both simulators and devices show same behaviour.
I can't debug it with XCode Instruments due to known deadlock problem: https://forums.xamarin.com/discussion/21656/
But I was able to see that there is really a problem by use of Xamarin Profiler.

Please see sample project here: https://github.com/lexboss93/iosleaktest/
Latest commit when I post this bug. Commit sha 8985627214e6ca259c4ffb835211eaf9550e3805

Detailed screenshots of Xamarin Profiler with descriptions are at xamarin forum:

or on stackoverflow:

What project does it takes an input pdf file and creates a thumbnail for each page by using of these method:
CGSize SaveThumbnailGetSize (PDFDocument pdfDocument, int pdfPage, string filename)
        CGSize size = pdfDocument.GetPageSize (pdfPage);
        nfloat K = 750f / size.Width < 1 ? 750f / size.Width : 1;

        MuPDFLib.Cookie cookie = new MuPDFLib.Cookie ();

        using (NativeBitmap img = pdfDocument.RenderToBitmap (PDFDocument.context, new CGRect (0, 0, (int)Math.Round (size.Width * K), (int)Math.Round (size.Height * K)), K, pdfPage, 0, ref cookie)) {
            NSError err = null;
            using (var uimg = ((UIImage)img)) {

                // Memory leak occurs only with this line. If comment this, memory leak will no occur.
                using (NSData data = uimg.AsJPEG (0.4f)) { 

                    //[optional line]
                    //data.Save (filename, true, out err);

            if (err != null) {
                Console.WriteLine ("Could not save thumbnail: {0}", err);

        return size;

I think something wrong happens in this line:
NSData data = uimg.AsJPEG (0.4f)

Because if I remove this everything is fine.

=== Xamarin Studio Community ===

Version 6.2.1 (build 3)
Installation UUID: 183e65ea-49c6-4e59-906c-fb09d12c4d10
	Mono 4.8.0 (mono-4.8.0-branch/8f6d0f6) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 408000520

=== NuGet ===


=== Xamarin.Profiler ===

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

=== Apple Developer Tools ===

Xcode 8.3 (12169)
Build 8E162

=== Xamarin.Android ===

Version: (Xamarin Studio Community)
Android SDK: /Users/user/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)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)

SDK Tools Version: 25.2.2
SDK Platform Tools Version: 23.1
SDK Build Tools Version: 23.0.3

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)

Android Designer EPL code available here:

=== Xamarin Android Player ===

Not Installed

=== Xamarin.iOS ===

Version: (Xamarin Studio Community)
Hash: e66c6f19
Branch: xcode8.3
Build date: 2017-03-28 00:48:33-0400

=== Xamarin.Mac ===

Version: (Xamarin Studio Community)

=== Xamarin Inspector ===

Version: 1.2.0
Hash: 62c17e6
Branch: d15-1
Build date: Mon, 20 Mar 2017 02:36:23 GMT

=== Build Information ===

Release ID: 602010003
Git revision: 5217903c99e9d9c5d3caa2498fd66d607debac29
Build date: 2017-03-23 12:36:46-04
Xamarin addins: 2c96d252b353fce2e8b8fd20884eee70c16c7f32
Build lane: monodevelop-lion-cycle9

=== Operating System ===

Mac OS X 10.12.3
Darwin ilnur-imac.shire.local 16.4.0 Darwin Kernel Version 16.4.0
    Thu Dec 22 22:53:21 PST 2016
    root:xnu-3789.41.3~3/RELEASE_X86_64 x86_64
Comment 1 Ilnur 2017-04-04 13:34:13 UTC
Created attachment 21184 [details]
visual studio for mac system info

Same result on visual studio for mac
Comment 2 Ilnur 2017-04-04 13:44:13 UTC
Steps to reproduce a bug.

1. Clone/download project
2. Run on simulator Ipad Air with ios 10.x (not tested with other devices).
3. Click "click me" button.
4. After task completes (look at console) open Activity monitor or Profiler
and see that allocated memory not freed (not garbage collected).

And for comparison do the same for Ipad Air with ios 9.3 and see that allocated memory is garbage collected (freed).
Comment 3 Rolf Bjarne Kvinge [MSFT] 2017-04-04 14:17:35 UTC
(In reply to lex89600466919 from comment #0)
I'm just guessing here, but can you try adding an NSAutoreleasePool around your code and see if it fixes the problem:

using (var pool = new NSAutoreleasePool ()) {
    using (NSData data = uimg.AsJPEG (0.4f)) {
Comment 4 Ilnur 2017-04-04 14:38:52 UTC
I've tried to do so with no success.
And also I looked at the 

And saw that xamarin do this for me:
public NSData AsJPEG (nfloat compressionQuality)
			using (var pool = new NSAutoreleasePool ())
				return (NSData) Runtime.GetNSObject (UIImageJPEGRepresentation (Handle, compressionQuality));
Comment 5 Ilnur 2017-04-04 14:53:32 UTC
Did some testing.

Memory leak occurs:
real device iPad Air iOS 10.2.1 (14D27)

simulator iPad Air iOS 10.0 (14A345)
simulator 14C89
simulator iPad Air 2 - iOS 10.0 (14A345)
simulator iPhone 6 - iOS 10.0 (14A345)

Memory leak not occurs:
simulator iPad Air iOS 10.3 (14E269)
simulator iPad Air 2 iOS 10.3 (14E269)

simulator iPad Air iOS 9.3 (13E233)
simulator iPhone 6 - iOS 10.3 (14E269)
Comment 6 Ilnur 2017-04-05 13:34:43 UTC
Reopening. Added info above.
Comment 7 John Miller [MSFT] 2017-07-27 16:44:44 UTC
I attempted to reproduce this issue based on the bug description with the latest Xamarin.iOS on iOS 10.3 and 10.2, and I was unable to hit the problem. If this issue is still occurring for you, please reopen this report and attach a project that reproduces the issue, ideally starting with a new template project and then adding just the code necessary to demonstrate the issue.