Page 1 of 1

Memory Leak?

Posted: Wed Jan 14, 2015 1:40 pm
by FlashFire
So I'm working on a 2D mining game. My issue though is that every second the game takes up about .1-.2 more MB of data each second, even if the game is just sitting their doing nothing. I'm running Garbage Collector each frame to see if that would solve it, but it still increases.(Without Garbage Collector, it increases then drops down to a higher number then originally).

I can post the code, but it's about 3k lines.

I mostly wanted to know if it's my coding(And common mistakes that create this?), the LOVE2D engine, or Lua.

Re: Memory Leak?

Posted: Wed Jan 14, 2015 1:54 pm
by Robin
We don't know anything until you post your code (please do it as a .love, not as text inside a post).

Re: Memory Leak?

Posted: Wed Jan 14, 2015 2:37 pm
by s-ol
I can't imagine there being a garbage collection bug in luaJit like that, so I would guess you are allocating some "external resource" and not freeing it or keeping some lua tables around unwillingly.

Maybe you are inserting into a large local table but are accidentally cleaning up a global one so the elements accumulate (or the other way around)?

Re: Memory Leak?

Posted: Wed Jan 14, 2015 2:40 pm
by FlashFire
There is the link, It's long and probably hard to understand though. Also has a few bugs.


https://www.dropbox.com/s/76it2pddqsoza ... .love?dl=0

Re: Memory Leak?

Posted: Wed Jan 14, 2015 9:06 pm
by szensk
It's not an issue with Love.

The garbage is coming from the backpack.draw function. It looks like you keep adding buttons forever. I end up with about 2000 buttons in backpack.buttons.list in a few seconds. You need to remove them from the list as you add more. Comment it out on line 188 of main.lua to see for yourself.

This is the shortest possible (albeit less than ideal) fix:

Code: Select all

function backpack.draw()
    backpack.buttons.list = {}
	for x = 0,9 do
		if backpack.selectedId == x+1 then
			love.graphics.draw(images.hotBarBox,screenSize.X/2-272+(54*x),screenSize.Y-67,0,1,1)
		end
		love.graphics.draw(images.backpackBox,screenSize.X/2-270+(54*x),screenSize.Y-65,0,1,1)
		if backpack.hotBar[x+1].block ~= nil then
			local subSize = data.itemInfo[backpack.hotBar[x+1].block].size or Vector2.new(2,2)
			local subSize = math.max(subSize.X,subSize.Y)/2
			love.graphics.draw(images[backpack.hotBar[x+1].block],screenSize.X/2-267+(54*x),screenSize.Y-65+3,0,.46/subSize,.46/subSize)
		end
		backpack.buttons.addButton(Vector2.new(screenSize.X/2-276+(54*x),screenSize.Y-65+4,0),Vector2.new(.46*100,.46*100),{part = 'hotBar',index = x+1})
	end

	if backpack.show then
		love.graphics.setColor(25,25,25,210)
		love.graphics.rectangle('fill',backpack.UIOFFSET.X-6,backpack.UIOFFSET.Y-6,backpack.UISIZE.X*108*10+12,backpack.UISIZE.Y*108*3+12)
		love.graphics.setColor(255,255,255)
		--love.graphics.draw(images.backpackBackground,screenSize.X/2-490,screenSize.Y/2-308)
		for x = 0,9 do
			for y = 0,2 do
				local size = backpack.UISIZE
				love.graphics.draw(images.backpackBox,backpack.UIOFFSET.X+(size.X*108*x),backpack.UIOFFSET.Y+(size.Y*108*y),0,2*size.X,2*size.Y)
				local index = x+1+(y*10)
				if backpack.items[index].block ~= nil then
					local subSize = data.itemInfo[backpack.items[index].block].size or Vector2.new(2,2)
					local subSize = math.max(subSize.X,subSize.Y)/2
					love.graphics.draw(images[backpack.items[index].block],backpack.UIOFFSET.X+4+(size.X*108*x),backpack.UIOFFSET.Y+4+(size.Y*108*y),0,backpack.UISIZE.X*.96/subSize,backpack.UISIZE.Y*.96/subSize)
					love.graphics.printf(backpack.items[index].amount,backpack.UIOFFSET.X+4+(size.X*108*x),backpack.UIOFFSET.Y+4+(size.Y*108*(y+1))-28,backpack.UISIZE.X*.96/subSize*100,'right')
				end
				backpack.buttons.addButton(Vector2.new(backpack.UIOFFSET.X+4+(size.X*108*x),backpack.UIOFFSET.Y+4+(size.Y*108*y)),Vector2.new(backpack.UISIZE.X*.96*100,backpack.UISIZE.Y*.96*100),{part = 'backpack',index = index})
				--love.graphics.draw(images.backpackBox,screenSize.X/2-490+24+(108*x),screenSize.Y/2-308+24+(108*y),0,2,2)
			end
		end
		if backpack.mouse.block ~= nil then
			local subSize = data.itemInfo[backpack.mouse.block].size or Vector2.new(2,2)
			local subSize = math.max(subSize.X,subSize.Y)/2
			love.graphics.draw(images[backpack.mouse.block],mouse.X,mouse.Y,0,backpack.UISIZE.X*.96/subSize,backpack.UISIZE.Y*.96/subSize)
		end
	end
end
Using this the memory usage stays at 5MB. I hope this was helpful.

Re: Memory Leak?

Posted: Wed Jan 14, 2015 9:13 pm
by bartbes
I expect it's all the calls to addButton.

Re: Memory Leak?

Posted: Wed Jan 14, 2015 10:42 pm
by FlashFire
@szensk Thanks for the help, It would of taken me days to find that error... If I ever did find it.

Re: Memory Leak?

Posted: Thu Jan 15, 2015 9:05 am
by bartbes
In my defence, szensk amended his reply after I posted... :neko: