Fixing Memory Leak

Hey, I’ve created a game and optimized the saving & other aspects so it works efficiently but the one thing I can’t figure out is what is causing a massive server side memory leak.

https://gyazo.com/afcdee1e26326a284b2e81d16a0833e4

As you can see, “Internal/DataModelGenericJob” progressively goes up when a server is created (will start low) but after a server is up for a few hours, it makes it rather laggy. I tried deleting everything in the game one at a time in studio to see if it’ll stop rising but nothing seems to stop it. I’m not sure what DataModelGenericJob refers to but if anyone can help, it’ll be greatly appreciated! thanks.

2 Likes

Do you keep things in memory which you don’t need?
i.e do you end up leaking instances due to connections sticking around (using :remove() instead of :Destroy() ) or keep instances in a table after needed

Can you show us some of your scripts so we can help you diagnose the issue?

1 Like

I haven’t used :remove() anywhere, I always destroy and I tend to reset tables after each use, e.g.

while wait(updateLeaderboardInterval) do 	
pcall(function()
	local topPlayerTable = {}
	local topPlayers = killsData:GetSortedAsync(false, 10)
	local data = topPlayers:GetCurrentPage()
	for i,v in ipairs(data) do
		local newTab = {
				v.key,
				v.value
		}
		table.insert(topPlayerTable, newTab)
	end
	for i,v in pairs(topPlayerTable) do
		leaderBoard2[i].StatName.Text = tostring(v[1])
		leaderBoard2[i].StatExp.Text = tostring(v[2])
	end		
end)
    end

I assume this isn’t your whole game?
Anything in your game could be causing a memory leak.

Do you destroy all unnecessary instances? I’ve forgot about this a few times myself.

Yeah, there’s a tonne of code that’s why I’m wondering what specifically “DataModelGenericJob” would be referring to. I have looked through a few times but do you have any examples of stuff I might not have thought of? this is my first major game and I might have overlooked something that I might not realise creates a leak. And do you think the only thing that would cause a memory leak is server-side tables/instances created that aren’t cleared up or is there anything else that can cause it?

Thanks :slight_smile:

If its due to the DataModelGenericJob, I would assume it has to somehow with Instances. Instances will remain in memory as long as there is anything with a direct reference.

If you need to hold references to instances, but still want them to be removed from memory when unneeded then consider using weak tables.

Do you have in any place where instances might build up?

Example: My game would build up a lot of ObjectValues under one a folder because I never deleted them.

2 Likes

Read through that, makes sense that when referencing something, you need to manually disconnect it otherwise it’ll stick around forever.

There are a few places where instances could build up so I will go through my code again and see if I can find anything with this new info, thanks.