Sunday, January 22, 2012

Garbage Collection and Large Object Heap.

I have posted a query on SO for better understanding of LOH, that was mainly driven by a bad answer in an interview. Following is the better answer.

Garbage collector, other than providing support for removing unused objects from heap, also support memory compation i.e. removing fragments from memory resulted in removing the objects that were not being used. Memory compaction is helpful because objects are stored in memory in contiguous memory locations, and if there are multiple fragment (note: fragment means unutilized spaces in memory) than a object may not get enough space and trigger GC for that generation, which may not be required.

The other facility, or advantage, that Large Object Heap provides is no memory compaction, that is no movement of used objects once some objects have been removed. This is done as a performance measure considering that moving too many objects from one memory location to another would consume CPU and the benefit may not be enough to compensate for that. As this article illustrates. Compacting large objects costs a lot. Similar with the examples in the article, say moving a byte in memory needs 2 cycles, then compacting a 8MB object in a 2GHz computer needs 8ms, which is a large cost.

Now, against the argument: why do we need LOH? It is only for the benefit of separating it from Generation 0 (an object gets memory in Gen0 or LOH), since Gen0 support memory compaction, which is useful for small object and not for large ones. Thus, a separate memory which does not support memory compaction, pins the large objects in heap.

No comments:

Post a Comment