Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit cab79b3

Browse files
committed
Bug 1539502 - [devtools] Warn when late allocations happen when recording memory leaking. r=jdescottes
Differential Revision: https://phabricator.services.mozilla.com/D127778
1 parent 5f31ca0 commit cab79b3

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

devtools/client/framework/test/allocations/browser_allocations_browser_console.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ async function testScript() {
3333

3434
// Close
3535
await BrowserConsoleManager.toggleBrowserConsole();
36+
37+
// Browser console still cleanup stuff after the resolution of toggleBrowserConsole.
38+
// So wait for a little while to ensure it completes all cleanups.
39+
// eslint-disable-next-line mozilla/no-arbitrary-setTimeout
40+
await new Promise(resolve => setTimeout(resolve, 500));
3641
}
3742

3843
add_task(async function() {

devtools/client/framework/test/allocations/browser_allocations_target.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ async function testScript(tab) {
2222

2323
// destroy the commands to also destroy the dedicated client.
2424
await commands.destroy();
25+
26+
// Spin the event loop to ensure commands destruction is fully completed
27+
// eslint-disable-next-line mozilla/no-arbitrary-setTimeout
28+
await new Promise(resolve => setTimeout(resolve, 0));
2529
}
2630

2731
add_task(async function() {

devtools/client/framework/test/allocations/browser_allocations_toolbox.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ async function testScript(tab) {
2020
await new Promise(resolve => setTimeout(resolve, 1000));
2121

2222
await toolbox.destroy();
23+
24+
// Spin the event loop to ensure toolbox destroy is fully completed
25+
// eslint-disable-next-line mozilla/no-arbitrary-setTimeout
26+
await new Promise(resolve => setTimeout(resolve, 0));
2327
}
2428

2529
add_task(async function() {

devtools/shared/test-helpers/allocation-tracker.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,31 @@ exports.allocationTracker = function({
186186
this.flushAllocations();
187187
}
188188

189+
// In the content process we watch for all globals.
190+
// Disable allocation record immediately, as we get some allocation reported by the allocation-tracker itself.
191+
if (watchAllGlobals) {
192+
dbg.memory.allocationSamplingProbability = 0.0;
193+
}
194+
189195
// Before computing allocations, re-do some GCs in order to free all what is to-be-freed.
190196
await this.doGC();
191197

198+
// If we are in the parent process, we watch only for devtools globals.
199+
// So we can more safely assert that no allocation occured while doing the GCs.
200+
// If means that the test we are recording is having pending operation which aren't properly recorded.
201+
if (!watchAllGlobals) {
202+
const allocations = dbg.memory.drainAllocationsLog();
203+
if (allocations.length > 0) {
204+
this.logAllocationLog(
205+
allocations,
206+
"Allocation that happened during the GC"
207+
);
208+
console.error(
209+
"Allocation happened during the GC. Are you waiting correctly before calling stopRecordingAllocations?"
210+
);
211+
}
212+
}
213+
192214
const memory = this.getAllocatedMemory();
193215
const objects = this.stillAllocatedObjects();
194216

@@ -379,11 +401,15 @@ exports.allocationTracker = function({
379401
* Reported allocations may have been freed.
380402
* Use `logAllocationSitesDiff` to know what hasn't been freed.
381403
*/
382-
logAllocationLog() {
383-
const allocations = dbg.memory.drainAllocationsLog();
404+
logAllocationLog(allocations, msg = "") {
405+
if (!allocations) {
406+
allocations = dbg.memory.drainAllocationsLog();
407+
}
384408
const sources = this.allocationsToSources(allocations);
385409
return this.logAllocationSites(
386-
"all allocations (which may be freed or are still allocated)",
410+
msg
411+
? msg
412+
: "all allocations (which may be freed or are still allocated)",
387413
sources
388414
);
389415
},

0 commit comments

Comments
 (0)