Hey everyone, long time
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)