Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Leak Memory...

Discussion in 'Editor & General Support' started by realwar_fx, Mar 30, 2018.

  1. realwar_fx

    realwar_fx

    Joined:
    Mar 30, 2018
    Posts:
    5
    Start scene with 300mb, finish - 550mb, where are the others 250?
    With each launch a leak more and more...:(


    Simple script:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. class Creator : MonoBehaviour
    4. {
    5.     GameObject go;
    6.  
    7.     void Start()
    8.     {
    9.         go = Resources.Load(@"Sprite") as GameObject;
    10.     }
    11.  
    12.     void Update()
    13.     {
    14.         for (int i = 0; i < 1000; ++i)
    15.         {
    16.             Instantiate(go);
    17.         }
    18.     }
    19. }
     
  2. Ailrau

    Ailrau

    Joined:
    Feb 5, 2017
    Posts:
    24
    You creating 1000 objects in every frame that's why this code is very very bad. You need to create objects in start or another specific void not update or anything repeated void
     
  3. realwar_fx

    realwar_fx

    Joined:
    Mar 30, 2018
    Posts:
    5
    Unity can not handle this?
     
  4. Lu4e

    Lu4e

    Joined:
    Mar 23, 2018
    Posts:
    276
    I start to realise what is leeching our unity support resource.
     
    Peter77 and Joe-Censored like this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Ummmm, do you not realize that your 1000 GameObjects you are creating every frame are going to use memory? The memory isn't leaking, you're just using it. If you destroy the GameObjects the memory should free up when the garbage collector hits.
     
    Last edited: Mar 30, 2018
  6. realwar_fx

    realwar_fx

    Joined:
    Mar 30, 2018
    Posts:
    5
    I turn off the scene, why memory is not completely freed?
     
  7. Lu4e

    Lu4e

    Joined:
    Mar 23, 2018
    Posts:
    276
    Resources.UnloadUnusedAssets()
    Hope it helps your problem:)