How Twitter Improved JVM Performance by Reducing GC and Faster Memory Allocation

Netty is a high-performance NIO (New IO) client server framework for Java that Twitter uses internally as a protocol agonostic RPC system. Twitter found some problems with Netty 3's memory management for buffer allocations beacause it generated a lot of garbage during operation. When you send as many messages as Twitter it creates a lot of GC pressure and the simple act of zero filling newly allocated buffers consumed 50% of memory bandwidth.

Netty 4 fixes this situation with:

  • Short-lived event objects, methods on long-lived channel objects are used to handle I/O events.
  • Secialized buffer allocator that uses pool which implements buddy memory allocation and slab allocation.

The result:

  • 5 times less frequent GC pauses: 45.5 vs. 9.2 times/min
  • 5 times less garbage production: 207.11 vs 41.81 MiB/s
  • The buffer pool is much faster than JVM as the size of the buffer increases. Some problems with smaller buffers.

Given how many services use the JVM in their messaging infrastructure and how many services have GC related performance problems, this is in impressive result others may want to consider.