Search Issue Tracker
By Design
Votes
1
Found in
2018.3.0a1
2018.3.12f1
2019.1.0a1
2019.2.0a1
Issue ID
1146884
Regression
No
Tilemap leaks memory when Tilemap.ResizeBounds() and/or Tilemap.ClearAllTiles() are called
Reproduction steps:
1. Open the attached Project from "TilemapTest.zip"
2. Open a Profiler Window with a Memory Profiler inside of it
3. Open and Play "SampleScene"
4. Click anywhere inside of the Game View
Expected result: Total Object Count inside of Memory Profiler does not increase for every new rendered tile
Actual result: Total Object Count inside of Memory Profiler increases for every new rendered tile
Reproduces on 2018.3.13f1, 2019.1.0f2, 2019.2.0a12
Could not reproduce on 2017.4 due to missing Tilemap methods
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- Editor crashes on StoredGraphicsBuffer::GetGfxBufferID when VFX Graph property is modified during Play Mode and Application.targetFrameRate is used to limit FPS
- Crash on NVAPI_Thunk when changing Player resolution while HDR display is used and Direct3D12 is set as the graphics API
- Only one out of multiple cameras is shown in the Play Mode while HDR display is used and Direct3D12 is set as the graphics API
- The "Paste Component as New" option is incorrectly displayed as active despite the action being prohibited
- "TLS Allocator ALLOC_TEMP_TLS" errors are thrown when unsuccessfully importing an FBX file
Resolution Note:
Regarding this issue, I noticed that in your GameManager.cs script, at lines 152 and 155, you are calling:
tilemap.SetTile(pos, Tile.Instantiate<Tile>(tilePrefabs["Dirt"]));
and
tilemap.SetTile(pos, Tile.Instantiate<Tile>(tilePrefabs["Grass"]));
When Tile.Instantiate<Tile>() is called, it creates a copy of the Tile ScriptableObject. This causes the increase in object count that you see. The thing to note is that you will need to manage the copy of the ScriptableObject yourself, which includes the destruction of that instance.
The following link gives a summary about the behaviour of the ScriptableObject.
https://forum.unity.com/threads/scriptableobject-behaviour-discussion-how-scriptable-objects-work.541212/
Instead, I would suggest using the Tile directly on the Tilemap, as internally the Tilemap Component will create and manage the instances of that Tile for you. You can do so by using this, for example:
tilemap.SetTile(pos, tilePrefabs["Grass"]);