Memory-takeCensus-02.js (1717B)
1 // Debugger.Memory.prototype.takeCensus behaves plausibly as we allocate objects. 2 3 // Exact object counts vary in ways we can't predict. For example, 4 // BaselineScripts can hold onto "template objects", which exist only to hold 5 // the shape and type for newly created objects. When BaselineScripts are 6 // discarded, these template objects go with them. 7 // 8 // So instead of expecting precise counts, we expect counts that are at least as 9 // many as we would expect given the object graph we've built. 10 11 load(libdir + 'census.js'); 12 13 // A Debugger with no debuggees had better not find anything. 14 var dbg = new Debugger; 15 var census0 = dbg.memory.takeCensus(); 16 Census.walkCensus(census0, "census0", Census.assertAllZeros); 17 18 function newGlobalWithDefs() { 19 var g = newGlobal({newCompartment: true}); 20 g.eval(` 21 function times(n, fn) { 22 var a=[]; 23 for (var i = 0; i<n; i++) 24 a.push(fn()); 25 return a; 26 }`); 27 return g; 28 } 29 30 // Allocate a large number of various types of objects, and check that census 31 // finds them. 32 var g = newGlobalWithDefs(); 33 dbg.addDebuggee(g); 34 35 g.eval('var objs = times(100, () => ({}));'); 36 g.eval('var rxs = times(200, () => /foo/);'); 37 g.eval('var ars = times(400, () => []);'); 38 g.eval('var fns = times(800, () => () => {});'); 39 40 var census1 = dbg.memory.takeCensus(); 41 Census.walkCensus(census1, "census1", 42 Census.assertAllNotLessThan( 43 { 'objects': 44 { 'Object': { count: 100 }, 45 'RegExp': { count: 200 }, 46 'Array': { count: 400 }, 47 'Function': { count: 800 } 48 } 49 }));