Hi, I am new to unity and have come across and interesting problem and I was wondering if someone might help explain it further. I am seeing the following behaviour when I have a Google Tango Manager / Tango AR Camera in my scene which also has a basic game object with a script attached to create a multidimensional array. I am using Unity 5.3.5f.
My multidimensional array 200,80,200 in size (x,y,z). If I create this array and declare a class which contains only TRUE (I’ll explain that more in a second) value types (e.g. int, float, double, bool) and run the code then I see the GC coming along and cleaning up taking about around 45 ms.
However, if I add string to my class - the GC immediately jumps to 150+ a massive leap and its very noticible in my game. I did some digging and found out that if I add any reference types list,dictionay, my own class then I get similiar results to when I add the string. I read up a bit more and realised that string is actually a reference type NOT a value type. I also checked that the code wasnt calling the constructor multiple times and once the array was built, it was never destroyed or rebuilt.
What I want to understand more is why there is such a huge jump in the GC when I add a string (even empty) to my class which is in the array?
Is this just how long its taking GC to navigate through my array and to find the reference objects?
Whats actually going on here?
using UnityEngine;
using System.Collections;
using System.Text;
using System.Collections.Generic;
public class TestObjectClass
{
public int myint{ get; set; }
public Vector3 mypos { get; set; }
public bool mybool { get; set; }
public string mystring { get; set; }
public float myfloat { get; set; }
public Vector2 mysecondPos { get; set; }
public Color mycolor{ get; set; }
}
public class CreateLargeArrayObjects : MonoBehaviour
{
TestObjectClass[,,] myBigArrayObjects;
/// <summary>
/// Start is called on the frame when a script is enabled just before
/// any of the Update methods is called the first time.
/// </summary>
public void Start ()
{
init ();
}
public void init ()
{
myBigArrayObjects = new TestObjectClass[200,80,200];
for (int x = 0; x < 200; x++) {
for (int y = 0; y < 80; y++) {
for (int z = 0; z < 200; z++) {
myBigArrayObjects [x, y, z] = new TestObjectClass ();
myBigArrayObjects [x, y, z].myint = 1;
myBigArrayObjects [x, y, z].mypos = new Vector3 (10, 10, 10);
myBigArrayObjects [x, y, z].mysecondPos = new Vector2 (20, 20);
myBigArrayObjects [x, y, z].mybool = true;
myBigArrayObjects [x, y, z].myfloat = 1.004f;
myBigArrayObjects[x,y,z].mycolor = new Color(1,0,0);
myBigArrayObjects [x, y, z].mystring = "absdsg";
}
}
}
}
}