BUG: Changing parent of a model/part many times LEAKS memory and takes MORE time

  1. What do you want to achieve? Keep it simple and clear!
    I am working on a custom distance rendering system which hides parts/models by parenting them in ReplicatedStorage if they are not within the distance defined. I have a problem where the more a model/part gets its parent changed, the longer it takes it the next time to change its parent. Also, changing parents leak memory.

  2. What is the issue? Include screenshots / videos if possible!
    The issue is: the more you change the parent of a model/part, the longer it takes next time to change its parent and you get memory leaks.

Here’s a video of my memory usage just going up from simply changing the parent of a model constantly: RobloxStudioBeta_iWlDSh0MOt.mp4

Keep in mind this is just a classic baseplate with a simple script to constantly change the parent of a model.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I created a simple place and put a script that parents a model in ReplicatedStorage and back to workspace after the FPS hits 59+ repeatedly and stops after 3 minutes has passed. As it keeps changing the parent of the model, the memory usage keeps going up high and it takes longer and longer to change the model’s parent.

To test this out on your own, you’ll need to place a model in workspace and call it “Model”. Next you’ll need a LocalScript to keep track of your FPS put under StarterPlayerScripts and another put under StarterCharacterScripts to change the parent of the model. You could also put a custom wait of your choice so it doesn’t do it instantly, but regardless, over time it’ll end up at the same result.

When line 20 gets executed ( model:Destroy() ), roblox will error saying: “Script timeout: exhausted allowed execution time” and the memory will spike by more than 2 times before it goes back to the same level it was before destroying the model. If you don’t destroy the model, then the memory levels never go back down but stays as high!

FPS LocalScript:

-- Services
local RunService = game:GetService("RunService")
-- Instances
local LocalPlayer = game.Players.LocalPlayer
-- Configs
-- Variables
local lastIteration = nil
local start = os.clock()
-- Tables
local frameUpdateTable = {}
-- Functions
local function HeartbeatUpdate()
	lastIteration = os.clock()
	for index = #frameUpdateTable, 1, -1 do
		frameUpdateTable[index + 1] = frameUpdateTable[index] >= lastIteration - 1 and frameUpdateTable[index] or nil
	end

	frameUpdateTable[1] = lastIteration
	LocalPlayer:SetAttribute(
		"fps",
		math.floor(os.clock() - start >= 1 and #frameUpdateTable or #frameUpdateTable / (os.clock() - start))
	)
end
-- Running Functions
RunService.Heartbeat:Connect(HeartbeatUpdate)

Second LocalScript for changing the parent of the model constantly:

-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Instances
local LocalPlayer = game.Players.LocalPlayer
-- Configs
local MAX_TIME = 180
-- Variables
local model = game.Workspace.Model
-- Functions
local function Start()
	local initiatedTime = os.clock()
	local start
	while true do
		start = os.clock()
		model.Parent = ReplicatedStorage
		repeat task.wait()
		until LocalPlayer:GetAttribute("fps") >= 59
		model.Parent = game.Workspace
		if os.clock() - initiatedTime >= MAX_TIME then
			model:Destroy()
			return
		end
	end
end
-- Running Functions
Start()
1 Like

If this is a bug, you might want to submit this to @Bug-Support

Well, it is a bug but I cannot post in bug reports. Hence, I am asking here if there is any way to fix this/prevent this.

There are multiple worlds/islands in my game which the player can load in and there’s a custom distance rendering script that hides parts if they are not within the distance defined.

Considering memory leaks happen the more a model/part gets its parent changed, there’d be a considerable amount of memory leak since there’s a lot of loading/unloading of the worlds/objects and the more it happens, the longer it takes to load/unload (as it needs more performance to change the parent)… I could clone the objects but that would not be effective and would not replicate with the server if there’s a change done by the server.

How am I supposed to load different worlds/islands in my simulator game and keep my custom distance rendering script without causing huge memory leaks while keeping it all in the same “roblox place”.

They meant that you should PM the @Bug-Support group, it’s a group you can report bugs to if you don’t have the Regulars role.

2 Likes