Of course it wont grow with a texture when you instantiate it, because that’s exactly what you’ve done, instance it! ;p
Textures are stored in one place, when you create a new object it’s pointing back at that same texture, why create a new one?
For memory to shoot up it needs to be from new variables placed on the heap, in their own space in RAM, without any instancing going on. For that, you can try this:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Controller : MonoBehaviour {
public GameObject prefab;
public Text text;
int numObjects = 0;
List<GameObject> references;
void Start () {
references = new List<GameObject>();
}
void Update () {
if(Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
if(Input.GetKeyDown(KeyCode.A))
{
// Add object if list hasn't reached capacity
if(numObjects < references.Capacity)
{
references.Add(Instantiate(prefab, new Vector3(Random.Range(-20, 20), Random.Range(-15, 15), 0), Quaternion.identity));
numObjects++;
text.text = "Reference list now size " + references.Capacity + ", objects:" + numObjects;
}
else
{
text.text = "Couldn't add object. Reference list now size " + references.Capacity + ", objects:" + numObjects;
}
}
if(Input.GetKeyDown(KeyCode.R))
{
// Add reference
references.Capacity += 1;
text.text = "Reference list now size " + references.Capacity + ", objects:" + numObjects;
}
}
}
Add that to your scene, with a reference to a prefab that has this script on it:
using UnityEngine;
public class BigString : MonoBehaviour {
private string s = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
void Start () {
//Creates a 4mb string
for(int ii = 0; ii < 11; ii++)
{
s += s;
}
}
}
In start the string doubles in size with each pass of the for loop, initially it’s 1000 characters, which means after 11 passes it will be 2048000 characters long, giving a 4mb size.
Each time the list is expanded in capacity (press R to add a reference), it only grows by a few bytes. When you actually create the object (press A), Unity’s allocated memory will jump noticeably. You don’t even need to put it into a build, just ctrl + shift + esc for Task Manager and view the memory for Unity Editor (unless you’re on Mac of course).