Memory Leak, Script help

Hey everyone, long time :slight_smile:

In my project I’m getting a lot of leakage, not sure why or how to fix / avoid it. Hopefully you can point me in the right direction. Below is code that should work in a empty scene, just attach “testSpawn” & “testPhysicsManager” to an empty game object and press play. Essentially all I’m doing in the actual project, just without the extra jazz.

BREAKDOWN:

  • Spawn
  • Collide
  • Destroy
  • Repeat

Eventual crash do to low memory, in the actual project it takes about 20 minutes to bug out…I wouldn’t suggest waiting for the posted code to crash…it’ll take a really long time. I’ve been watching the task manager to gain an idea of where the memory is going. As objects are spawned / destroyed memory increases (duh) But also retains objects in memory when the scene is restarted fresh. I have to close and re-open unity in order to reclaim memory. I assume reloading the scene would do the same thing as seen in other posts. I haven’t found a proper solution to this though…so here I am. Thanks in advance!

using UnityEngine;
using System.Collections;

public class testSpawn : MonoBehaviour {
    public int totalSpawned;

    void Update () {
        for(int i = 0; i < 100; i++){
            GameObject gO = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            testPhysicsManager.Objects.Add (gO);
         
            gO.AddComponent<Rigidbody> ();
            gO.AddComponent<testDestroy> ();
         
            totalSpawned++;
        }
    }
}


-------------------------------------------------------------------------------------------

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class testPhysicsManager : MonoBehaviour {
    public static List<GameObject> Objects = new List<GameObject>();

    public int objectCount;

    void Update () {
        objectCount = Objects.Count; 
    }
}


-------------------------------------------------------------------------------------------

using UnityEngine;
using System.Collections;

public class testDestroy : MonoBehaviour {

    void OnCollisionEnter(){
        testPhysicsManager.Objects.Remove (gameObject);
        Destroy (gameObject);
    }
}

Also, I’ve tried these lines of code from posts I found…but I’m either using them incorrectly or they may not apply here…

        //1
        //Resources.UnloadUnusedAssets (); //Greatly increases leakage (has opposite of desired effect)

        //2
        //System.GC.Collect (); //No noticable change, also causes system crash if spawn count is > certain range (say 20 - 100)

You are creating and destroying 100 objects every frame. Why? What use case for this?

For more helpful suggestions, does this crash occur in a desktop build, or is it just an editor issue?

How many objects are there present when the scene crashes?

no I only create 1 at a time in the actual project, but they are often destroyed and quickly replaced. Overtime it becomes an issue, I spawn 100 in this test to speed up the process and make it easier to notice in the task manager. Hunting down 5kb is pretty tough lol. In the actual project though it’s more like 1mb every minute, per object…

It occurs in all builds, desktop / app / editor,
it varies but usually about 200-300, but they’ve all been destroyed and replaced something like 5-10,000x in total

I still have loads to do, but basically it’s a gravity game. All these dots collide n do junk before they go on to collide again. Ya’da ya’da. But I need the memory to continue working on it…

Im assuming you add them to the static list, do you ever remove them from the list after deletion?

yep, line 46:

  • testPhysicsManager.Objects.Remove (gameObject);

Noticed that if you change:
void OnCollisionEnter()
into
void Start()

then memory doesnt keep increasing…

uh, then the project doesnt work at all…
besides they need to collide in order to trigger the events (that take place in the actual project), I built these test scripts to reflect the actual project build

I think though even if it were in the start method the issue would remain. The objects would still be deleted and then respawned. It’s the object staying in memory after deletion that’s the issue

Object pooling is probably the answer

Hmm, found a unity guide on it. I’ll check into it, thanks!