I'm not experienced with programming with love or/and lua, started doing my first project mostly for educational purposes.
When i checked my program with win task manager, every time I've been reloading images, they were taking more and more RAM. I got mostly no idea how could i repair it, because when I'm trying to add unload() method to scenes where I'd set image to nil, they aren't reloading properly, or doing nothing. Every left click taken ram increases by 20-30 MB. I guess it's because with every reload new instance of image is being created, but I may be wrong. I prefer to read what others have to say since I couldn't create exact question that google would answer me.
Memory leaks with images
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Memory leaks with images
- Attachments
-
- game.love
- (2.18 MiB) Downloaded 73 times
Re: Memory leaks with images
Looks like every time you change newState, it calls .load for its respective state. So for the menu, once you click to fight, the ingame state stuff loads (which is nothing), and when you click again it calls .load for menu again, creating a new reference to the image:
You can fix that by just calling the function once at the beginning (if you don't want to reload it over and over, but uses more memory as you get bigger):
Or you can take what you originally did one step further and use unload to remove these images and make it squeaky clean (less memory used, but you have to reload the image every time newState goes to "menu":
I recommend the former unless you make sure you're not removing functions (if you do that you'll have to redefine all the menu functions). I'd also recommend waiting on some more input for this issue, since I know there are people here that love to optimize their applications, and are probably rushing towards the forums, foaming at the mouth and seeking my blood for something I overlooked.
Er, either way, this is a start! Hope it helps!
P.S. Welcome to the forums!
Code: Select all
menu = {
backgroundImage,
Start,
Exit
}
function menu.load()
menu.backgoundImage = love.graphics.newImage("img/coloseum.png") --everytime I am called, I create a new userdata thingie!
menu.Start, menu.Exit = love.graphics.newImage("img/start.png"), love.graphics.newImage("img/exit.png")
end
You can fix that by just calling the function once at the beginning (if you don't want to reload it over and over, but uses more memory as you get bigger):
Code: Select all
menu = {
backgoundImage = love.graphics.newImage("img/coloseum.png"),
Start = love.graphics.newImage("img/start.png"),
Exit = love.graphics.newImage("img/exit.png")
}
function menu.load()
end
Code: Select all
function menu.unload()
for i, v in pairs(menu) do
if type(v) == "userdata" then
menu[i] = nil
end
end
collectgarbage()
end
Er, either way, this is a start! Hope it helps!
P.S. Welcome to the forums!
Re: Memory leaks with images
After I understand what's happening in unload example I'm probably gonna use this one ;] It seems to be more useful than calling everything at the beginning.
Thanks ;]
Thanks ;]
Who is online
Users browsing this forum: Google [Bot] and 5 guests