The memory of cesium for ue4

I have put 3dtiles data in the scene ,and I can see the data in the Cesium viewer.
It is easy to notice that the memory increases when a new tile loading,but never decrease.
It seems the tile data not be released property.
Is it a bug? Can any tell me more about it?

1 Like

Hi @duanmang,

We recently discover a memory leak when decoding textures. The bug is fixed in this PR and will go out to the next release.

Beside the bug, currently, we recently find out that unreal’s garbage collection won’t kick in during the editor mode. To force it, you can use gc.CollectEveryFrame 1 in the editor command console

Bao

Thanks for your answer!

I found the code is different from your version.May be my code is newer.It seems the problem has been resolved.(Code1)

I found another place may lead the memory leak.We use the class HalfConstructedReal to produce the StaticMeshComponent. There is std::vector loadModelResult in HalfConstructedReal. There might be some LoadModelResult instances not released, because they never had a chance to be passed to their staticMeshComponent in time. Where do you think we should deal with them. All the best.

Code1:

GltfReader::readImage(const gsl::span& data) const {
ImageReaderResult result;

result.image.emplace();
ImageCesium& image = result.image.value();

image.bytesPerChannel = 1;
image.channels = 4;

int channelsInFile;
stbi_uc* pImage = stbi_load_from_memory(
reinterpret_cast<const stbi_uc*>(data.data()),
static_cast(data.size()),
&image.width,
&image.height,
&channelsInFile,
image.channels);
if (pImage) {
const int lastByte =
image.width * image.height * image.channels * image.bytesPerChannel;
// std::uint8_t is not implicitly convertible to std::byte, so we must use
// std::transform here to force the conversion.
std::transform(
pImage,
pImage + lastByte,
std::back_inserter(image.pixelData),
(char c) { return std::byte(c); });
stbi_image_free(pImage);
} else {
result.image.reset();
result.errors.emplace_back(stbi_failure_reason());
}

return result;
}

I have download cesium for unreal V1.4.1 and test. But the issue is same, which looks like the memory only increases after loading 3dtiles but never decrease.

Following is Unreal Engine 4 Crash Reporter:

LoginId:38b7a68c41645290e2aa4792ae67878a
EpicAccountId:58d55dcfccc249548eaf5aa5b45e5b76

Fatal error: [File:D:/Build/++UE4/Sync/Engine/Source/Runtime/Core/Private/GenericPlatform/GenericPlatformMemory.cpp] [Line: 197] Ran out of memory allocating 8388608 bytes with alignment 0

UE4Editor_Core
UE4Editor_Core
UE4Editor_Core
UE4Editor_CoreUObject
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_CesiumRuntime!applyTexture() [D:\Build\++Portal\Sync\LocalBuilds\PluginTemp\HostProject\Plugins\CesiumForUnreal\Source\CesiumRuntime\Private\CesiumGltfComponent.cpp:1415]
UE4Editor_CesiumRuntime!loadModelGameThreadPart() [D:\Build\++Portal\Sync\LocalBuilds\PluginTemp\HostProject\Plugins\CesiumForUnreal\Source\CesiumRuntime\Private\CesiumGltfComponent.cpp:1506]
UE4Editor_CesiumRuntime!UCesiumGltfComponent::CreateOnGameThread() [D:\Build\++Portal\Sync\LocalBuilds\PluginTemp\HostProject\Plugins\CesiumForUnreal\Source\CesiumRuntime\Private\CesiumGltfComponent.cpp:1641]
UE4Editor_CesiumRuntime!UnrealResourcePreparer::prepareInMainThread() [D:\Build\++Portal\Sync\LocalBuilds\PluginTemp\HostProject\Plugins\CesiumForUnreal\Source\CesiumRuntime\Private\Cesium3DTileset.cpp:450]
UE4Editor_CesiumRuntime!Cesium3DTiles::Tile::update()
UE4Editor_CesiumRuntime!Cesium3DTiles::Tileset::_visitTileIfNeeded()
UE4Editor_CesiumRuntime!Cesium3DTiles::Tileset::_visitTile()
UE4Editor_CesiumRuntime!Cesium3DTiles::Tileset::_visitTileIfNeeded()
UE4Editor_CesiumRuntime!Cesium3DTiles::Tileset::_visitTile()
UE4Editor_CesiumRuntime!Cesium3DTiles::Tileset::_visitTileIfNeeded()
UE4Editor_CesiumRuntime!Cesium3DTiles::Tileset::_visitTile()
UE4Editor_CesiumRuntime!Cesium3DTiles::Tileset::_visitTileIfNeeded()
UE4Editor_CesiumRuntime!Cesium3DTiles::Tileset::_visitTile()
UE4Editor_CesiumRuntime!Cesium3DTiles::Tileset::_visitTileIfNeeded()
UE4Editor_CesiumRuntime!Cesium3DTiles::Tileset::_visitTile()
UE4Editor_CesiumRuntime!Cesium3DTiles::Tileset::_visitTileIfNeeded()
UE4Editor_CesiumRuntime!Cesium3DTiles::Tileset::_visitTile()
UE4Editor_CesiumRuntime!Cesium3DTiles::Tileset::_visitTileIfNeeded()
UE4Editor_CesiumRuntime!Cesium3DTiles::Tileset::_visitTile()
UE4Editor_CesiumRuntime!Cesium3DTiles::Tileset::_visitTileIfNeeded()
UE4Editor_CesiumRuntime!Cesium3DTiles::Tileset::_visitTile()
UE4Editor_CesiumRuntime!Cesium3DTiles::Tileset::_visitTileIfNeeded()
UE4Editor_CesiumRuntime!Cesium3DTiles::Tileset::updateView()
UE4Editor_CesiumRuntime!ACesium3DTileset::Tick() [D:\Build\++Portal\Sync\LocalBuilds\PluginTemp\HostProject\Plugins\CesiumForUnreal\Source\CesiumRuntime\Private\Cesium3DTileset.cpp:987]
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Core
UE4Editor_Core
UE4Editor_Core
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor
UE4Editor
UE4Editor
UE4Editor
UE4Editor
kernel32
ntdll