Components.utils.schedulePreciseGC

  • Revision slug: Components.utils.schedulePreciseGC
  • Revision title: Components.utils.schedulePreciseGC
  • Revision id: 75691
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment new for bug 661927; page created, 199 words added

Revision Content

{{ gecko_minversion_header("7.0") }}

This method lets scripts schedule a garbage collection cycle. The garbage collection cycle will occur sometime in the future, when no JavaScript code is executing. This is useful particularly when testing for memory leaks, because normal garbage collection is conservative when JavaScript code is running to ensure that in-use memory isn't inadvertently collected.

Note: Garbage collection is a very expensive operation and should only be forced when it is known to be absolutely necessary.

Requires UniversalXPConnect privileges.

Using schedulePreciseGC()

When you call Components.utils.schedulePreciseGC(), you specify a callback that is executed in once the scheduled garbage collection has been completed:

Components.utils.schedulePreciseGC(
  function() {
    // This code is executed when the garbage collection has completed
  }
); 

Since the garbage collection doesn't occur until some time in the future (unlike, for example, Components.utils.forceGC(), which causes garbage collection immediately but isn't able to collect all JavaScript-related memory), the callback lets you know when that's been finished.

See also

If you want to schedule garbage collection to occur in the future, at a time when no JavaScript code is running, you can use Components.utils.schedulePreciseGC() instead.

Revision Source

<p>{{ gecko_minversion_header("7.0") }}</p>
<p>This method lets scripts schedule a garbage collection cycle. The garbage collection cycle will occur sometime in the future, when no JavaScript code is executing. This is useful particularly when testing for memory leaks, because normal garbage collection is conservative when JavaScript code is running to ensure that in-use memory isn't inadvertently collected.</p>
<div class="note"><strong>Note:</strong> Garbage collection is a very expensive operation and should only be forced when it is <strong>known</strong> to be absolutely necessary.</div>
<p>Requires <code>UniversalXPConnect</code> privileges.</p>
<h2 id="Using_schedulePreciseGC()">Using schedulePreciseGC()</h2>
<p>When you call <code>Components.utils.schedulePreciseGC()</code>, you specify a callback that is executed in once the scheduled garbage collection has been completed:</p>
<pre class="brush: js">Components.utils.schedulePreciseGC(
  function() {
    // This code is executed when the garbage collection has completed
  }
); 
</pre>
<p>Since the garbage collection doesn't occur until some time in the future (unlike, for example, <a href="/en/Components.utils.forceGC" title="en/Components.utils.forceGC"><code>Components.utils.forceGC()</code></a>, which causes garbage collection immediately but isn't able to collect all JavaScript-related memory), the callback lets you know when that's been finished.</p>
<h2 id="See_also">See also</h2>
<ul> <li><a href="/en/Components.utils.forceGC" title="en/Components.utils.forceGC"><code>Components.utils.forceGC()</code></a></li> <li>{{ ifmethod("nsIDOMWindowUtils","garbageCollect") }}</li>
</ul>
<p>If you want to schedule garbage collection to occur in the future, at a time when no JavaScript code is running, you can use <a href="/en/Components.utils.schedulePreciseGC" title="en/Components.utils.schedulePreciseGC"><code>Components.utils.schedulePreciseGC()</code></a> instead.</p>
Revert to this revision