Nos bénévoles n'ont pas encore traduit cet article en Français. Aidez-nous à réaliser cette tâche !

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.


void JS_SetGCZeal(JSContext *cx, uint8 zeal, uint32 frequency, JSBool compartment);
Name Type Description
cx JSContext * A context. The GC zeal level of the associated JSRuntime is set.
zeal uint8 The desired level of garbage collection: 0 for normal, 1 for very frequent GC, 2 for extremely frequent GC.
frequency uint32 When zeal is 2, a GC is triggered every frequency allocations.
compartment JSBool If this is true, the triggered allocation will be a compartment GC. Otherwise it will be a full GC. Obsolete since Gecko 14


JS_SetGCZeal sets the level of garbage collection for a runtime. This is the zeal argument. At level 0, the default, garbage collection rarely happens unless the application explicitly requests it. At level 1, SpiderMonkey collects at places that are likely to cause problems for embedders, such as when roots are removed. At level 2, SpiderMonkey collects every time an object, string, or function is allocated (or really every frequency allocations). When the compartment argument is true, only the current compartment is collected. Otherwise, all compartments are collected.

Note: The compartment parameter was removed in Gecko 14.

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 causes JavaScript code to run very (or 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 when building SpiderMonkey.

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

MXR ID Search for JS_SetGCZeal

Étiquettes et contributeurs liés au document

Contributeurs ayant participé à cette page : Sheppy, ethertank, Jorend, billmccloskey
Dernière mise à jour par : ethertank,