is it necessary to "free" texture data / love.graphics.Image ?

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
trabitboy
Prole
Posts: 19
Joined: Sun May 12, 2019 2:09 pm

is it necessary to "free" texture data / love.graphics.Image ?

Post by trabitboy »

Hi!
In the context of a paint app,
I tend to create a lot of new textures / images,
each time I save the GPU canvas:

Code: Select all

--TODO REMOVE try disable display as workaround on android 
function saveCanvasToFrame(idx)

	disableDisplay=true

	love.graphics.setCanvas()
	fromGpu=cvs:newImageData()
	frames[idx].data=fromGpu
	frames[idx].pic=love.graphics.newImage(fromGpu)
	--pic is not referenced anymore but is it enough for it to be freed?
	
	disableDisplay=false
end
it's not clear to me if you need to call any kind of deallocation function for the texture data on the GPU to be released?
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: is it necessary to "free" texture data / love.graphics.Image ?

Post by raidho36 »

It will be freed automatically once Lua garbage-collects discarded items. You can manually release the associated memory, but it's not strictly necessary. Keep in mind that ImageData resides in RAM and Texture resides in VRAM.
User avatar
pgimeno
Party member
Posts: 3655
Joined: Sun Oct 18, 2015 2:58 pm

Re: is it necessary to "free" texture data / love.graphics.Image ?

Post by pgimeno »

I've found that in a similar situation, if I didn't force garbage collection, the memory grew and grew out of control until the memory was completely filled. My hypothesis is that, since Lua does not know the size of the underlying object, it doesn't care that there are many big ones waiting to be garbage-collected, so if their number doesn't reach a high enough threshold, GC doesn't happen. Since in the Lua side the memory used by UserData is very small, many objects can accumulate until GC is triggered.

Manually running GC every 5 seconds worked for me. https://notabug.org/pgimeno/Thrust-II-r ... e.lua#L557
User avatar
slime
Solid Snayke
Posts: 3161
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: is it necessary to "free" texture data / love.graphics.Image ?

Post by slime »

If that situation happens in code like the above where the lifetime of the texture is obvious, Object:release (new in LÖVE 11.0) is probably a better solution – it'll perform better, at least.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: is it necessary to "free" texture data / love.graphics.Image ?

Post by raidho36 »

pgimeno wrote: Tue Sep 17, 2019 11:24 pmSince in the Lua side the memory used by UserData is very small, many objects can accumulate until GC is triggered.
This implies that Lua would trigger GC sooner if it knew about object sizes, but that's not the case. Plain Lua objects that take up a lot of memory don't trigger GC any quicker than normal. GC only cares about reference counting.
trabitboy
Prole
Posts: 19
Joined: Sun May 12, 2019 2:09 pm

Re: is it necessary to "free" texture data / love.graphics.Image ?

Post by trabitboy »

thanks a lot,
I will add a call to release() explicitly when an Image ( or other gpu resource ) gets out of scope. :awesome:

maybe we should add a mention on the wiki about this ?
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: is it necessary to "free" texture data / love.graphics.Image ?

Post by raidho36 »

Wiki does tell about this. Maybe you missed it because you were focused on whatever you were looking for.
trabitboy
Prole
Posts: 19
Joined: Sun May 12, 2019 2:09 pm

Re: is it necessary to "free" texture data / love.graphics.Image ?

Post by trabitboy »

:release() exists on the wiki page of love.graphics.Image , but the particular context of objects having allocated gpu memory, and the preventive use of release() in this context, is not explained.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: is it necessary to "free" texture data / love.graphics.Image ?

Post by zorg »

trabitboy wrote: Thu Sep 19, 2019 2:26 pm :release() exists on the wiki page of love.graphics.Image , but the particular context of objects having allocated gpu memory, and the preventive use of release() in this context, is not explained.
https://love2d.org/wiki/Object:release
It's defined for all Object types, even though it isn't explained what will happen to specific memory areas whether in the gpu or in main RAM; It does say that the lua reference will be cleaned up and if that was the only one it had, the object itself will be completely deleted, so one could infer that to mean freeing up all areas of memory.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
m0nkeybl1tz
Prole
Posts: 14
Joined: Fri Jan 22, 2016 3:25 am

Re: is it necessary to "free" texture data / love.graphics.Image ?

Post by m0nkeybl1tz »

This thread saved my life! I'm drawing to and sampling from a canvas every frame, and I was having my framerate drop in half every few seconds. Calling Object:release() on my ImageData stopped it from spiking, as well as saved me 5 FPS overall.
Post Reply

Who is online

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