Physics or Memory leak causes server to slow down?

I’ve recently been working on a new game, and have run into the issue where the server will be constantly slowed down, with 2-5 second lag spikes on top.

The lag issues stays manageable for about 20 minutes, then the server just… gives up.

Script runtime comes up to about 5% total at the max (very rare, most of the time 1% or lower), so I’m curious as to why the lag amounts to so much. I ran the server microprofiler and found this scattered about constantly, and I’d noticed the same problem in studio after leaving the game running for a while.

A few other notable things:

  • There are enemies that spawn and despawn overtime, also despawning when killed by a player. The total amount of them comes to about 30. Some of the lag spikes are attributed to this, but there aren’t many parts, and spawning/despawning barely takes any script power.
  • There are these Item Boxes that are also spawning and despawning. The server also lags when they appear/disappear, but less so than the enemies. Again, the spawning/despawning process barely takes script power.
  • I’m using a few different collision groups to enhance the gameplay. The item blocks, players, and the enemies all use their own collision groups.

Yet, no matter what, the game always slows down to a crawl consistently. This makes me wonder if it’s some sort of memory leak, yet I haven’t seen any script lag that would point to that. I’ve searched for touched events, ChildAdded, DescendantAdded, and more, and couldn’t find anything related to that, even at runtime.

Is there any way that I can hunt down and reduce this type of server lag, or perhaps get a more pinpoint on what’s directly causing it? It’s an AFK type game, so I’d prefer if servers could last 8+ hours before needing to restart them. Right now it becomes nearly unplayable after 2.

Have to checked for connections which are not disconnected? Do you have any tables containing reference to objects which later get removed, checking for these could help to track down memory leak.

1 Like

At this point I’ve narrowed it down to just the enemies, and their spawning and despawning.

I ran a test (which was just killing all the NPCs over and over and respawning them), and it gave the same results as the Playtest.

I looked through all the scripts related to the NPC, including spawning and despawning. There aren’t any other references that are stored, and I definitely don’t store connections in tables. I’m not quite sure which stats I should be looking at to see any other “leaks” that I could potentially fix.

I wasn’t asking whether you are storing connections in table. Something like this

local Time = {}
game.Players.PlayerAdded:Connect(function(Player)
 Time[Player] = os.clock()
end)

Now even if the player leaves the key Player would exist in the dictionary, the same case occurs in arrays too. I was asking whether you have something like this.

all of my information about players, NPCs, or Items are stored as values or model objects (under the Player, in the Workspace, etc).

Everything vanishes when the data itself vanishes, I checked for any potential leftover stuff, but couldn’t find any