Dan*_*man 10 memory performance r
我正在重写这个问题以澄清我在寻找什么
我想在 RStudio 中监控我的内存使用情况,以便我可以避免在集群上出现内存不足错误。我正在寻找一种计算峰值内存使用量的方法,其中包括 global variables 和 local variables。例如,峰值内存使用量应该考虑函数和应用循环中的中间变量。
编辑:此赏金即将到期,但我仍然没有找到我要找的东西。我想要一些可以连续监控内存使用情况的东西,而不仅仅是打印到控制台。
gc(reset = T)
sum(gc()[, "(Mb)"]) # 172Mb
lapply(1:3, function(x) {
mx <- replicate(10, rnorm(1e6)) # 80Mb object
mean(mx)
})
sum(gc()[, "(Mb)"]) # 172Mb -- still the same!
Run Code Online (Sandbox Code Playgroud)
Dan*_*man 11
我在包裹中找到了我要找的东西peakRAM。从文档中:
该软件包可以轻松监控使用的总 RAM 和峰值 RAM,以便开发人员可以快速识别和消除 RAM 消耗大的代码。
mem <- peakRAM({
for(i in 1:5) {
mean(rnorm(1e7))
}
})
mem$Peak_RAM_Used_MiB # 10000486MiB
Run Code Online (Sandbox Code Playgroud)
lapply权重返回的对象只有 488 字节,因为它是总结性的:垃圾收集在平均值计算后删除了中间对象。
help('Memory')提供有关 R 如何管理内存的有用信息。
特别是,您可以使用object.size()跟踪单个对象的大小,并memory.size()了解每一步使用了多少总内存:
# With mean calculation
gc(reset = T)
#> used (Mb) gc trigger (Mb) max used (Mb)
#> Ncells 405777 21.7 831300 44.4 405777 21.7
#> Vcells 730597 5.6 8388608 64.0 730597 5.6
sum(gc()[, "(Mb)"])
#> [1] 27.3
l<-lapply(1:3, function(x) {
mx <- replicate(10, rnorm(1e6)) # 80Mb object
mean(mx)
print(paste('Memory used:',memory.size()))
})
#> [1] "Memory used: 271.04"
#> [1] "Memory used: 272.26"
#> [1] "Memory used: 272.26"
object.size(l)
#> 488 bytes
## Without mean calculation :
gc(reset = T)
#> used (Mb) gc trigger (Mb) max used (Mb)
#> Ncells 464759 24.9 831300 44.4 464759 24.9
#> Vcells 864034 6.6 29994700 228.9 864034 6.6
gcinfo(T)
#> [1] FALSE
sum(gc()[, "(Mb)"])
#> [1] 31.5
l<-lapply(1:4, function(x) {
mx <- replicate(10, rnorm(1e6))
print(paste('New object size:',object.size(mx)))
print(paste('Memory used:',memory.size()))
mx
})
#> [1] "New object size: 80000216"
#> [1] "Memory used: 272.27"
#> [1] "New object size: 80000216"
#> [1] "Memory used: 348.58"
#> [1] "New object size: 80000216"
#> [1] "Memory used: 424.89"
#> [1] "New object size: 80000216"
#> [1] "Memory used: 501.21"
object.size(l)
#> 320000944 bytes
sum(gc()[, "(Mb)"])
#> [1] 336.7
Run Code Online (Sandbox Code Playgroud)
由reprex 包(v0.3.0)于 2020 年 8 月 20 日创建
如果不是返回mean而是返回整个对象,则内存使用量的增加是显着的。
您可以gc为此使用该功能。
实际上,该gc函数提供了字段 11 和 12 中使用的当前和最大内存(Mb关于文档,但显然是Mio在我的机器上的实践中)。您可以使用参数重置最大值reset=TRUE。下面是一个例子:
> gc(reset=TRUE)
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 318687 17.1 654385 35.0 318687 17.1
Vcells 629952 4.9 397615688 3033.6 629952 4.9
> a = runif(1024*1024*64) # Should request 512 Mio to the GC (on my machine)
> gc()
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 318677 17.1 654385 35.0 318834 17.1
Vcells 67738785 516.9 318092551 2426.9 67739236 516.9
> memInfo <- gc()
> memInfo[11] # Maximum Ncells
[1] 17.1
> memInfo[12] # Maximum Vcells
[1] 516.9
> rm(a) # `a` can be removed by the GC from this point
> gc(reset=TRUE) # Order to reset the GC infos including the maximum
used (Mb) gc trigger (Mb) max used (Mb)
Ncells 318858 17.1 654385 35.0 318858 17.1
Vcells 630322 4.9 162863387 1242.6 630322 4.9
> memInfo <- gc()
> memInfo[11]
[1] 17.1
> memInfo[12] # The maximum has been correctly reset
[1] 4.9
Run Code Online (Sandbox Code Playgroud)
在这个例子中我们可以看到,516.9 - 4.9 = 512 Mb在围绕该runif调用的两个 gc 调用之间,GC 已经分配了up 到(与预期的结果一致)。
| 归档时间: |
|
| 查看次数: |
569 次 |
| 最近记录: |