gpt4 book ai didi

r - 监视 R 中的内存使用情况

转载 作者:行者123 更新时间:2023-12-03 07:51:17 28 4
gpt4 key购买 nike

是否可以监视 R 正在使用或已用于调用函数的内存量?例如,我有一个任意函数,例如:

smallest.sv <- function(){
A <- matrix(rnorm(1e6), 1e3);
mysvd <- svd(A);
return(tail(mysvd$d, 1));
}

运行该函数只是返回一个标量,但大量内存用于计算该函数。现在我需要做性能基准测试。处理时间很简单:
system.time(x <- smallest.sv())

但是我也想知道这个调用需要多少内存,而不修改函数(它应该适用于任意函数)。有没有办法做到这一点?

编辑:澄清一点。我最感兴趣的是在函数调用期间使用的内存上限,即需要多少物理内存才能处理函数调用。在许多情况下,这明显小于我认为的分配内存总量。

最佳答案

R 提供内存分析支持,见 Section 3.3 of the Writing R Extensions manual :

3.3 Profiling R code for memory use

Measuring memory use in R code is useful either when the code takes more memory than is conveniently available or when memory allocation and copying of objects is responsible for slow code. There are three ways to profile memory use over time in R code. All three require R to have been compiled with `--enable-memory-profiling', which is not the default, but is currently used for the Mac OS X and Windows binary distributions. All can be misleading, for different reasons.

In understanding the memory profiles it is useful to know a little more about R's memory allocation. Looking at the results of `gc()' shows a division of memory into `Vcells' used to store the contents of vectors and `Ncells' used to store everything else, including all the administrative overhead for vectors such as type and length information. In fact the vector contents are divided into two pools. Memory for small vectors (by default 128 bytes or less) is obtained in large chunks and then parcelled out by R; memory for larger vectors is obtained directly from the operating system.



然后再提供三个部分。

关于r - 监视 R 中的内存使用情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7856306/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com