Skip to content

Commit 6207cdc

Browse files
committed
Fixed memory leak in double buffer pattern
1 parent d5e4e9c commit 6207cdc

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

Assets/Patterns/7. Double Buffer/Cave/GameController.cs

+8
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ private int GetSurroundingWallCount(int cellX, int cellY)
163163
//Display the texture on a plane
164164
private void GenerateAndDisplayTexture(int[,] data)
165165
{
166+
//We are constantly creating new textures, so we have to delete old textures or the memory will keep increasing
167+
//The garbage collector is not collecting unused textures
168+
Resources.UnloadUnusedAssets();
169+
//We could also use
170+
//Destroy(displayPlaneRenderer.sharedMaterial.mainTexture);
171+
//Or reuse the same texture
172+
173+
166174
//These two arrays are always the same so we could init them once at start
167175
Texture2D texture = new Texture2D(GRID_SIZE, GRID_SIZE);
168176

0 commit comments

Comments
 (0)