tracked-objects.sys.mjs (1716B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 // Test-only module in order to register objects later inspected by 6 // the allocation tracker (in the same folder). 7 // 8 // We are going to store a weak reference to the passed objects, 9 // in order to prevent holding them in memory. 10 // Allocation tracker will then print detailed information 11 // about why these objects are still allocated. 12 13 const objects = []; 14 15 /** 16 * Request to track why the given object is kept in memory, 17 * later on, when retrieving all the watched object via getStillAllocatedObjects. 18 */ 19 export function track(obj) { 20 // We store a weak reference, so that we do force keeping the object in memory!! 21 objects.push(Cu.getWeakReference(obj)); 22 } 23 24 /** 25 * Return the reference and NodeId's of all the objects passed via `track()` method, 26 * which are still hold in memory. 27 * 28 * NodeId's are used by spidermonkey memory API to designates JS objects in heap snapshots. 29 */ 30 export function getStillAllocatedObjects() { 31 return ( 32 objects 33 // Filter out objects which have been freed already 34 .filter(ref => !!ref.get()) 35 .map(weakRef => { 36 return { 37 weakRef, 38 // Convert objects from here instead of from allocation tracker in order 39 // to be from the shared system compartment and avoid trying to compute the NodeId 40 // of a wrapper! 41 ubiNodeId: ChromeUtils.getObjectNodeId(weakRef.get()), 42 }; 43 }) 44 ); 45 } 46 47 /** 48 * Used by tests to clear all tracked objects 49 */ 50 export function clear() { 51 objects.length = 0; 52 }