summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/mozilla/tests/webgpu/common/util/collect_garbage.js
blob: 9b7ef10af5175ee72c1d7a46a02a31d294ad076b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* AUTO-GENERATED - DO NOT EDIT. Source: https://github.com/gpuweb/cts
**/import { resolveOnTimeout } from './util.js';


/**
 * Attempts to trigger JavaScript garbage collection, either using explicit methods if exposed
 * (may be available in testing environments with special browser runtime flags set), or using
 * some weird tricks to incur GC pressure. Adopted from the WebGL CTS.
 */
export async function attemptGarbageCollection() {

  const w = globalThis;
  if (w.GCController) {
    w.GCController.collect();
    return;
  }

  if (w.opera && w.opera.collect) {
    w.opera.collect();
    return;
  }

  try {
    w.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
    getInterface(Components.interfaces.nsIDOMWindowUtils).
    garbageCollect();
    return;
  } catch (e) {

    // ignore any failure
  }
  if (w.gc) {
    w.gc();
    return;
  }

  if (w.CollectGarbage) {
    w.CollectGarbage();
    return;
  }

  let i;
  function gcRec(n) {
    if (n < 1) return;

    let temp = { i: 'ab' + i + i / 100000 };

    temp = temp + 'foo';
    temp; // dummy use of unused variable
    gcRec(n - 1);
  }
  for (i = 0; i < 1000; i++) {
    gcRec(10);
  }

  return resolveOnTimeout(35); // Let the event loop run a few frames in case it helps.
}