mapgl icon indicating copy to clipboard operation
mapgl copied to clipboard

Memory Leak Issue with maplibre_proxy, clear_layer(), and clear_legend()

Open JMTocho opened this issue 10 months ago • 4 comments

Hi! this package it's great! thanks!

Im having a memory leak in a shinyapp when using maplibre_proxy along with the functions clear_layer() and clear_legend(). Every time the user applies different filters, RAM usage increases. Initially, this is not a problem, but as more queries are made, memory consumption grows continuously until the application crashes. Here a small example(mine works with more data):

library(shiny)
library(bslib)
library(colourpicker)
library(dplyr)
library(sf)
library(shinyWidgets)
library(mapgl)

nc <- st_read(system.file("shape/nc.shp", package = "sf"),
              quiet = TRUE)

ui <- bootstrapPage(
  pickerInput(inputId = 'var',
              label = 'Ver',
              choices = c("AREA","PERIMETER"),
              options = list(`style` = "btn-info")),
  maplibreOutput("map")
)



server <- function(input, output, session) {
  
  
  output$map <- renderMaplibre({
    
    
    maplibre() |>
      fit_bounds(nc, animate = FALSE)|>
      add_fill_layer(
        id = "polygon_layer",
        source = nc,
        fill_color = "blue",
        fill_opacity = 0.5,
        tooltip = "AREA"
      )
  })
  
  

    observe({
    df2 <- nc |> 
    dplyr::select(input$var)


    
    col_indicador <- colnames(df2)[1]
    minimo <- min(df2[[1]], na.rm = TRUE)
    maximo <- max(df2[[1]], na.rm = TRUE)
    
    valores <- seq(minimo, maximo, length.out = 5)
    cols <- c("#8B2500", "#EE4000", "#9AFF9A", "#3B8C4D","#09622A")
      

    
    maplibre_proxy("map") |>
      clear_layer("polygon_layer")|>
      clear_legend()|>
      add_fill_layer(id = "polygon_layer",
                     source = df2,
                     fill_outline_color = "black",
                     fill_color = interpolate(
                       column = col_indicador,
                       property = NULL,
                       values = valores,
                       stops = cols,
                       na_color = "black"
                     ),
                     fill_opacity = 0.7,
                     hover_options = list(
                       fill_color = "yellow",
                       fill_opacity = 1
                     ))|>
      add_legend(legend_title = col_indicador, colors = cols,
                 values = valores, position = "top-right",
                 type = "categorical")

})
}

shinyApp(ui, `server)`

Am I using maplibre_proxy, clear_layer(), and clear_legend() correctly? How can I properly release memory to prevent crashes? Is my logic correct to prevent RAM usage from growing indefinitely? I am testing this with profiling—what should I look for to diagnose the issue?

Mapgl version : 0.2.0

JMTocho avatar Feb 14 '25 17:02 JMTocho

I also tried with eventReactive and Observevent but same result...

JMTocho avatar Feb 14 '25 17:02 JMTocho

I think this may now be fixed in my latest PR? Would you mind giving it a try?

walkerke avatar May 16 '25 17:05 walkerke

I think this may now be fixed in my latest PR? Would you mind giving it a try?

Ok. I tried with the example and my app and no...I have the same result.

JMTocho avatar May 16 '25 22:05 JMTocho

Hmm. Unfortunately, I can't reproduce this - I'm not seeing memory grow indefinitely even when I stress-test it.

walkerke avatar May 16 '25 23:05 walkerke