Multiple large images crashes Codea

I’m having an issue with images. I recently updated a lot of the art for my game, and now a handful of the images are fairly large.

Now Codea crashes after running the project for a bit, but I don’t understand why.

I made sure none of the images were larger than the maximum allowed size (the largest ones are 1920x1336) and they shouldn’t take too much memory as they add up to ~450KB.

Does anyone know why this would cause crashes?

@JakAttak How many images are loaded at the same time. I can load a bunch of 1920x1336 to see how many will load before I start crashing.

@JakAttak I can create and display 8 1920x1336 images. When I create the 9th one, Codea crashes. This is on a 16gb iPad Air.

An image that size will take up 1920 * 1336 * 4 bytes each. That’s about 10 MB per image.

@JakAttak Even if I call collectgarbage() before I create a new image, Codea still crashes when I create the 9th image. I was watching the memory usage before I added the collectgarbage and it got as high as 1.34 mb with 8 images. After I added the collectgarbage before I created a new image, the memory used stayed below .4 mb, but still crashed on the 9 th image. So I’m not sure why Codea crashed. It wasn’t because of too much memory being used.

@Simeon, interesting. I just got the size from on my computer, they show as ~1MB for the large ones.

@dave1707, yes I monitor memory usage in my app and it hovers in the range of 0.8MB - 1.2MB, with maximums at 1.9MB. Loading 12 images at app start, 5 of which are 1920x1336, the rest are small 100x100 or 50x50

@dave1707 can you post the sample code you’re describing? I wonder if the image data is sticking around in the GPU texture memory, and so the iPad is still running out of RAM.

@JakAttak images are decompressed when loaded into memory — they are only compressed while on the file system. There are certain types of texture formats which are compressed in memory (e.g. PVRTC) but they have their own downsides.

@Simeon, gotcha. still, 10MB x 4 shouldn’t fill up the iPad (which I believe has 0.8GB of RAM available to apps?)

@JakAttak that can depend on what else is running, I’ll look into it though.

I had the same issue in another thread. Crashes after awhile. I switched to saving it in meshes and it seems to hold up well. Wonder if that is the issue for u.

@Simeon Here’s the code that crashes when 9 images are created. Tap screen to create images. I also included simple code that shows memory usage increase just running simple program.

Simple code showing min, curr, max memory used.

function setup()
    parameter.watch("min")
    parameter.watch("mem")
    parameter.watch("max")
    min=9999999
    max=0
end

function draw()
    background(40, 40, 50)
    mem=collectgarbage("count")*1024
    min=math.min(min,mem)
    max=math.max(max,mem)
end

Code to create images 1920x1336. Tap screen to create images.

displayMode(FULLSCREEN)

function setup()
    rectMode(CORNER)
    spriteMode(CORNER)
    tab={}
    min=9999999
    max=0
end

function draw()
    background(40, 40, 50)
    for a,b in pairs(tab) do
        sprite(b,0,0)
    end
    mem=collectgarbage("count")*1024
    if mem<min then
        min=mem
    end
    if mem>max then
        max=mem
    end
    text("tap screen to create another image",WIDTH/2,HEIGHT-50)
    text("min   "..min,WIDTH/2,HEIGHT-100)
    text("curr  "..mem ,WIDTH/2,HEIGHT-125)
    text("max   "..max,WIDTH/2,HEIGHT-150)
end

function touched(t)
    if t.state==BEGAN then
        img=image(1920,1336)
        setContext(img)
        fill(255,0,0)
        rect(0,0,1920,1336)
        fill(255)
        text((#tab+1).."  images created",WIDTH/2,HEIGHT/2)
        setContext()
        table.insert(tab,img)
    end
end

In the mean time, I’ve toned the images down to about fit the width,height values (1103 x 768), though I fear this won’t look near as nice on a retina screen (I can’t test, my iPad Mini has terrible resolution).

Does anyone with a retina iPad have any guidance on how big an image should be if it will fill the screen and look nice and crisp?

@JakAttak A retina iPad’s screen resolution is 2048x1536. I would recommend using vector arts, so they can scale to any resolution and still look sharp.

@SkyTheCoder, my images are PDFs, so it should be good?

@JakAttak PDFs are indeed vector graphics, so they should be good. You might want to use them through readImage so Codea doesn’t have to rasterize it every time you draw it.

is there a function to release memory or release other app’s memory?

@firewolf collectgarbage()