1

I am working through a memory issue with one of our webapps. I am using Chrome's heap profiler. I want to make sure I understand something very explicitely, as I'm making assumptions on this information.

CHrome debuggger

The @ symbol in the heap profile screenshot above. I want to make sure I understand crystal clear: equal object ids implies the same object

a.objectId == b.objectId implies a same as b a.objectId == b.objectId implies NOT a same as b

Therefore if I have two objects that I expected to actually be the same thing, yet their object id differs, this implies an unexpected copy occurred? This implies I can go and figure out in my code where I might be creating unnecessary duplicates?

The documentation appears to say this, but they don't quite say it explicitly, going on to say why they have an object id, not what it represents.

This is an object ID. Displaying an object's address makes no sense, as objects are moved during garbage collections. Those object IDs are real IDs — that means, they persist among multiple snapshots taken and are unique. This allows precise comparison between heap states. Maintaining those IDs adds an overhead to GC cycles, but it is only initiated after the first heap snapshot was taken — no overhead if heap profiles aren't used.

I get that. But I need to fit this back into my C programmer head. I realize, even with native heaps, pointer values can change over time. Can I effectively treat object ids as pointer addresses unique over time?

trincot
  • 317,000
  • 35
  • 244
  • 286
Doug T.
  • 64,223
  • 27
  • 138
  • 202

1 Answers1

0

So I ran some test code in Chrome and the answer appears to be yes, the same object id implies identical objects. If object id differs, this implies a copy or different object altogether.

I profiled the following snippet of code which can be found in this github repo:

(function heapTest(doc) {
  'use strict';

  function clone(obj) {
    return JSON.parse(JSON.stringify(obj));
  }

  var b = {'grandchild-key-2': 5};
  var a = {'child-key-1': b};
  doc.child1 = a;
  doc.child1_again = a;

  doc.child1_copy = clone(a);

})(document);

The heap profiler confirms the two references share object ids, the copy receives a new object id.

enter image description here

In short, this behaves like I expect. Multiple references to the same object receive the same object id. Copies refer to a different object and receive a different object id.

Doug T.
  • 64,223
  • 27
  • 138
  • 202