Debian Codegraph @

man/profmem.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/profmem.R
3 \name{profmem}
4 \alias{profmem}
5 \alias{profmem_begin}
6 \alias{profmem_end}
7 \alias{profmem_suspend}
8 \alias{profmem_resume}
9 \alias{profmem_status}
10 \alias{profmem_depth}
11 \title{Memory profiling R}
12 \usage{
13 profmem(
14 expr,
15 envir = parent.frame(),
16 substitute = TRUE,
17 threshold = getOption("profmem.threshold", 0L),
18 on_error = c("ignore", "warning", "error")
19 )
20
21 profmem_begin(threshold = getOption("profmem.threshold", 0L))
22
23 profmem_end()
24
25 profmem_suspend()
26
27 profmem_resume()
28
29 profmem_status()
30
31 profmem_depth()
32 }
33 \arguments{
34 \item{expr}{An \R expression to be evaluated and profiled.}
35
36 \item{envir}{The environment in which the expression should be evaluated.}
37
38 \item{substitute}{Should \code{expr} be \code{\link[base:substitute]{base::substitute()}}:d or not.}
39
40 \item{threshold}{The smallest memory allocation (in bytes) to log.}
41
42 \item{on_error}{(character) Controls whether evaluation errors should
43 signal an error (\code{"error"}), a warning (\verb{"warning"), or be ignored (}"ignore"`; default).}
44 }
45 \value{
46 \code{profmem()} and \code{profmem_end()} returns the collected allocation
47 data as an \code{Rprofmem} data.frame with additional attributes set.
48 An \code{Rprofmem} data.frame has columns \code{what}, \code{bytes}, and \code{trace}, with:
49 \itemize{
50 \item \code{what}: (character) type of memory event;
51 either \code{"alloc"} or \code{"new page"}
52 \item \code{bytes}: (numeric) number of bytes allocated or \code{NA_real_}
53 (when \code{what} is \code{"new page"})
54 \item \code{trace}: (list of character vectors) zero or more function names
55 }
56
57 The attributes set are:
58 \itemize{
59 \item \code{threshold} : The threshold used (= argument \code{threshold})
60 \item \code{expression}: The expression profiled (= argument \code{expr})
61 \item \code{value} : The value of the evaluated expression
62 (only set if there was no error)
63 \item \code{error} : The error object in case the evaluation failed
64 (only set if there was an error)
65 }
66
67 \code{profmem_begin()} returns (invisibly) the number of nested profmem
68 session currently active.
69
70 \code{profmem_suspend()} and \code{profmem_resume()} returns nothing.
71
72 \code{profmem_status()} returns \code{"inactive"}, \code{"active"},
73 or \code{"suspended"}.
74
75 \code{promem_depth()} returns a non-negative integer.
76 }
77 \description{
78 \code{profmem()} evaluates and memory profiles an \R expression.
79
80 \code{profmem_begin()} starts the memory profiling of all the following \R
81 evaluations until \code{profmem_end()} is called.
82 }
83 \details{
84 In order for memory profiling to work, \R must have been \emph{built} with memory
85 profiling enabled. Function
86 \code{\link[base:capabilities]{base::capabilities("profmem")}} will
87 return \code{TRUE} of it is enabled, otherwise \code{FALSE}.
88 If memory profiling is \emph{not} supported, \code{profmem()} and \code{profmem_begin()}
89 will produce an informative error. The pre-built \R binaries on
90 \href{https://cran.r-project.org/}{CRAN} support memory profiling.
91
92 What is logged? The \code{profmem()} function uses \code{\link[utils:Rprofmem]{utils::Rprofmem()}} for
93 logging memory, which logs all memory \emph{allocations} that are done via the
94 \R framework. Specifically, the logger is tied to \code{allocVector3()} part
95 of \R's native API. This means that nearly all memory allocations done
96 in \R are logged. \emph{Neither} memory deallocations \emph{nor} garbage collection
97 events are logged. Furthermore, allocations done by non-\R native libraries
98 or \R packages that use native code \code{Calloc() / Free()} for internal objects
99 are also \emph{not} logged.
100
101 Any memory events that would occur due to calling any of the \pkg{profmem}
102 functions themselves will \emph{not} be logged and \emph{not} be part of the returned
103 profile data (regardless whether memory profiling is active or not).
104 This is intentional.
105
106 If a profmem profiling is already active, \code{profmem()} and \code{profmem_begin()}
107 performs an \emph{independent}, \emph{nested} profiling, which does not affect the
108 already active one. When the active one completes, it will contain all
109 memory events also collected by the nested profiling as if the nested one
110 never occurred.
111
112 Profiling gathered by \pkg{profmem} \emph{will} be corrupted if the code profiled
113 calls \code{\link[utils:Rprofmem]{utils::Rprofmem()}}, with the exception of such calls done via the
114 \pkg{profmem} package itself.
115 }
116 \examples{
117 if (capabilities("profmem")) {
118
119 ## Memory profile an R expression
120 p <- profmem({
121 x <- raw(1000)
122 A <- matrix(rnorm(100), ncol = 10)
123 })
124
125 ## Display the results
126 print(p)
127
128 ## Total amount of memory allocation
129 total(p)
130
131 ## Allocations greater than 1 kB
132 p2 <- subset(p, bytes > 1000)
133 print(p2)
134
135 ## The expression is evaluated in the calling environment
136 str(x)
137 str(A)
138
139 }
140 }