This article covers features introduced in SpiderMonkey 1.8

Enable GC zeal, a testing and debugging feature that helps find GC-related bugs in JSAPI applications.

Syntax

void
JS_SetGCZeal(JSContext *cx, uint8_t zeal, uint32_t frequency);
Name Type Description
cx JSContext * A context. The GC zeal level of the associated JSRuntime is set.
zeal uint8_t The desired level of garbage collection.
frequency uint32_t With some zeal levels, a GC is triggered every frequency allocations.

Description

JS_SetGCZeal sets the level of additional garbage collection to perform for a runtime, for the purpose of finding or reproducing bugs.

There are several different levels which have different functions:

Zeal level Description
0 Normal amount of collection.  The default: no additional collections are performed.
1 Collect when roots are added or removed.
2 Collect when every frequency allocations.
3 Collect on window paints.
4 Verify pre write barriers between instructions.
5 Verify pre write barriers between window paints.
6 Verify stack rooting.
7 Collect the nursery every frequency nursery allocations.
8 Incremental GC in two slices: 1) mark roots 2) finish collection.
9 Incremental GC in two slices: 1) mark all 2) new marking and finish.
10 Incremental GC in multiple slices.
11 Verify post write barriers between instructions.
12 Verify post write barriers between paints.
13 Check internal hashtables on minor GC.
14 Perform a shrinking collection every frequency allocations.

With GC zeal enabled, GC-related crashes are much easier to reproduce (they happen more reliably) and debug (they happen sooner, closer to the source of the bug). The drawback is that GC zeal can cause JavaScript code to run extremely slowly.

Regularly running your test suite with GC zeal enabled at level 2 is a good practice.

This function is available only in DEBUG builds. To enable this function in an optimized build, define the macro JS_GC_ZEAL building SpiderMonkey.

(In a debug build of Gecko, you can also set the current GC zeal level using the javascript.options.gczeal preference.)

See Also