Wednesday, June 20, 2012

Garbage Collection in JavaScript, Part 2

Of the Chrome V8 JavaScript engine's 198kloc source (excluding comments), about 19kloc comprise the garbage collector. It is a generational Cheney copying collector with mark sweep. In October 2011 the V8 team added an incremental garbage collector to the mix. Incremental garbage collection contrasts with traditional stop-the-world garbage collection in that it is more amenable to low-latency applications requiring minimizing garbage collection pauses at the cost of reduced total throughput. The V8 garbage collector actually has a whole assortment of configurable flags (see shell --help). If these options can be tweaked, one can provide a customized JavaScript experience (especially garbage collector experience) tailored to the particular performance needs of the app.

Running the latest version of V8 (svn revision 11853) on the V8 benchmarks (admittedly probably not the most relevant or indicative example), I see 1206 garbage collection events (through the --trace_gc option). A great majority of the GC events are the cheap nursery (new generation) collections which take a couple of milliseconds. Most of the allocations are small (< 64 bytes). 6 of the 7 major mark-sweep GCs are due to the Splay benchmark, which should be unsurprising since that is the main memory allocation and GC benchmark in the suite. The Splay benchmark itself contributes 380 GC events. The max GC pause was 60ms (get this information with the "--print_cumulative_gc_stat" option). This last bit is one of the key data points for low-latency apps. According to some experiments out of UWisc Milwaukee, between 150-195ms is the threshold of human perception with respect to GUIs and keyboard/mouse input. I am somewhat curious what the threshold looks like for modern multi-touch interfaces.

Turning on the gc_global flag dramatically increases the number of mark-sweep collections to 23 but decreases the max_gc_pause to 55ms. Total GC events decreases to 121.

1 comment:

  1. Try --new-space-size=1024 --never-compact for lower pauses.


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