G1

If you would like to have both low latency and high throughput, Java 1.7 update 4 onwards has another option called garbage-first GC (G1). G1 is a server-style garbage collector, primarily meant for multicore machines with large memories. It is planned as a long-term replacement for CMS. So, to modify our thumb rule, if you are using Java 7 onwards, simply use G1.

G1 partitions the heap into a set of equal-sized regions, where each set is a contiguous range of virtual memory. Each region is assigned a role, such as Eden, Survivor, Old. G1 performs a concurrent global marking phase to determine the live references of objects throughout the heap. After the mark phase is over, G1 knows which regions are mostly empty. It collects objects with no live references, in these regions first, and this frees the larger amount of memory. A typical G1 heap may look like this:

The regions selected by G1 as candidates for garbage collection are garbage collected using evacuation. G1 copies objects from one or more regions of the heap to a single region on the heap, and it both compacts and frees up memory. This evacuation is performed in parallel on multiple cores to reduce pause times and increase throughput. So, each garbage collection round reduces fragmentation while working within user-defined pause times.

There are three aspects in memory optimization in Java:

  • Memory footprint
  • Cost of accessing objects in memory
  • Cost of garbage collection

Java objects, in general, are fast to access but consume much more space than the actual data inside them.

JVM garbage collection can be a challenge if you have a lot of short-lived RDDs. JVM needs to go over all the objects to find the ones it needs to garbage collect. The cost of the garbage collection is proportional to the number of objects the GC needs to go through. Therefore, using fewer objects and the data structures that use fewer objects (simpler data structures, such as arrays) helps.

Serialization also shines here as a byte array needs only one object to be garbage collected.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset