Reputation: 10015
I read some posts and books about .Net/C#/CLR and so on, and found following slide in Microsoft's presentation of 2005 year:
- GC takes time – “% time in GC” counter
- If objects die in gen0 (survival rate is 0) it’s the ideal situation
- The longer the object lives before being dead, the worse (with exceptions)
- Gen0 and gen1 GCs should both be relatively cheap; gen2 GCs could cost a lot
- LOH – different cost model
- Temporary large objects could be bad
- Should reuse if possible
My question is what does it mean Should reuse if possible
? Is it that CLR reuse allocated memory for new object in LOH or that user (developer in our case) should do it ?
Upvotes: 2
Views: 216
Reputation: 156978
I think that is a note to us, as implementers, not a note on how Microsoft works (so no, it does not automatically reuse objects). If you have a object on the LOH, and you immediate dispose it, the LOH can get fragmented very soon. That is why it says "Temporary large objects could be bad".
The other thing is in the same line: if you have a large object and you can reuse it, you prevent recreating that object and thus improve performance. This is true because you prevent the LOH to get fragmented faster and you lower the memory pressure. One specific thing that comes in mind here are large string objects. Those are ideal to reuse. The string intern pool is located on the LOH, so if you intern frequently used large strings, you do what it asks.
Upvotes: 2
Reputation: 138
I agreed with Patrick's statement; CLR doesn't reuses object in LOH. These guidelines are for our implementation purposes.
Gen2 garbage collection process is very costly, so we need to avoid this. So we can do this by reusing the objects, because fragmentation process also performed and it will take more time in case of LOH. We can reuse these objects by using object pool.
Upvotes: 1