Gathering Scattered Objects

This page demonstrates how an overview of the space occupied by the objects of the same kind can be evaluated using the Heap Profiler.

Below is the source code of the script, for reference:

function Item(x)
{
  this.x = x;
}

function numbers()
{
  var result = new Array(10000);
  for (var i = 0, l = result.length; i < l; ++i)
    result[i] = new Item(i);
  return new Item(result);
}

function strings()
{
  var result = new Array(10000);
  for (var i = 0, l = result.length; i < l; ++i)
    result[i] = new Item(i.toString());
  return new Item(result);
}

function init()
{
  numberCache = numbers();
  stringCache = strings();
  documentCache = new Item(document.body.textContent.toLowerCase());
}

Try this:

  • Take a heap snapshot
  • Open the Summary view

Objects of the same kind can be scattered among application data, and unintentionally, form a significant group, wasting memory. The Summary view helps to discover excessive instantiations.

For example, Item objects are used for different storage purposes among application code. All objects are grouped in the Summary view, regardless of their origin, and statistics for them is provided.