V8 JSObject Memory Exercise
In this exercise you will inspect how basic JS objects are laid out in memory.
You can launch a v8 REPL under gdb with exercise run v8 --gdb.
Create Objects to Inspect
Create several objects which we will inspect the underlying memory of. Here are some ideas:
- Create an empty object
{} - Create an object with a single property
{'a': 4142} - Create an object and assign properties and elements
{'a': 4142, 'b': 42, 0: 61, 2: 62} - Create an object with integers, doubles, and references
{'a': 4142, 'b': 1.1, 'c': {}, 'd': []}
Inspect the Memory of the Objects
For each of the objects you created:
- Use
%DebugPrintto dump the object's information- NOTE: The order of the
%DebugPrintoutput does not match the actual order of values in memory - Observe how each object of each "different shape" has different Map pointers
- If you make two objects with the same shape (same property names and types) they may share the same Map pointer
- Notice how there may be "inline properties", a "property array", and an "element array"
- NOTE: The order of the
- Break into gdb by hitting
ctrl-cor using%SystemBreak() - Dump the memory of each object using
x/32xg <address>(remember to remove the tag bit!)- Try to match the memory to our object layout diagrams
- Try printing the memory of the property and element arrays (remember to remove the tag bit!)
Map Transitions
Using %DebugPrint we can see what Map pointer each object is using. If we modify the object in specific ways (such as adding properties), we can observe this Map pointer changing.
Create an object with a single property
foo = {'a': 4142}- Use
%DebugPrintto checkfooMap pointer
- Use
Now create a new object using the same original property name
bar = {'a': 2000}- Use
%DebugPrintto checkbarMap pointer, it should be the same asfoo
- Use
Add a new property to the object
foo['b'] = 6262- Use
%DebugPrintto checkfooMap pointer again, did it change?
- Use
Add a new property to the object
bar['b'] = 4000- Use
%DebugPrintto checkbarMap pointer again, it should matchfooagain
- Use