Bug 20922 - Array.Sort() GeneratesGarbage
Summary: Array.Sort() GeneratesGarbage
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: master
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2014-06-26 17:38 UTC by Ray
Modified: 2014-06-27 06:45 UTC (History)
2 users (show)

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

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 GitHub or Developer Community 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:
Status:
RESOLVED FIXED

Description Ray 2014-06-26 17:38:28 UTC
When working on a game using Mono, I've discovered that Array.Sort (And pretty any container class that would call this such as List<T>) generates garbage when called. The qsort call from under the hood allocates a QSortStack array with every call, which can generate a decent amount of garbage. The allocation itself is only ~288 bytes, but a high performing application like a game which can sort elements ~60 times a frame could easily end up generating about a MB a second.

https://github.com/mono/mono/blob/6169424b4c497e6b650806c83653f1c0dc98a13a/mcs/class/corlib/System/Array.cs#L1485
Comment 1 Ray 2014-06-26 17:40:33 UTC
It's also worth mentioning that, while it's not garbage itself since it's a delegate, you could also greatly reduce the amount of allocations that the chain of Array.Sort() does by internally passing the Comparison<T>'s used by ref.
Comment 2 Marek Safar 2014-06-27 06:45:34 UTC
Fixed in master. Although I don't know what you meant by the Comparison<T> comment