I need to improve memory performance on my application and I could see that I have problems with memory fragmentation.
I’ve read an interesting article on large objects from Andrew Hunter of Red Gate, and one of the solutions he recommends is:
If large data structures need to live
for a long time, and especially if
they need to grow in size over time,
the best approach is simply to
consider using or writing a different
data structure to store them. Arrays
can contain up to around 10,000
elements before they are put on the
large object heap and can cause
problems, so a very effective way to
store 100,000 entries might be to
store 10 arrays each containing 10,000
elements: none will end up on the
large object heap so no fragmentation
will occur. This could be written as
anIListsubclass, which would make it
easy to drop in transparently to
replace existing code.
How do I implement his suggestion in my code?
My program has a very complex form (with an object that leaves residual memory every time it opens. I found a complex list that may be the culprit, and I’d like to implement his suggestion to see if it fixes the issue.
What’s wrong with using List for that? That’s nothing but an implementation of IList and you can do the partitioning yourself. But if you want to do it transparently:
Implement IList (it’s just an interface, nothing special about it. Maybe I don’t understand the question?) and back it up by arrays of your desired size. Your
Get()would then take theindex / sizeOfArraysas index of the array containing the desired item and return theindex % sizeOfArraysth item in that array.For fun, because it’s a lazy friday, I wrote something up. Note:
Itemimplementation, especially the setter, for exampleThat said, here’s a starting point that reduced my pre-weekend motivational deficit. I left some interesting methods as exercise to the dear reader (or OP).. 😉