Record a memory snapshot for all the R objects reachable from x.

Usage,
mem_snapshot(x)

Arguments

x

An R object. This becomes the root of the graph of R objects captured in the snapshot. A good starting object is the namespace registry. See root_ns_registry().

Value

A data frame containing nodes, arrows between these nodes, and metadata.

Details

The snapshot keeps all the captured objects alive to make it easy to compare multiple snapshots. The snapshot objects are identified by their address in memory but there is no risk of R reusing a memory address with a different object between runs.

Excluding objects from snapshots

To avoid ulterior snapshots recording previous ones, all objects in the global environment are excluded from the snapshot, unless reachable through another path.

If you record a snapshot of the R precious list (see ?roots), objects of the global environments will be reachable through the global binding cache that lives in the precious list. In that case, exclude these objects from snapshots using mem_stash().

See also

The GC roots. mem_stash() to prevent objects from being recorded in a snapshot. mem_diff() to take the difference between a "before" and "after" snapshot.

Examples

# Take a snapshot
s <- mem_snapshot(list(1, list(2)))
#> Creating snapshot...
#> Computing dominance and retainment...
#> Creating data frame...
s
#> # A tibble: 4 × 8
#>   id        type  node    n_parents n_children n_retained retained_size gc_depth
#>   <chr>     <chr> <list>      <int>      <int>      <int>     <bch:byt>    <int>
#> 1 0x565252… list  <mmtls…         0          2          3          232B        1
#> 2 0x565252… doub… <mmtls…         1          0          0           56B        2
#> 3 0x565252… list  <mmtls…         1          1          1          112B        2
#> 4 0x565252… doub… <mmtls…         1          0          0           56B        3

# Inspect nodes
n <- s$node[[1]]
n
#> <memtools:node> (root)
#> id: "0x565252b8d848"
#> type: "list"
#> parents: list [0]
#> children: list [2]
#> dominator: <NULL>
#> dominated: list [2]
#> self_size: 64B
#> retained_size: 232B
#> gc_depth: 1

# Inspect arrows
n$children
#> [[1]]
#> <memtools:arrow>
#> from: <mmtls_nd>
#> to: <mmtls_nd>
#> depth: 1
#> rel: "list_elt"
#> 
#> [[2]]
#> <memtools:arrow>
#> from: <mmtls_nd>
#> to: <mmtls_nd>
#> depth: 1
#> rel: "list_elt"
#> i: 1
#>