When reading the Improving .NET Application Performance and Scalability I have bumped into this under "Improving Managed Code Performance/Garbage Collector Guidlines:
Avoid preallocating and chunking memory.
But the book never goes into explaining as to why preallocating is bad or what the Chunking really means in light of GC optimization.
Can someobody shed more light on the issue and explain to me why the 2 are bad and what do they really mean in terms of using GC with .net?
http://www.microsoft.com/downloads/en/details.aspx?FamilyId=8A2E454D-F30E-4E72-B531-75384A0F1C47&displaylang=en
Pre-allocating memory is a technique that doesn't scale well. Chunking may push the allocation in the Large Object Heap. Either will make you use memory needlessly. Trust the garbage collector, allocate when you need it.
It does have a few things to say about what the problems are. Page 198:
C++ programmers often allocate a large block of memory (using malloc) and then use chunks at a time, to save multiple calls to malloc. This is not advisable for managed code for several reasons:
- Allocation of managed memory is a quick operation and the garbage collector has been optimized for extremely fast allocations. The main reason for preallocating memory in unmanaged code is to speed up the allocation process. This is not an issue for managed code.
- If you preallocate memory, you cause more allocations than needed; this can trigger unnecessary garbage collections.
- The garbage collector is unable to reclaim the memory that you manually recycle.
- Preallocated memory ages and costs more to recycle when it is ultimately released.
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