如何让闪亮的记忆在会议结束后返回?

时间:2023-02-06 17:19:07

I have a shiny app that allows each user to select which data set to load. Everything in the app works wonderful, except for memory usage. After the session is ended and the user closes the web browser, Shiny does not give back the free memory to the machine it is running on. Eventually, after accessing it enough times, it runs out of memory.

我有一个闪亮的应用程序,允许每个用户选择加载哪个数据集。除了内存使用之外,应用程序中的所有东西都运行得很好。在会话结束并用户关闭web浏览器之后,闪亮不会将空闲内存返回给正在运行的机器。最终,在访问它足够多次之后,它将耗尽内存。

In traditional R, I often take care of this with frequent calls to gc() after removing data. However, that does not seem to work in my shiny app.

在传统的R中,我经常在删除数据之后频繁调用gc()来处理这个问题。然而,这似乎并不适用于我的闪亮应用。

Hours of googling has not rendered anything insightful. Is there a clean way of freeing up unused memory in this scenario?

数小时的谷歌搜索并没有带来任何深刻的见解。在这个场景中是否有一种干净的方式释放未使用的内存?

1 个解决方案

#1


2  

Maybe you can gc() under observe statement with invalidateLater? Also maybe you can limit each session to some memory threshold or some timeout if pissible? Below you can see how much memory you are taking for each session. Also look at your Task Manager in the Processes how much this process takes in (note: current example takes about 440Mb per session)

也许您可以使用invalidateLater在观察语句下使用gc() ?也可以将每个会话限制为内存阈值或超时(如果可能的话)?在下面,您可以看到每个会话占用了多少内存。还可以查看流程中的任务管理器(注意:当前示例每节大约需要440Mb)

rm(list = ls())
library(shiny)

cleanMem <- function(n=10) { for (i in 1:n) gc() }

runApp(list(
  ui = fluidPage(
    tableOutput('foo')
  ),
  server = function(input, output,session) {

    observe({
      # periodically collect
      invalidateLater(1000,session)
      cleanMem()
    })

    x1 <- 1:100000000
    x2 <- rbind(mtcars, mtcars)
    env <- environment()  # can use globalenv(), parent.frame(), etc
    output$foo <- renderTable({
      data.frame(
        object = ls(env),
        size = unlist(lapply(ls(env), function(x) {
          object.size(get(x, envir = env, inherits = FALSE))
        }))
      )
    })
  }
))

#1


2  

Maybe you can gc() under observe statement with invalidateLater? Also maybe you can limit each session to some memory threshold or some timeout if pissible? Below you can see how much memory you are taking for each session. Also look at your Task Manager in the Processes how much this process takes in (note: current example takes about 440Mb per session)

也许您可以使用invalidateLater在观察语句下使用gc() ?也可以将每个会话限制为内存阈值或超时(如果可能的话)?在下面,您可以看到每个会话占用了多少内存。还可以查看流程中的任务管理器(注意:当前示例每节大约需要440Mb)

rm(list = ls())
library(shiny)

cleanMem <- function(n=10) { for (i in 1:n) gc() }

runApp(list(
  ui = fluidPage(
    tableOutput('foo')
  ),
  server = function(input, output,session) {

    observe({
      # periodically collect
      invalidateLater(1000,session)
      cleanMem()
    })

    x1 <- 1:100000000
    x2 <- rbind(mtcars, mtcars)
    env <- environment()  # can use globalenv(), parent.frame(), etc
    output$foo <- renderTable({
      data.frame(
        object = ls(env),
        size = unlist(lapply(ls(env), function(x) {
          object.size(get(x, envir = env, inherits = FALSE))
        }))
      )
    })
  }
))