Steve Rowley
2025-Sep-04 21:16 UTC
[R] Processing repeated images and memory recovery by GC
I have a question about repeated image processing and recovering memory via garbage collection. A couple images are displayed simultaneously for analysis, using split.screen(). I'm looping over a large collection of images, using jpeg/readJPEG(), png/readPNG(), and caTools/read.gif() to load an image into memory from a file. Then I use erase.screen() to erase the previous image, use plot() to set up a plotting area, and display the image with rasterImage(). Then the user elects some processing, and we move on to the next image(s). I'm very carefully not holding references to any of the previous images. GC is indeed happening, but memory quickly fills up. It certainly *looks* as though the image memory is not being released: printing out trace info using mem_used()[[1]] says memory use increases with each image, by about the image size. Forcing periodic GC's, measuring memory used before and after, shows that no memory is recovered. Some questions: (1) Is it known that processing images in this way will lead to allocating memory that is not recovered by GC? (2) Are there any tools you'd recommend to see exactly *what* is filling up memory, just in case it's not previous images, but some other problem? This is R 4.4.2 (for now). Running MacOS on an ARM processor. ___________ Steve Rowley <sgr at alum.mit.edu> Zoom: 839 529 4589 <https://us04web.zoom.us/j/8395294589?pwd=dlQ4MUFHK1NFOCtoZFpUNFRtZ2lSQT09> It is very dark & after 2000. If you continue, you are likely to be eaten by a bleen. [[alternative HTML version deleted]]
Bert Gunter
2025-Sep-04 22:01 UTC
[R] Processing repeated images and memory recovery by GC
As I understand it and you, this kind of specialized technical question is usually not a good fit to this list about general issues in R programming, though someone may respond here of course. Alternatively, you may find it useful to post on one (or some?) of the R SIGS (Special Interest Groups) listed at https://www.r-project.org/mail.html to see if one of them might be a better fit. (Though some listed there may be inactive). Note also that some R packages dealing with image processing have their own dedicated lists. Happy hunting. Cheers, Bert On Thu, Sep 4, 2025 at 2:17?PM Steve Rowley via R-help <r-help at r-project.org> wrote:> I have a question about repeated image processing and recovering memory via > garbage collection. > > A couple images are displayed simultaneously for analysis, using > split.screen(). I'm looping over a large collection of images, using > jpeg/readJPEG(), png/readPNG(), and caTools/read.gif() to load an image > into memory from a file. Then I use erase.screen() to erase the previous > image, use plot() to set up a plotting area, and display the image with > rasterImage(). Then the user elects some processing, and we move on to the > next image(s). > > I'm very carefully not holding references to any of the previous images. > GC is indeed happening, but memory quickly fills up. It certainly *looks* > as though the image memory is not being released: printing out trace info > using mem_used()[[1]] says memory use increases with each image, by about > the image size. Forcing periodic GC's, measuring memory used before and > after, shows that no memory is recovered. > > Some questions: > > (1) Is it known that processing images in this way will lead to allocating > memory that is not recovered by GC? > > (2) Are there any tools you'd recommend to see exactly *what* is filling up > memory, just in case it's not previous images, but some other problem? > > This is R 4.4.2 (for now). Running MacOS on an ARM processor. > ___________ > Steve Rowley <sgr at alum.mit.edu> Zoom: 839 529 4589 > <https://us04web.zoom.us/j/8395294589?pwd=dlQ4MUFHK1NFOCtoZFpUNFRtZ2lSQT09 > > > It is very dark & after 2000. If you continue, you are likely to be eaten > by a bleen. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Ivan Krylov
2025-Sep-05 09:52 UTC
[R] Processing repeated images and memory recovery by GC
? Thu, 4 Sep 2025 17:16:15 -0400 Steve Rowley via R-help <r-help at r-project.org> ?????:> Then I use erase.screen() to erase the previous > image, use plot() to set up a plotting area, and display the image > with rasterImage().erase.screen() works by drawing a background-coloured rectangle over the plot, so the previous images remain in the display list despite being completely over-painted: dev.new(bg='white') split.screen(c(1,2)) # [1] 1 2 recordPlot() |> object.size() # 57584 bytes for (i in 1:2) { screen(i, FALSE) plot(0:1, 0:1, type = 'n') rasterImage(as.raster(matrix(runif(2^20), 2^10)), 0, 0, 1, 1) } recordPlot() |> object.size() |> print(units = 'Mb') # 16.2 Mb for (i in 1:2) { erase.screen(i) screen(i, FALSE) plot(0:1, 0:1, type = 'n') rasterImage(as.raster(matrix(runif(2^20), 2^10)), 0, 0, 1, 1) } recordPlot() |> object.size() |> print(units = 'Mb') # 32.3 Mb How much slower would it be to maintain your own "display list" of images and redraw the plot from scratch (e.g. using layout()) every time you change it? -- Best regards, Ivan