How can I release memory that an image takes?

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
shimatsukaze
Prole
Posts: 10
Joined: Fri Oct 07, 2016 7:51 am

How can I release memory that an image takes?

Post by shimatsukaze »

If I use

Code: Select all

love.graphics.newImage("asset/logo.png")
how can I release the memory after this image is no longer used?
I have to load lots of images, and I can't load them all at a time, as they are too large.
However, only about ten images will appear at one scene, so I wonder if I could release the memory after every scene and load new ones?
I mean, is there a way to load images before every scene and destroy them afterwards so as to save memory?
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: How can I release memory that an image takes?

Post by Roland_Yonaba »

Normally Lua, takes care of it. I mean the garbage collector does.
You can help him by assigning nil to the reference which points to the image loaded. I mean, when the asset loaded is no longer used, assign nil to it. In that way, you are tagging this element so that the gabage collector, when it will run, will collect this asset and release it from memory, in case there are no longer any other valid reference to this object.
Of course, instead of waiting for the garbage collector to run, you can call it via collectgarbage(). But really, this is not really necessary in most practical cases.
shimatsukaze
Prole
Posts: 10
Joined: Fri Oct 07, 2016 7:51 am

Re: How can I release memory that an image takes?

Post by shimatsukaze »

Roland_Yonaba wrote:Normally Lua, takes care of it. I mean the garbage collector does.
You can help him by assigning nil to the reference which points to the image loaded. I mean, when the asset loaded is no longer used, assign nil to it. In that way, you are tagging this element so that the gabage collector, when it will run, will collect this asset and release it from memory, in case there are no longer any other valid reference to this object.
Of course, instead of waiting for the garbage collector to run, you can call it via collectgarbage(). But really, this is not really necessary in most practical cases.
Thank you, it works.
I didn't learn about garbage recycling in Lua; I thought I had to release it manually.
but if I don't run "collectgarbage()", the memory use will even increase to 1GB even if I assign nil to the object. Maybe because I was loading too many images while testing.
Whatever, "collectgarbage()" really helps me a lot. Thank you again.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: How can I release memory that an image takes?

Post by pgimeno »

I've noticed that automatic garbage collection is a bit lazy, or maybe just slow, especially when there's high CPU consumption. If you generate a lot of garbage per frame (e.g. creating many tables on the fly that you pass as arguments to functions, or creating many temporary objects), it's advisable to run collectgarbage() in your love.update, to prevent stuttering.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests