Page 1 of 1
Garbage Collection not working on ImageData?
Posted: Thu Dec 13, 2012 1:59 pm
by Mathias
I was wondering, why does
Code: Select all
function love.update(dt)
test = love.image.newImageData("test.jpg")
end
leak memory? I know its no useful code. In my real code I load the images in a thread and only when needed, but it also leaks memory and this is the smallest code I managed to show the problem.
As far as I understand, since no reference is kept to each ImageData, they should be collected immediately. Is there a way to free memory explicitly in lua/löve?
Kinda sounds like a pretty stupid question, but I didn't find a solution to this...
Re: Garbage Collection not working on ImageData?
Posted: Thu Dec 13, 2012 2:53 pm
by Robin
Why it leaks memory: it loads the same memory once every frame, which takes up a lot of memory and the garbage collector just can't keep up: it has to run each frame, but it will still be behind.
As for a solution:
Code: Select all
test = love.image.newImageData("test.jpg")
function love.update(dt)
end
Re: Garbage Collection not working on ImageData?
Posted: Thu Dec 13, 2012 3:31 pm
by Mathias
Then why does
Code: Select all
function love.update(dt)
test = love.image.newImageData("test.jpg")
love.timer.sleep(1)
end
still leak memory? (although much slower of course)
As I wrote, my real code is more sane then that. I want to load a huge bunch of images, but need each only for a few seconds but it seems I can't free the used memory...
Re: Garbage Collection not working on ImageData?
Posted: Thu Dec 13, 2012 3:38 pm
by bartbes
Because where in love.timer.sleep would it garbage collect?
Re: Garbage Collection not working on ImageData?
Posted: Thu Dec 13, 2012 5:02 pm
by Mathias
I would have thought, the sleep gives the environment time to do the collection.
But if I replace the sleep with something similar it actually works. Nice! Thanks a lot!
Now my real question: How to do this in a thread?
All thread examples I found use something like
so as I understand it, there is never time to do garbage collection. Is there some Loop called repeatedly in threads as well?
Re: Garbage Collection not working on ImageData?
Posted: Thu Dec 13, 2012 6:02 pm
by bartbes
You are doing it oh so wrong. Please stop doing this. You should not, I repeat you should
not repeatedly be loading things, it is extremely inefficient in all ways imagineable.
Other than that, the garbage collection runs automatically, but does not completely cycles particularly fast, you can force it to do a cycle (
collectgarbage), but if you're doing sane things (see above), then you should not need to.
Re: Garbage Collection not working on ImageData?
Posted: Thu Dec 13, 2012 6:10 pm
by Mathias
I have 150 images I want to show in a kind of slideshow. I can't load all images in love.load, because they consume to much memory. Thats why I want to load them at runtime, preferably in a thread.
In my real code I don't load one image forever, but different ones at different times and I want the old ones to disappear.
collectgarbage seems to work. Thanks!
Re: Garbage Collection not working on ImageData?
Posted: Thu Dec 13, 2012 6:22 pm
by Roland_Yonaba
Well here is a
similar approach to the problem, using threads.
Re: Garbage Collection not working on ImageData?
Posted: Thu Dec 13, 2012 7:06 pm
by bartbes
Fair enough.
I think this would be a nice thing for coroutines..