πŸ‘©β€πŸ”§ RTrace and Memory Leaks
Check your leaky code!
If an expectation fails and hits an unreachable() instruction, any unreleased references in the function call stack will be held indefinitely as a memory leak. Test Suites don't stop running if they fail the test callback. However, tests will stop if they fail inside the beforeEach(), beforeAll(), afterEach(), and afterAll() callbacks.
Typically, a throws() test will leave at least a single Expectation on the heap. This is to be expected, because the unreachable() instruction unwinds the stack, and prevents the ability for each function to __release a reference pointer properly. Your test suite output may look like this:
1
[Describe]: toHaveLength TypedArray type: Uint32Array
2
​
3
[Success]: βœ” should assert expected length
4
[Throws]: βœ” when expected length should not equal the same value RTrace: +3
5
[Success]: βœ” should verify the length is not another value
6
[Throws]: βœ” when the length is another expected value RTrace: +3
Copied!
The RTrace: +3 corresponds to an Expectation, a Uint32Array, and a single backing ArrayBuffer that was left on the heap because of the fact that the expectation failed. This was expected because these two tests were annotated with the throws(desc, callback) function. If you see a function that is expected to pass and RTrace returns a very large value, it might be an indicator of a very serious memory leak, and the DefaultTestReporter can be your best friend when it comes to finding these sorts of problems.
Among other solutions, the following methods are exposed to you as a way to inspect how many allocations and frees occurred during the course of function execution.
1
declare class RTrace {
2
​
3
/** Return the current number of active references on the heap. */
4
public static count(): i32;
5
​
6
/**
7
* Start counting heap allocations.
8
*
9
* @param {i32} label - The numeric label for this refcounting group.
10
*/
11
public static start(label: i32): void;
12
​
13
/**
14
* End counting heap allocations.
15
*
16
* @param {i32} label - The numeric label for this refcounting group.
17
* @returns The delta number of heap allocations relative to the start.
18
*/
19
public static end(label: i32): i32;
20
​
21
/** Overall module count for ARC increments. */
22
public static increments(): i32;
23
​
24
/** Overall module count for ARC decrements. */
25
public static decrements(): i32;
26
​
27
/** Overall TestGroup count for ARC increments. */
28
public static groupIncrements(): i32;
29
​
30
/** Overall TestGroup count for ARC decrements. */
31
public static groupDecrements(): i32;
32
​
33
/** Overall Test count for ARC increments. */
34
public static testIncrements(): i32;
35
​
36
/** Overall Test count for ARC decrements. */
37
public static testDecrements(): i32;
38
​
39
/** Overall count for ARC allocations. */
40
public static allocations(): i32;
41
42
/** Overall count for ARC frees. */
43
public static frees(): i32;
44
​
45
/** TestGroup count for ARC allocations. */
46
public static groupAllocations(): i32;
47
48
/** TestGroup count for ARC frees. */
49
public static groupFrees(): i32;
50
​
51
/** Test count for ARC allocations. */
52
public static testAllocations(): i32;
53
​
54
/** Test count for ARC frees. */
55
public static testFrees(): i32;
56
​
57
/** Overall count for ARC reallocations. */
58
public static reallocations(): i32;
59
​
60
/** TestGroup count for ARC reallocations. */
61
public static groupReallocations(): i32;
62
​
63
/** TestGroup count for ARC reallocations. */
64
public static testReallocations(): i32;
65
​
66
/**
67
* This method triggers a garbage collection.
68
*/
69
public static collect(): void;
70
​
71
/**
72
* Get the type id (class id) of the pointer.
73
*
74
* @param {usize} pointer - The pointer.
75
* @returns {u32} - The type id of the allocated block.
76
*/
77
public static typeIdOf(pointer: usize): u32;
78
​
79
/**
80
* Get the type id (class id) of a reference.
81
*
82
* @param {T} reference - The reference.
83
* @returns {u32} - The type id of the allocated block.
84
*/
85
public static typeIdOfReference<T>(reference: T): u32;
86
​
87
/**
88
* Get the size of a pointer.
89
*
90
* @param {usize} pointer - The pointer.
91
* @returns {u32} - The size of the allocated block.
92
*/
93
public static sizeOf(pointer: usize): u32;
94
​
95
/**
96
* Get the size of a reference.
97
*
98
* @param {T} reference - The reference.
99
* @returns {u32} - The size of the allocated block.
100
*/
101
public static sizeOfReference<T>(reference: T): u32;
102
​
103
/**
104
* Get the currently allocated blocks.
105
*/
106
public static activeBlocks(): usize[];
107
​
108
/**
109
* Get the current groups allocated blocks.
110
*/
111
public static activeGroupBlocks(): usize[];
112
​
113
/** Get the current tests allocated blocks. */
114
public static activeTestBlocks(): usize[];
115
​
116
/** Get the current ARC count of a given pointer. */
117
public static refCountOf(ptr: usize): u32;
118
​
119
/**
120
* Gets the current count of the specified reference.
121
* @param {T} reference - the reference.
122
*/
123
public static refCountOfReference<T>(reference: T): u32;
124
}
Copied!
Every one of these functions exist in the RTrace namespace and will call into JavaScript to query the state of the heap relative to the overall test file, the test group, and each individual test depending on the function.
RTrace can be disabled with the --nortrace cli option, to avoid collecting reference counting statistics.
Last modified 1yr ago
Copy link