Page 1 of 1
Memory Management
Posted: Fri Jul 21, 2017 6:47 am
by Hydrolek99
Hello. I was wondering how would one go about removing an image/graphic from a Love Program? Like a background or sprite that is no longer needed in a game anymore.I really do not want to have my game take up a huge amount of memory due to images/Sprites that have no purpose. Would I be able to delete them if I were to put them in a table then remove them when they are no longer needed or is there another way of doing it?
After doing some looking around, I have found out about garbage collection and a Love.discard function.
would any of those help me out? If so how exactly?
Re: Memory Management
Posted: Fri Jul 21, 2017 6:52 am
by zorg
I never heard of love.discard myself, and i'm not sure if the lua collectgarbage function would free up space taken up by graphics, since those are on the C++ side; still, if you don't have any lua variable pointing to a resource, löve would eventually free those automatically, so this is not something you should worry about. (running collectgarbage after freeing stuff might work, but again, unless you're absolutely sure you need to do that, you probably shouldn't)
Re: Memory Management
Posted: Sat Jul 22, 2017 12:22 am
by CaptainMaelstrom
I'll second what zorg has said.
to create an image:
Code: Select all
image = love.graphics.newImage(filepath)
to delete
The actual memory will be freed up sooner or later (up to Lua's garbage collection implementation)
Re: Memory Management
Posted: Sat Jul 22, 2017 7:17 am
by Hydrolek99
Ok. That seems simple enough. Thank you. :-)
So all I would need to do to free up space is tell the garbage collection to do it somewhere in my code?
Re: Memory Management
Posted: Sat Jul 22, 2017 10:32 am
by zorg
Hydrolek99 wrote: ↑Sat Jul 22, 2017 7:17 am
Ok. That seems simple enough. Thank you. :-)
So all I would need to do to free up space is tell the garbage collection to do it somewhere in my code?
Yes, you can force it by calling collectgarbage, but even without that, the resources will be freed automatically, sooner or later.