We have an enormous program that is generating too much garbage. This is hurting performance, generating thousands of soft page faults every second, reducing latency, hurting throughput, etc.
One of the prime culprits is System.Collections.Generic.Dictionary, so we would like to replace it with a version that allows us to pre-allocate say 10,000 entries, then reuse these entries on the fly.
Are there any existing implemenations of Dictionary that allow pre-allocation of entries, and do not generate garbage at runtime?
We decided not to use the garbage free dictionary in the end, as it didn’t impact overall GC that much.
For the record, here is an implementation of a garbage free dictionary. Its memory hungry, but it works well.
To use, instantiate a new dictionary, with enough slots for the potential integer keys. For example, if the primary integer key could range between 0 and 50,000, then you will need to pass 50,000 in when you instantiate it:
If the key could range from 200,000 to 250,000, instantiate it with 50,000 potential keys, and it will automatically “rebase” the key (based on the first key it sees) so it can range from 200,000 to 250,000.