Bug 20922

Summary: Array.Sort() GeneratesGarbage
Product: [Mono] Class Libraries Reporter: Ray <Ray>
Component: mscorlibAssignee: Marek Safar <masafa>
Severity: normal CC: masafa, mono-bugs+mono
Priority: ---    
Version: master   
Target Milestone: Untriaged   
Hardware: PC   
OS: Windows   
Tags: Is this bug a regression?: ---
Last known good build:

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.

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