summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/gc/bug-1505622.js
blob: 3f6f83c076dfc5c4f3fcf810f96e9422f993648c (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
// Test that we don't repeatedly trigger last-ditch GCs.

// Turn of any zeal which will disrupt GC number checks.
gczeal(0);

function allocUntilFail() {
  gc(null, 'shrinking');

  const initialSize = gcparam("gcBytes");
  const initialMaxSize = gcparam("maxBytes");
  const initGCNumber = gcparam("majorGCNumber");

  // Set a small heap limit.
  gcparam("maxBytes", initialSize + 16 * 1024);

  let error;
  try {
    let a = [];
    while (true) {
      a.push(Symbol()); // Symbols are tenured.
    }
  } catch(err) {
    error = err;
  }

  const finalGCNumber = gcparam("majorGCNumber");

  // Resetore heap limit.
  gcparam("maxBytes", initialMaxSize);

  gc();
  assertEq(error, "out of memory");
  return finalGCNumber - initGCNumber;
}

// Set the time limit for skipping last ditch GCs to 5 seconds.
gcparam("minLastDitchGCPeriod", 5);
assertEq(gcparam("minLastDitchGCPeriod"), 5);

// Allocate up to the heap limit. This triggers a last ditch GC.
let gcCount = allocUntilFail();
assertEq(gcCount, 1)

// Allocate up to the limit again. The second time we fail without
// triggering a GC.
gcCount = allocUntilFail();
assertEq(gcCount, 0)

// Wait for time limit to expire.
sleep(6);

// Check we trigger a GC again.
gcCount = allocUntilFail();
assertEq(gcCount, 1)