Detect RAM Spikes

Hey. I am wondering how I might detect RAM spikes in my game.
Ideas?
Thanks!

2 Likes

you can check the client memory which determines the laginess of your game by holding ctrl + shift + f7 and look on the top left of your screen for your client memory. You can also use the Micro Profiler to determine whether something is making your game laggy.

2 Likes

Thanks for your speedy response.
But how might I detect this using a local script?

You don’t really detect stuff like this with local scripts. You just use the micro profiler or ctrl + shift + f7 in the roblox client to see your local memory.

Yeah, but this would be for an anti-cheat. So that wouldn’t really work.

Yea its not a good idea for an anti-cheat

you can use Stats:GetTotalMemoryUsageMb() to get the server or clients memory usage.

game.Players.PlayerAdded:Connect(function(player)
while true do
wait(5)
if game:GetService("Stats"):GetTotalMemoryUsageMb() > 700 then
     print("Player is exploiting",player.Name)
     player:Kick("Message here ig")
end
end
end)

you can instead do

game.Players.PlayerAdded:Connect(function(player)
while task.wait(5) do
if game:GetService("Stats"):GetTotalMemoryUsageMb() > 700 then
     print("Player is exploiting",player.Name)
     player:Kick("Message here ig")
end
end
end)

U need to test the memory usage in roblox not in roblox studio cause there is a big difference, also this anti-cheat is a big mistake since it could kick non exploiters. Since loops take alot of memory ur better off making a exploit that doesnt need this

2 Likes

Im kina out of options at this point

@neviquatro @betterthenharrybarry

If I am not mistaken, those will check for the total Memory Usage, which means it doesn’t detect spikes. So eventually every player will get kicked because it is collective.

Wouldn’t that just kick them if their computer is constantly using more than 700 MB of RAM???

1 Like

You can just change the value lmaoo

Yeah, I know that I’m just saying that’s not the best solution to the problem.

1 Like

U can check if it spikes by comparing the values, not that hard.

What might work as a solution is this:

local Stats = game:GetService("Stats")
local RunService = game:GetService("RunService")
local MaxDelta = 100
local LastValue

RunService.Heartbeat:Connect(function()
       local CurrentValue = Stats:GetTotalMemoryUsageMb()

       if not LastValue then
             LastValue = CurrentValue
             return
       end

       local Delta = CurrentValue - LastValue

       if Delta >= MaxDelta then
             warn("RAM Spiked")
       end

       LastValue = CurrentValue
end)

Hope it helps

1 Like

I am going to test it. Ill let you know!

Update: Didnt work

Sorry for the late post.

Did you ensure that the RAM actually spiked before saying it didn’t work? The script Jimmy has given you should have worked.

You can take measurements on the client using Stats:GetTotalMemoryUsageMb() and check for outliers:

local t = 0.01 --how often we take measurements?
local n = 50 --how large is our sample?
--if the memory is 200MB more than average, it's a spike
local threshold = 200

local function getCurrentMemoryUsage(): number 
	return game.Stats:GetTotalMemoryUsageMb()
end

local function getAverage(samples: {number}): number
	if #samples == 0 then return 0 end
	local total = 0
	for _, v in pairs(samples) do total += v end 
	return total/#samples 
end

local measurements = {}

while task.wait(t) do
	local current = getCurrentMemoryUsage()
	if #measurements < n then 
		table.insert(measurements, current) 
		continue 
	end
	local diff = current-getAverage(measurements)
	if diff > threshold then 
		--we found a lag spike
		print("Lag Spike Detected:", current) 
		task.wait(1)
	end
	table.remove(measurements, 1)
	table.insert(measurements, current)
end

Here’s a test you can do to verify the above code:

for i = 1, 350 do Instance.new("Part", workspace) end
1 Like