Memory optimization question

Hi all,

I’ve been going through the API and haven’t ran across any way of doing this yet.

Say I have created a custom prefab using various things. Is there any way to tell how much memory I’m going to be consuming which each spawn of these?

I ask because I’m trying to push my current project to it’s limit and had my test machine (Very low end computer) run out of memory multiple times.

I would rather have a calculated result rather than using the trial and error method and having the recompile over and over again and that’s not counting the time of transfer and starting on the other machine.

For example let’s say I want to spawn a bunch of cubes with rigidbodies, plus textures etc, is there a way I can get a byte readout on the space that single gameobject is going to take up.

If you own Unity Pro, the easiest way is the builtin profiler as menitioned in the comments.

If you do not own Unity Pro, you can use System.Diagnostics.Process to inspect the memory of your process. You should wait at least a frame (better some longer) between creation and inspection as Unity may initialize more memory asynchonously.

Here’s a first impression on a capture-memory-consumption function could look like (totally untested and written on-the-fly)

IEnumerable Start()
{
    var p = System.Diagnostics.Process.GetCurrentProcess();
    long mem = p.VirtualMemorySize64;
    for (int i = 0; i < 100; i++)
        Instantiate(prefab);
    yield return new WaitForSeconds(0.5f);
    Debug.Log("creating 100 prefabs took " + (p.VirtualMemorySize64 - mem) + " bytes");
}