Monday, April 1, 2013

Unity3d Get Heap and Memory Size

Unity3d is a very powerful game engine. Underneath it makes use of the awesome Mono framework, which makes it possible for people to write video games using C#. How awesomer can things get?

Below is a code snippet that one can use to get the heap size and the used memory size.


#if UNITY_IPHONE && !UNITY_EDITOR
    [DllImport("__Internal")]
#else
       [DllImport("mono")]
#endif
static extern long mono_gc_get_used_size();  //Amount Used

#if UNITY_IPHONE && !UNITY_EDITOR
    [DllImport("__Internal")]
#else
       [DllImport("mono")]
#endif
       static extern long mono_gc_get_heap_size(); //Total Amount

long memUsed = mono_gc_get_used_size();
long deltaMem = memUsed - previousMemUsed;
long heapSize = mono_gc_get_heap_size();
float percentageMemUsed = (memUsed / (float)heapSize) * 100;
previousMemUsed = memUsed;



For more profiling, use these tools: http://docs.unity3d.com/Documentation/Manual/Profiler.html

No comments: