JS Ext

Wednesday, November 28, 2012

Mark and Sweep Optimization: Generations

One of the performance penalties of the mark-and-sweep algorithm is that it searches all active objects in your heap.  This means the larger your heap is, the longer the GC cycle takes.  One way to increase performance is to split your heap into generations.

Let's split the heap into three generations: young, middle and old.  All new objects go into the young generation.  When you fill up the young generation, you GC that generation only.  If an object survives enough GC cycles, then that object gets moved to the middle generation.  If that generation fills up, you GC that generation and move any longer lived objects into the next generation.

If you keep the heap size of this generation small, then the GC cycle is really fast.  In a typical Java application, most of your objects are GC'ed really fast because they are temporary objects.  Think of all the BigDecimal and String instances that you manipulate.  Those classes are final; every operation creates a new instance that can now be GC'ed in the young generation.

With a generational garbage collector, you perform more GC cycles, but since the young generation is small   each young generation GC cycle is a lot faster.  The longer GC cycles run far less frequently.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.