Garbage Collection not working on ImageData?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Mathias
Prole
Posts: 5
Joined: Thu Dec 13, 2012 12:48 pm

Garbage Collection not working on ImageData?

Post 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...
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Garbage Collection not working on ImageData?

Post 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
Help us help you: attach a .love.
Mathias
Prole
Posts: 5
Joined: Thu Dec 13, 2012 12:48 pm

Re: Garbage Collection not working on ImageData?

Post 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...
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Garbage Collection not working on ImageData?

Post by bartbes »

Because where in love.timer.sleep would it garbage collect?
Mathias
Prole
Posts: 5
Joined: Thu Dec 13, 2012 12:48 pm

Re: Garbage Collection not working on ImageData?

Post 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

Code: Select all

while active do update() end
so as I understand it, there is never time to do garbage collection. Is there some Loop called repeatedly in threads as well?
Last edited by Mathias on Thu Dec 13, 2012 6:04 pm, edited 2 times in total.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Garbage Collection not working on ImageData?

Post 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.
Mathias
Prole
Posts: 5
Joined: Thu Dec 13, 2012 12:48 pm

Re: Garbage Collection not working on ImageData?

Post 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!
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Garbage Collection not working on ImageData?

Post by Roland_Yonaba »

Well here is a similar approach to the problem, using threads.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Garbage Collection not working on ImageData?

Post by bartbes »

Fair enough.

I think this would be a nice thing for coroutines..
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Semrush [Bot] and 3 guests