- Home /
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 have Unity Pro the profiler will show memory usuage to the level of detail you require.
thanks karljj1 so I can't simply place a cube and add a rigidbody then find out how much memory that is going to require. I know how to calculate the textures and everything else.
$$anonymous$$aybe I'm being too vague.
Is there a way to find out the total memory allotment of lets say the rigidbody component? You know it's maximum. I'm sure they didn't just say "hey rigidbody you just go ahead and use all the memory you want sweet thing".
If there isn't a way through script is there a way I can access the code for the components so I can see for myself?
Using the profiler you can see the infromation you need. $$anonymous$$g see my image, the rigid body uses 108 bytes. You need Unity Pro for this and can also access the profiler data using scripts.
[1]: /storage/temp/35234-untitled.png
Answer by Immanuel-Scholz · Nov 14, 2014 at 01:03 PM
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");
}
Your answer
