Bug 13706 - NSDictionay memory leak
Summary: NSDictionay memory leak
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Library (Xamarin.Mac.dll) ()
Version: 1.4.x
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Aaron Bockover [MSFT]
Depends on:
Reported: 2013-08-02 06:29 UTC by Adrian Gallero
Modified: 2014-12-03 15:22 UTC (History)
2 users (show)

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

Test case (18.10 KB, application/zip)
2013-08-02 06:31 UTC, Adrian Gallero

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 Adrian Gallero 2013-08-02 06:29:44 UTC
Steps to reproduce:

In a new Mono/Xam mac application, drop a button and write the following event handler:

        partial void RunTest(NSObject sender)
            for (int i = 0; i < 10000; i++)
                using (NSData obj = NSData.FromArray(new byte[100000]))
                    using (NSData key = NSData.FromArray(new byte[10000]))
                        using (NSDictionary attr = NSDictionary.FromObjectAndKey(obj, key))

            GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);

Or use the attached project.

Every time you press the button, the application leaks about 1GB of memory. If opened with Apple Instruments, the NSDictionary instances aren't being freed.

This seems to happen only in Mono/Xam mac, not in MonoTouch.
Comment 1 Adrian Gallero 2013-08-02 06:31:21 UTC
Created attachment 4522 [details]
Test case
Comment 2 Miguel de Icaza [MSFT] 2014-12-03 15:22:08 UTC
The GC.Collect there could contains some false positives from the obj and key still being around.

In my case, it takes a couple of seconds, but Activity Monitor shows the memory going back to a baseline (baseline, because the GC can not always return all the memory to the OS, only memory that is completely empty, and the above would likely create some fragmentation at some point).