# Out of memory when using Makie with large images

**URL:** <https://discourse.julialang.org/t/out-of-memory-when-using-makie-with-large-images/103786>\
**Category:** Visualization\
**Tags:** makie, cairomakie\
**Created:** [September 12, 2023, 12:41pm UTC](https://discourse.julialang.org/t/out-of-memory-when-using-makie-with-large-images/103786 "2023-09-12T12:41:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![klafyvel](https://avatars.discourse-cdn.com/v4/letter/k/a587f6/32.png) [@klafyvel](https://discourse.julialang.org/u/klafyvel)\
**Post date:** [September 12, 2023, 12:41pm UTC](https://discourse.julialang.org/t/out-of-memory-when-using-makie-with-large-images/103786/1 "2023-09-12T12:41:35Z")

</div>

Hi,  
I’m having a problem with a big memory footprint when using CairoMakie to plot big images in the REPL. The code consists of loading four images with `load` and then plotting them using `image!` (no treatment whatsoever, so I did not include the code, but I can if necessary). If I run the plot code (with the images already loaded) once, it runs fine. But as I run it more times (say, for example, because I’m tweaking some attributes such as `xlabel`), julia eventually runs out of memory. I noticed a big memory footprint from my axes (1.4GB each!), although the output of `varinfos()` remains constant. Is there a kind of draft mode to reduce this footprint? (I know I could use GLMakie and tweak using Observables, but I find it more convenient to edit a vim buffer and execute its content with vim-slime).

Output of `varinfo()` for reference:

```julia
 julia --project
julia> varinfo()
  name size summary
  –––––––––––––––– ––––––––––– –––––––
  Base Module 
  Core Module 
  InteractiveUtils 519.661 KiB Module 
  Main Module 

# Loading images and modules...

julia> varinfo()
  name size summary                                                                        
  –––––––––––––––– –––––––––– –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  Base Module                                                                         
  Core Module                                                                         
  InteractiveUtils 1.184 MiB Module                                                                         
  Main Module                                                                         
  ans 2.095 KiB Markdown.MD                                                                    
  img_100nm 48.000 MiB 4096×4096 Array{RGB{N0f8},2} with eltype ColorTypes.RGB{FixedPointNumbers.N0f8}
  img_10nm 1.286 MiB 605×557 Array{RGBA{N0f8},2} with eltype ColorTypes.RGBA{FixedPointNumbers.N0f8}
  img_1um 48.000 MiB 4096×4096 Array{RGB{N0f8},2} with eltype ColorTypes.RGB{FixedPointNumbers.N0f8}
  img_pvktoa 48.000 MiB 4096×4096 Array{RGB{N0f8},2} with eltype ColorTypes.RGB{FixedPointNumbers.N0f8}

julia> 

# plotting once...

julia> varinfo()
  name size summary                                                                        
  –––––––––––––––– –––––––––– –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  Base Module                                                                         
  Core Module                                                                         
  InteractiveUtils 1.189 MiB Module                                                                         
  Main Module                                                                         
  ans 1.462 GiB Figure                                                                         
  ax_100nm 1.462 GiB Axis                                                                           
  ax_10nm 1.462 GiB Axis                                                                           
  ax_1um 1.462 GiB Axis                                                                           
  ax_toa 1.462 GiB Axis                                                                           
  f 1.462 GiB Figure                                                                         
  img_100nm 48.000 MiB 4096×4096 Array{RGB{N0f8},2} with eltype ColorTypes.RGB{FixedPointNumbers.N0f8}
  img_10nm 1.286 MiB 605×557 Array{RGBA{N0f8},2} with eltype ColorTypes.RGBA{FixedPointNumbers.N0f8}
  img_1um 48.000 MiB 4096×4096 Array{RGB{N0f8},2} with eltype ColorTypes.RGB{FixedPointNumbers.N0f8}
  img_pvktoa 48.000 MiB 4096×4096 Array{RGB{N0f8},2} with eltype ColorTypes.RGB{FixedPointNumbers.N0f8}

```

---

<div class="post-metadata">

**Author:** ![klafyvel](https://avatars.discourse-cdn.com/v4/letter/k/a587f6/32.png) [@klafyvel](https://discourse.julialang.org/u/klafyvel)\
**Post date:** [September 12, 2023, 1:10pm UTC](https://discourse.julialang.org/t/out-of-memory-when-using-makie-with-large-images/103786/2 "2023-09-12T13:10:21Z")

</div>

After some investigation, it seems to be an issue with the internal type used by Makie (or Cairo?). The following test code (using fixed point numbers for the images):

```julia
using CairoMakie, Colors, FixedPointNumbers

CairoMakie.activate!(px_per_unit=3)

imgs = [rand(RGB{N0f8}, 4096, 4096) for _ in 1:4]

varinfo()

f = Figure()

axes = [Axis(f[i, j]) for i in 1:2, j in 1:2]

map(t->image!(t...), zip(axes, imgs))

varinfo()

```

yields

```julia
julia> varinfo()
  name size summary                            
  –––––––––––––––– ––––––––––– –––––––––––––––––––––––––––––––––––
  Base Module                             
  Core Module                             
  InteractiveUtils 866.682 KiB Module                             
  Main Module                             
  ans 192.000 MiB 4-element Vector{Matrix{RGB{N0f8}}}
  imgs 192.000 MiB 4-element Vector{Matrix{RGB{N0f8}}}

#plotting ...

julia> varinfo()
  name size summary                                                                                                                                             
  –––––––––––––––– ––––––––––– ––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  Base Module                                                                                                                                              
  Core Module                                                                                                                                              
  InteractiveUtils 871.404 KiB Module                                                                                                                                              
  Main Module                                                                                                                                              
  ans 1.955 GiB 4-element Vector{Image{Tuple{StepRangeLen{Float32, Float64, Float64, Int64}, StepRangeLen{Float32, Float64, Float64, Int64}, Matrix{RGB{Float32}}}}}
  axes 1.955 GiB 2×2 Matrix{Axis}                                                                                                                                    
  f 1.955 GiB Figure                                                                                                                                              
  imgs 192.000 MiB 4-element Vector{Matrix{RGB{N0f8}}}                                                                                                                 

```

while if I use `RGBf` instead of `RGB{N0f8}` I get:

```julia
julia> varinfo()
  name size summary                                
  –––––––––––––––– ––––––––––– –––––––––––––––––––––––––––––––––––––––
  Base Module                                 
  Core Module                                 
  InteractiveUtils 866.635 KiB Module                                 
  Main Module                                 
  ans 1.000 GiB 4-element Vector{Matrix{RGBA{Float32}}}
  imgs 1.000 GiB 4-element Vector{Matrix{RGBA{Float32}}}

# Plotting ...

julia> varinfo()
  name size summary                                                                                                                                              
  –––––––––––––––– ––––––––––– –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  Base Module                                                                                                                                               
  Core Module                                                                                                                                               
  InteractiveUtils 871.311 KiB Module                                                                                                                                               
  Main Module                                                                                                                                               
  ans 1.018 GiB 4-element Vector{Image{Tuple{StepRangeLen{Float32, Float64, Float64, Int64}, StepRangeLen{Float32, Float64, Float64, Int64}, Matrix{RGBA{Float32}}}}}
  axes 1.018 GiB 2×2 Matrix{Axis}                                                                                                                                     
  f 1.018 GiB Figure                                                                                                                                               
  imgs 1.000 GiB 4-element Vector{Matrix{RGBA{Float32}}}                                                                                                              

```

Does Makie internally convert to `RGBAf` or something similar?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [September 12, 2023, 8:02pm UTC](https://discourse.julialang.org/t/out-of-memory-when-using-makie-with-large-images/103786/3 "2023-09-12T20:02:07Z")

</div>

Makie does convert to RGBAf but still it seems weird that so much more memory is taken up than your actual images. I’d have to take a look where this happens, maybe something isn’t gc’ed correctly.
