Memory Leak?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
FlashFire
Prole
Posts: 20
Joined: Thu Sep 11, 2014 12:44 am

Memory Leak?

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Memory Leak?

Post by Robin »

We don't know anything until you post your code (please do it as a .love, not as text inside a post).
Help us help you: attach a .love.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Memory Leak?

Post 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)?

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
FlashFire
Prole
Posts: 20
Joined: Thu Sep 11, 2014 12:44 am

Re: Memory Leak?

Post 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
szensk
Party member
Posts: 155
Joined: Sat Jan 19, 2013 3:57 am

Re: Memory Leak?

Post 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.
Last edited by szensk on Wed Jan 14, 2015 9:15 pm, edited 2 times in total.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Memory Leak?

Post by bartbes »

I expect it's all the calls to addButton.
FlashFire
Prole
Posts: 20
Joined: Thu Sep 11, 2014 12:44 am

Re: Memory Leak?

Post by FlashFire »

@szensk Thanks for the help, It would of taken me days to find that error... If I ever did find it.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Memory Leak?

Post by bartbes »

In my defence, szensk amended his reply after I posted... :neko:
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 93 guests