Skip to content

What are the first line of a heap dump that are not address ? #1

@tenderlove

Description

@tenderlove

Those are "ROOTS" (the ones with type = "ROOTS"). The system has various "roots".

What is a "ROOT"?

Objects in the system form a tree data structure. Imagine some code like this:

a = "foo"
b = "bar"
c = { a => b }

It's easy to see how c is connected to a and b and prevents them from being garbage collected:

abc

But say we have this:

a = "foo"
b = "bar"
c = { a => b }
GC.start
p c

What prevents c from being garbage collected? Something must hold a reference to c so that the garbage collector can know that it shouldn't be GC'd. This is where ROOTS come in. Roots are special nodes in the system that hold on to these references:

roots

MRI has a few different places it considers a root, and those are in the name root (like vm, finalizers, etc). Explaining each of these types is a little too long for here, but you can think of them as the root of the tree that forms your object graph. They keep your program objects alive, and they are the starting point for the GC to find live objects in your system. In fact the graph I showed above is a little inaccurate. Since a and b are also top level local variables like c, the graph looks a bit more like this:

total

Hope that helps!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions