Search Unity

Possible memory leak

Discussion in 'Editor & General Support' started by venom789, Jun 29, 2022.

  1. venom789

    venom789

    Joined:
    Apr 18, 2022
    Posts:
    178
    Hello,

    I challenged myself to finalize a small prototype of very simple games.
    To allow me to better understand the issues and be efficient for the future.

    My problem is optimization, I'm not yet an expert, and in programming, it's far from being optimized...

    I tried to do my best so that no extra items remained on my scene so useless.

    I noticed that when I play I had some small FPS loss, but since nothing is optimized, I don't worry too much.

    On the other hand I noticed that when my game goes into gameOver, and if I leave it too long in game Over, my pc lags.

    I opened my task manager window, opened the performance tab, Memory and I put game in game Over for a moment, then I clicked on restart.
    My surprise to see my memory go down all of a sudden.

    Is this what we call memory leak???
     
    Last edited: Jun 29, 2022
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    yes it sounds like you may have a memory leak in your code. You are probably doing something like adding objects to a collection unendingly during your scene.
     
    venom789 likes this.
  3. venom789

    venom789

    Joined:
    Apr 18, 2022
    Posts:
    178
    I disabled all the elements of my game to find the culprit, finally it comes from the only script that I did not write xD
     
  4. venom789

    venom789

    Joined:
    Apr 18, 2022
    Posts:
    178
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7.  
    8.  
    9. public class MeshColliderUpdate : MonoBehaviour
    10. {
    11.     private float time = 0;
    12.     SkinnedMeshRenderer meshRenderer;
    13.    
    14.     void Start()
    15.     {
    16.         meshRenderer = GetComponent<SkinnedMeshRenderer>();      
    17.     }
    18.    
    19.     void Update()
    20.     {
    21.         time += Time.deltaTime;
    22.         if (time >= 0)
    23.         {
    24.             time = 0;
    25.             UpdateCollider();
    26.         }
    27.     }
    28.  
    29.     public void UpdateCollider()
    30.     {      
    31.         Mesh colliderMesh = new ();
    32.         meshRenderer.BakeMesh(colliderMesh);
    33.         GetComponent<MeshCollider>().sharedMesh = null;
    34.         GetComponent<MeshCollider>().sharedMesh = colliderMesh;
    35.     }
    36. }
     
  5. venom789

    venom789

    Joined:
    Apr 18, 2022
    Posts:
    178
    Ok the problem comes from :
    if (time >= 0)
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Yeah you're creating a new Mesh every frame:
    Mesh colliderMesh = new ();

    This is going to result in a memory leak since you never call
    Destroy()
    on these meshes.
     
    venom789 likes this.
  7. venom789

    venom789

    Joined:
    Apr 18, 2022
    Posts:
    178
    Thanks !!!!!!! I'll check it out right now ^^
     
  8. venom789

    venom789

    Joined:
    Apr 18, 2022
    Posts:
    178
    PraetorBlue

    You are a real genius, it works, you saved me ^^

    So if I understood correctly at each UpdateCollider() a new mesh was created, and since it was not deleted they add it to the memory of my game, is that right?

    I tried with Destroy and DestroyImmediate, both work well, can you advise me which one?
     
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    I would advise - neither.

    Rather, make one new Mesh object in Awake, and reuse it constantly.
    Destroy it in OnDestroy. (not DestroyImmediate)
     
    venom789 likes this.
  10. venom789

    venom789

    Joined:
    Apr 18, 2022
    Posts:
    178
    I don't understand, I need a new mesh every frame, so that it is aligned with the animation of my character.
     
  11. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,925
    No you don't. You can just keep updating the existing one.

    Code (CSharp):
    1. private SkinnedMeshRenderer meshRenderer;
    2. private Mesh bakedMesh;
    3.  
    4. private void Awake()
    5. {
    6.     meshRenderer = GetComponent<SkinnedMeshRenderer>();
    7.     bakedMesh = new Mesh();
    8. }
    9.  
    10. private void OnDestroy()
    11. {
    12.     Destroy(bakedMesh);
    13. }
    14.  
    15. public void UpdateCollider()
    16. {
    17.     meshRenderer.BakeMesh(bakedMesh);
    18.     //etc etc
    19. }
     
    PraetorBlue and venom789 like this.
  12. venom789

    venom789

    Joined:
    Apr 18, 2022
    Posts:
    178
    Wow just perfect, I'm too stupid, I hadn't understood it like that :/
    On the other hand, I had succeeded in preventing the memory leak, but I still got an error in the console ... it would surely have caused me problems later.

    PraetorBlue spiney199 You are too strong thank you
    !!!!!!!!!
    !!!!
    !!
    !​
     
  13. venom789

    venom789

    Joined:
    Apr 18, 2022
    Posts:
    178
    So I don't need all this mess in UPDATE anymore, thanks again ^^
     
    spiney199 likes this.