Save that I have a (large) collection of instances of Dictionary. The key value in that dictionary is always one of say 10 known strings.
If the collection contains 1000000 entries, will the that string key value occupy memory for each instance and key? Is there any good way to optimize a case like that, perhaps using string interning?
Another way would be to use say a short for the key instead and translate between the string and the short but the syntax get's a bit messy...
Usually not - they are stored as a single immutable variable. Strings can be interned which will help saving memory.
But this depends. If you construct the string every time (e.g. concatenation) they will not be interned. Defining them as constants ensures they will be interned.
You can check if two strings are the same in memory using object.ReferenceEquals().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With