How to download and delete instances from RAM?

Hey, so im trying to make a system that would download and clear certain parts of the map from the RAM to improve performance, for example when a player is teleported to a house, the house level is downloaded from server and map is cleared from RAM. I tried using instance:Destroy(), but it doesn’t really clear the object from ram based on my tests. I didn’t find much on the internet, so i decided to ask here.

2 Likes

A trick here is to enable streaming, and to “detach” the interiors from the house itself.

The house itself can exist on the map, but it won’t have any interior; when a player tries to enter the house, they’re instead teleported into a different part of the map that will actually contain and display those interiors.

Streaming will then kick in to load in those interiors, and unload the map from memory.

Now I don’t know how your map is set up, and neither can I explain why :Destroy() doesn’t work for you (at least not until I see your entire codebase), but this is a good idea to consider.

That is actually quite smart! I would definetily try using this method. About the code, here it is

local map = game.Workspace.TheMap
script.Parent.MouseButton1Up:Connect(function()
	map:Destroy()
	map = nil
end)

Nothing complicated, just deleting the test map once button pressed. The test map is in a model in workspace

That’s literally just a crude form of streaming, so I suppose my method works very well for you here.

That aside, assuming that you are deleting the map on client-side, make sure you’re actually looking at client memory, and not server memory, since the map will still exist on the server.

I actually tested once more and found out, that it actually cleared off ram, its just the fact that the map weighed less than i expected so i didn’t notice the change of ram memory. Both client and server destruction works, but the server destruction cleared off more ram (maybe because some data of map was still replicated to client in first case).
Also, a side question, is there a way to download assets, that are not BaseParts, from server manually ?
Could i actually use a trick with streaming enabled, where i put certain assets in a part, then when i dont need them i move the part away from player, and then move it close to player when the assets are needed ?

Roblox developers do not have access to Luau’s garbage collector (aka its automatic memory management tool) and we also do not have access to machine memory locations. While :Destroy() sets the parent to nil and fully destroys the object, allowing the garbage collector to collect it, you still have no way of ensuring that it is fully cleaned up by garbage collector.

I’d recommend some reading here: A Beginner's Guide to Lua Garbage Collection