Tuesday, July 29, 2008

The Open Closed Principle

Patterns in Practice: The Open Closed Principle by Jeremy Miller

Build Scalable Systems That Handle Failure Without Losing Data

Build Scalable Systems That Handle Failure Without Losing Data by Udi Dahan

Labels: , ,

Friday, July 04, 2008

OutOfMemoryException and Pinning

OutOfMemoryException and Pinning

Some fundamentals of memory

Some fundamentals of memory

Thursday, July 03, 2008

Large Object Heap

For LOH, though, because compaction is expensive the CLR team chose to sweep them, making a free list out of dead objects that can be reused later to satisfy large object allocation requests. Adjacent dead objects are made into one free object.

If I don't have enough free space to accommodate the large object allocation requests, I will first attempt to acquire more segments from the OS. If that fails, then I will trigger a generation 2 garbage collection in hope of freeing up some space.

During a generation 2 garbage collection, I take the opportunity to release segments that have no live objects on them back to the OS. Memory from the last live object to the end of the segment is decommitted. And the free spaces remain committed though they are reset, meaning the OS doesn't need to write data in them back to disk.

A garbage collection occurs if one of the following conditions happens:
  • Allocation Exceeds the Generation 0 or Large Object Threshold (this is the most typical case).
  • System.GC.Collect Is Called
  • System Is in Low Memory Situation This happens when I receive the high memory notification from the OS.

LOH Performance Implications

Allocation costs.The CLR makes the guarantee that the memory for every new object I give out is cleared. This means the allocation cost of a large object is completely dominated by memory clearing (unless it triggers a garbage collection). If it takes two cycles to clear 1 byte, it means it takes 170,000 cycles to clear the smallest large object. For a 16MB object on a 2GHz machine, it will take approximately 16ms to clear the memory.

Collection costs. LOH and generation 2 are collected together. If either one's threshold is exceeded, a generation 2 collection will be triggered. If many large objects are allocated on a very temporary basis and you have a big SOH, you could be spending too much time running garbage collections; not to mention that the allocation cost can really add up if you keep allocating and letting really large objects go.

From Large Object Heap Uncovered by Maoni Stephens.