Currently, as of 5/24/23, if the API Terrain:Clear() is called when a large amount of terrain voxels are present, it can fail. Instead of clearing the terrain and continuing script execution, it will hang for 30 seconds, causing script execution to be terminated. However, when the error occurs, the terrain gets cleared right after the error, despite the script being terminated. This is problematic behavior!
To reproduce, simply open an empty baseplate, generate an 8,000 x 500 x 8,000 piece of terrain and call Terrain:Clear() in a script.
Expected behavior
It is expected that calling Terrain:Clear() would be a fast operation & not fail. I’m not sure why this is slower than a few seconds, it’s as if the engine is manually resetting data in the same table internally instead of allocating a new one & GCing the old one.
I also experience the same issue with Terrain:Clear() and what I did to get around it was to measure the size of my map and create pillars to FillRegion with Air
--Our terrain max size so far is 9000x9000 units total
--Divide it into 1000x1000 pillars and loop across the map clearing in segments
for i = 1,9 do
for j = 1,9 do
local startX = -4500
local endX = 4500
local distance = endX - startX
local division = (distance / 9) * (i-1) --1000
local division2 = (distance / 9) * (j-1) --1000
local Region2 = Region3int16.new(Vector3int16.new(-4500 + division,0,-4500 + division2),Vector3int16.new(-3500 + division,2900,-3500 + division2))
workspace.Terrain:FillRegion(Region3int16toRegion3(Region2),4,Enum.Material.Air)
end
end
And then I just run a Terrain:Clear() right after to clean up any remaining bits.
Terrain in general seems rather slow and prone to memory leaking. I’m not sure what causes it but PasteRegion, FillRegion, etc all seem to caused huge spikes in UntrackedMemory that never goes down and it becomes a significant issue when rotating between various maps using TerrainRegions.
Thank you for reporting this. It is not about manually resetting the data, but all the systems that have to be kept up to date with current shape of terrain (e.g. collision) are trying to update their caches for the entire world a the same time in a sub-optimal way. This problem has always existed, but became more prominent recently because of some new memory optimizations.
We will look into this and try to fix it. In the mean time, please feel free to use @heedicalking 's workaround.
Is there any update to this and perhaps any other Terrain-related performance issues? We are starting to get execution times errors again related to this as well as high terrain memory usage that is never cleaned up
Hi @heedicalking , the fix was released back in July IIRC. I just verified that it works.
I am verifying using the following reproduction steps:
Load a large 8k x 8k terrain (I can’t upload it because it is too big - 23MB).
Type workspace.Terrain:Clear() into the command bar
Without the fix, Studio locks up for a long time. With the fix, the operation returns immediately. Note that it takes some time for the operation to propagate (to refresh all meshes, etc). But this shouldn’t cause any crashes or lockups.