在 R 会话中管理可用内存的技巧 [英] Tricks to manage the available memory in an R session

查看:21
本文介绍了在 R 会话中管理可用内存的技巧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人们使用什么技巧来管理交互式 R 会话的可用内存?我使用下面的函数 [基于 Petr Pikal 和 David Hinds 在 2004 年发布到 r-help 列表的帖子] 来列出(和/或排序)最大的对象,并偶尔 rm()他们.但到目前为止,最有效的解决方案是……在具有充足内存的 64 位 Linux 下运行.

What tricks do people use to manage the available memory of an interactive R session? I use the functions below [based on postings by Petr Pikal and David Hinds to the r-help list in 2004] to list (and/or sort) the largest objects and to occassionally rm() some of them. But by far the most effective solution was ... to run under 64-bit Linux with ample memory.

大家还想分享其他什么好技巧吗?每个帖子一个.

Any other nice tricks folks want to share? One per post, please.

# improved list of objects
.ls.objects <- function (pos = 1, pattern, order.by,
                        decreasing=FALSE, head=FALSE, n=5) {
    napply <- function(names, fn) sapply(names, function(x)
                                         fn(get(x, pos = pos)))
    names <- ls(pos = pos, pattern = pattern)
    obj.class <- napply(names, function(x) as.character(class(x))[1])
    obj.mode <- napply(names, mode)
    obj.type <- ifelse(is.na(obj.class), obj.mode, obj.class)
    obj.size <- napply(names, object.size)
    obj.dim <- t(napply(names, function(x)
                        as.numeric(dim(x))[1:2]))
    vec <- is.na(obj.dim)[, 1] & (obj.type != "function")
    obj.dim[vec, 1] <- napply(names, length)[vec]
    out <- data.frame(obj.type, obj.size, obj.dim)
    names(out) <- c("Type", "Size", "Rows", "Columns")
    if (!missing(order.by))
        out <- out[order(out[[order.by]], decreasing=decreasing), ]
    if (head)
        out <- head(out, n)
    out
}
# shorthand
lsos <- function(..., n=10) {
    .ls.objects(..., order.by="Size", decreasing=TRUE, head=TRUE, n=n)
}

推荐答案

为了进一步说明频繁重启的常见策略,我们可以使用 littler 允许我们直接从命令行运行简单的表达式.这是我有时用来为一个简单的 crossprod 计时不同的 BLAS 的示例.

To further illustrate the common strategy of frequent restarts, we can use littler which allows us to run simple expressions directly from the command-line. Here is an example I sometimes use to time different BLAS for a simple crossprod.

 r -e'N<-3*10^3; M<-matrix(rnorm(N*N),ncol=N); print(system.time(crossprod(M)))'

同样,

 r -lMatrix -e'example(spMatrix)'

加载 Matrix 包(通过 --packages | -l 开关)并运行 spMatrix 函数的示例.由于 r 总是以 'fresh' 开头,所以这种方法在包开发过程中也是一个很好的测试.

loads the Matrix package (via the --packages | -l switch) and runs the examples of the spMatrix function. As r always starts 'fresh', this method is also a good test during package development.

最后但并非最不重要的一点是 r 也适用于使用 '#!/usr/bin/r' shebang-header 的脚本中的自动批处理模式.Rscript 是 littler 不可用的替代方法(例如在 Windows 上).

Last but not least r also work great for automated batch mode in scripts using the '#!/usr/bin/r' shebang-header. Rscript is an alternative where littler is unavailable (e.g. on Windows).

这篇关于在 R 会话中管理可用内存的技巧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆