[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  tracemem: Trace Copying of Objects

##  Description

This function marks an object so that a message is printed whenever the
internal code copies the object. It is a major cause of hard-to-predict memory
use in R.

##  Usage

    
    
    tracemem(x)
    untracemem(x)
    retracemem(x, previous = NULL)

##  Arguments

x

An R object, not a function or environment or ` NULL ` .

previous

A value as returned by ` tracemem ` or ` retracemem ` .

##  Value

A character string for identifying the object in the trace output (an address
in hex enclosed in angle brackets), or ` NULL ` (invisibly).

##  Details

This functionality is optional, determined at compilation, because it makes R
run a little more slowly even when no objects are being traced. ` tracemem `
and ` untracemem ` give errors when R is not compiled with memory profiling; `
retracemem ` does not (so it can be left in code during development).

It is enabled in the CRAN macOS and Windows builds of  R  .

When an object is traced any copying of the object by the C function `
duplicate ` produces a message to standard output, as does type coercion and
copying when passing arguments to ` .C ` or ` .Fortran ` .

The message consists of the string ` tracemem ` , the identifying strings for
the object being copied and the new object being created, and a stack trace
showing where the duplication occurred. ` retracemem() ` is used to indicate
that a variable should be considered a copy of a previous variable (e.g.,
after subscripting).

The messages can be turned off with ` [ tracingState
](/link/tracingState?package=base&version=3.6.2) ` .

It is not possible to trace functions, as this would conflict with ` [ trace
](/link/trace?package=base&version=3.6.2) ` and it is not useful to trace `
NULL ` , environments, promises, weak references, or external pointer objects,
as these are not duplicated.

These functions are [ primitive ](/link/primitive?package=base&version=3.6.2)
.

##  See Also

` [ capabilities ](/link/capabilities?package=base&version=3.6.2) ("profmem")
` to see if this was enabled for this build of  R  .

` [ trace ](/link/trace?package=base&version=3.6.2) ` , ` [ Rprofmem
](/link/Rprofmem?package=base&version=3.6.2) `

[ https://developer.r-project.org/memory-profiling.html
](https://developer.r-project.org/memory-profiling.html)

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Ftracemem&utm_source=r-docs&utm_medium=docs&utm_term=tracemem&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    a <- 1:10
    tracemem(a)
    ## b and a share memory
    b <- a
    b[1] <- 1
    untracemem(a)
    
    ## copying in lm: less than R <= 2.15.0
    d <- stats::rnorm(10)
    tracemem(d)
    lm(d ~ a+log(b))
    
    ## f is not a copy and is not traced
    f <- d[-1]
    f+1
    ## indicate that f should be traced as a copy of d
    retracemem(f, retracemem(d))
    f+1
    # }
    

Run the code above in your browser using  [ DataCamp Workspace
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Ftracemem&utm_source=r-docs&utm_medium=docs&utm_term=tracemem&utm_content=run_example_in_workspace)

Powered by  [ DataCamp ](https://www.datacamp.com/)