Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible memory leak #1253

Closed
wch opened this issue Jul 20, 2016 · 7 comments · Fixed by #1254
Closed

Possible memory leak #1253

wch opened this issue Jul 20, 2016 · 7 comments · Fixed by #1254

Comments

@wch
Copy link
Collaborator

wch commented Jul 20, 2016

If you run this app and hit the reload button over and over, the memory usage will keep increasing:

library(shiny)
library(pryr)

vals <- reactiveValues(x = 1)

ui <- fluidPage(
  verbatimTextOutput("memUsed")
)

server <- function(input, output, session) {
  # Allocate a 40MB object in this session
  data <- 1:1e7

  memUsed <- function() {
    paste(round(as.numeric(pryr::mem_used()) / 1024 / 1024), "MB used")
  }

  output$memUsed <- renderText({
    memUsed()
  })

  # When this is code is included in the app, memory used will increase with 
  # each reload. When it is commented out, the memory used will NOT increase on
  # each reload.
  valueObserver <- observeEvent(vals$x, {
    print('Reactive values observable event triggered')
  })

  session$onSessionEnded(function() {
    print('Session Ended')

    # Another way to prevent memory usage from growing is to uncomment this
    # isolate(vals$x <- runif(1))

    gc()
    print(memUsed())
  })
}

shinyApp(ui, server)

If the observeEvent is commented out, then memory usage will NOT increase with each reload.

Also, if vals$x is modified in the onSessionEnded function, then the memory will not increase with each reload.

I think the root of the problem is that vals$x keeps a reference to the observer which gets cleared only when vals$x changes.

@jcheng5
Copy link
Member

jcheng5 commented Jul 20, 2016

The observer does get destroyed, right? You don't see the "Reactive values
observable event triggered" stacking up?

On Wed, Jul 20, 2016 at 1:47 PM Winston Chang [email protected]
wrote:

If you run this app and hit the reload button over and over, the memory
usage will keep increasing:

library(shiny)
library(pryr)
vals <- reactiveValues(x = NULL)
ui <- fluidPage(
verbatimTextOutput("memUsed")
)
server <- function(input, output, session) {

Allocate a 40MB object in this session

data <- 1:1e7

memUsed <- function() {
paste(round(as.numeric(pryr::mem_used()) / 1024 / 1024), "MB used")
}

output$memUsed <- renderText({
memUsed()
})

When this is code is included in the app, memory used will increase with

each reload. When it is commented out, the memory used will NOT increase on

each reload.

valueObserver <- observeEvent(vals$x, {
print('Reactive values observable event triggered')
})

session$onSessionEnded(function() {
print('Session Ended')

# Another way to prevent memory usage from growing is to uncomment this
isolate(vals$x <- runif(1))

gc()
print(memUsed())

})
}

shinyApp(ui, server)

If the observeEvent is commented out, then memory usage will NOT increase
with each reload.

Also, if vals$x is modified in the onSessionEnded function, then the
memory will not increase with each reload.

I think the root of the problem is that vals$x keeps a reference to the
observer which gets cleared only when vals$x changes.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#1253, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAH6DzY1iZYui2Yx2NJS9ZCruOuGS6pdks5qXolTgaJpZM4JRMNk
.

@wch
Copy link
Collaborator Author

wch commented Jul 20, 2016

Oops, I forgot to comment out the isolate part. I'll edit the post.

@jcheng5
Copy link
Member

jcheng5 commented Jul 20, 2016

OK that was going to be my next question. So we actually never see that print message at all?

@wch
Copy link
Collaborator Author

wch commented Jul 20, 2016

I made another edit. :/ Try again.

@wch
Copy link
Collaborator Author

wch commented Jul 20, 2016

Whether or not it actually gets triggered doesn't seem to matter.

@jcheng5
Copy link
Member

jcheng5 commented Jul 20, 2016

Oh OK, now I get it. OK, there's an easy fix here. When the observeEvent's observer is destroyed during teardown, it's destroyed, but not invalidated. Invalidating it will free it for gc.

@jcheng5
Copy link
Member

jcheng5 commented Jul 20, 2016

Interestingly, we don't even bother saving a reference to the most-recent ctx object in the Observer R6 object. I guess we could...? And clear it out during invalidation?

@wch wch mentioned this issue Jul 21, 2016
jcheng5 pushed a commit that referenced this issue Jul 21, 2016
* When observer is destroyed, invalidate context. Closes #1253

* Update NEWS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants