How do i avoid memory leaks?!

Hello!

i have tried to make a map reloading system (it deleted the map from workspace and copies the map from server storage)
the thing is, this isn’t even code related, but after deleting the map from explorer itself MEMORY DOES NOT GO DOWN!!!
it’s not really as code related BUT I REALLY NEED HELP WITH THIS. If you die too many times your game will just crash because the memory does not go down after the map gets deleted.
before:
image
after:
image
after deleting:
image

it just somehow gets even higher.

1 Like

Have you checked out how “debrisservice” works?
Link: Debris | Documentation - Roblox Creator Hub

wouldn’t help me since i need it to get deleted after you “die” which means debris won’t help.

Could you possibly share your code where you instance the map and where you delete the map?

I haven’t actually got to deleting but i’ve deleted the entire folder from explorer which doesn’t free up much memory

Make a server script which removes the map, that could help.
If you :clone() and then :destroy() it shouldn’t take any memory up

this is the code:

local number = 0
task.wait()
local assetnumber = workspace.Values.AssetNumber.Value - 300
local function loaded()
	game:GetService("ContentProvider"):PreloadAsync({workspace.Pizzeria})
	script.Parent.Text = "Loading lighting"
	task.wait(10)
	warn("loaded")
	workspace.CurrentCamera.CFrame = workspace.Pizzeria.Cam.OfficeCamera1.CFrame
	script.Parent.Parent.Visible = false
	script:Clone().Enabled = false
	for _, songs in pairs(workspace.MenuRelatedInstances:GetDescendants()) do
		if songs:IsA("Sound") then
			songs:Stop()
		end
	end
	game.ReplicatedStorage.StartScriptsRemote:FireServer()
	script:Clone()
	script:Destroy()
end
local number = 0
local connection
local function loading()
	task.delay(25, loaded)
	script.Parent.Parent.Visible = true
	script.Parent.Parent.Parent.Parent.Menu.Enabled = false
		connection = workspace:WaitForChild("Pizzeria").DescendantAdded:Connect(function()
		number += 1
		warn(assetnumber, number)
		script.Parent.Text = "Reloading map. This may take a while."
		if number >= assetnumber then
			loaded()
			connection:Disconnect()
		end
	end)
end
script.Parent.Parent.Parent.Parent.Menu["New Game"].MouseButton1Click:Connect(loading)

yeah, it’s not really well written but it deletes itself which frees up memory which can actually be seen but the map still takes a lot of memory and frees up like 200 mb out of way more

One potential propblem is in line 11: script:Clone(). You clone it twice (Line 11, Line 18) and only remove once. Every time you call the function, you keep 1 more of the scripts.

Does this solve your problem?

1 Like

whoops! but that still won’t solve my issue. thanks though
also randomly when i play before the map even gets loaded it’s 4gb :sob:

do u wanna try using trove cleaner made by sleitnick

the wat??? link it please.
rowblocks

Do you have custom data structures (tables with data in them) in your application? If so, show what are you adding, and what happens if you call a custom ‘destroy’ method for the object.

this tool is good for preventing memory leak

The easiest way to delete maps and remove them from memory is to use Debris as shown in the following script:

local Debris = game:GetService('Debris') -- Get the Debris Service
local Map -- Define your map object
local RemovalTime = 0 -- Add an optional delay before removing the map

Debris:AddItem(Map,RemovalTime) -- Add an item to the Debris queue for deletion

i’ve decided to scrap this and instead just reset values when you die.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.