Cache your common string
Think of the following scenario:
Your application consist out of many grid result pages that come from different SQL statements and your application is extensible (everything is configuration base),
The XML that represent a grid results is:
It consist out of 3 classes:
You might already see that most of the string properties of a column are widely used and will be the same for all the users.
When you deserialize the value into memory you actually create numerous new strings although most of them are the same - as a result you waste memory.
There are couple of thing you can do to fix this bug - One of them is to create a typed column and thus use the common properties as a part of the class but that would mean you have to make lots of changes and sometimes your application requires that each user defines his own properties.
Another option that you can use (which I think is adhoc with the most ROI) is to override the setter of the string properties to get the string out of a new StringCache class (simple string dictionary as a singleton).
Using this method you make sure that a string will only be in memory once because when deserializing process occur every property is being set of the string value that is actually being fetched from the StringCache object as a result you use the same string reference.
I have created a sample XML file, when deserializing it without using StringCache object the held memory was approx 20K
But when taking advantage of the StringCache the held memory was reduced to approx 5k
That's 75% reduce of memory
All because you have similar string in your object modal (That happens a lot)
The StringCache class as a Bypass property only to enable you to see the difference in a running application.
This is not the final solution and you might also want to change the StringCache to limit the number of Items / use WeakReference.
If you have any suggestion / fixes please comment me on that.
The profiling was done using JetBrains profiler.