Memory leaks with images

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
dotPoozer
Prole
Posts: 2
Joined: Sun Feb 23, 2014 10:24 pm

Memory leaks with images

Post by dotPoozer »

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.
Attachments
game.love
(2.18 MiB) Downloaded 74 times
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: Memory leaks with images

Post by Helvecta »

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:

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
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":

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
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! :D
dotPoozer
Prole
Posts: 2
Joined: Sun Feb 23, 2014 10:24 pm

Re: Memory leaks with images

Post by dotPoozer »

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 ;]
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest