【问题标题】:Amount of available memory in cache initialised with the R function: `memoise::memoise()`使用 R 函数初始化的缓存中的可用内存量:`memoise::memoise()`
【发布时间】:2021-05-26 01:32:17
【问题描述】:

我反复调用一个这样初始化的记忆函数:

library(memoise)
memoised_func <- memoise::memoise(func)

我知道我可以使用 cache 参数 (https://www.rdocumentation.org/packages/memoise/versions/2.0.0/topics/memoise) 设置分配给 memoised_func 的缓存大小。但是有没有办法在任何一个时间点查看缓存中有多少可用内存? 我问是因为我想知道它是否已满,因此我的程序可以通过增加缓存的大小来加速。

(如果相关,我会在 Ubuntu 20.10 上运行 R 4.0.2

【问题讨论】:

    标签: r caching memoization memoise


    【解决方案1】:

    我找到了解决此问题的解决方法。

    默认情况下memoise 使用cachem::cache_mem 创建缓存。或者,我可以使用cachem::cache_disk,它允许我指定文件系统上缓存的位置。然后看看缓存中有多少空闲内存就很简单了:

    library(memoise)
    library(cachem)
    
    #assigned in global scope
    cache.dir <<- tempdir() 
    cache.size <<- 2048 * 1024^2
    
    memoised_func <- memoise::memoise(func, 
                                    cache = cachem::cache_disk(
                                                    dir = cache.dir,
                                                    max_size = cache.size))
    
    cache.percent.full <- function() {
        used.mem <- file.size(dir(cache.dir, full.names = TRUE)) %>% 
                        sum() 
        return(used.mem/cache.size * 100)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-06
      • 2019-04-21
      • 2017-12-14
      • 2012-02-20
      • 1970-01-01
      • 2018-05-08
      • 2021-12-31
      • 2017-03-24
      相关资源
      最近更新 更多