AllocationStats

From Unify Community Wiki
Jump to: navigation, search

AllocationStats is a simple helper utility for showing how much memory your application has allocated. It uses GC.GetTotalMemory to keep track of the memory usage.

Usage

Add the AllocMem script to any gameObject in your scene. When you press play, a small window will show containing the following info.

  • Currently allocated Shows the total memory allocated according to the GC.
  • Peak allocated Shows the max memory allocated during the application run with the amount of memory which was allocated when GC was called last time inside parentheses.
  • Allocation rate Shows how fast the application is allocating memory in mb per eh, 0.3 seconds. I should fix that sometime.
  • Collection frequency Shows how far apart the GC collections are spaced in seconds.
  • Last collect delta Shows how high the framerate was when GC was last called, GC calls usually make the framerate drop.
using UnityEngine;
using System.Collections;
using System.Text;
 
[ExecuteInEditMode ()]
public class AllocMem : MonoBehaviour {
 
	public bool show = true;
	public bool showFPS = false;
	public bool showInEditor = false;
	public void Start () {
		useGUILayout = false;
	}
 
	// Use this for initialization
	public void OnGUI () {
		if (!show || (!Application.isPlaying && !showInEditor)) {
			return;
		}
 
		int collCount = System.GC.CollectionCount (0);
 
		if (lastCollectNum != collCount) {
			lastCollectNum = collCount;
			delta = Time.realtimeSinceStartup-lastCollect;
			lastCollect = Time.realtimeSinceStartup;
			lastDeltaTime = Time.deltaTime;
			collectAlloc = allocMem;
		}
 
		allocMem = (int)System.GC.GetTotalMemory (false);
 
		peakAlloc = allocMem > peakAlloc ? allocMem : peakAlloc;
 
		if (Time.realtimeSinceStartup - lastAllocSet > 0.3F) {
			int diff = allocMem - lastAllocMemory;
			lastAllocMemory = allocMem;
			lastAllocSet = Time.realtimeSinceStartup;
 
			if (diff >= 0) {
				allocRate = diff;
			}
		}
 
		StringBuilder text = new StringBuilder ();
 
		text.Append ("Currently allocated			");
		text.Append ((allocMem/1000000F).ToString ("0"));
		text.Append ("mb\n");
 
		text.Append ("Peak allocated				");
		text.Append ((peakAlloc/1000000F).ToString ("0"));
		text.Append ("mb (last	collect ");
		text.Append ((collectAlloc/1000000F).ToString ("0"));
		text.Append (" mb)\n");
 
 
		text.Append ("Allocation rate				");
		text.Append ((allocRate/1000000F).ToString ("0.0"));
		text.Append ("mb\n");
 
		text.Append ("Collection frequency		");
		text.Append (delta.ToString ("0.00"));
		text.Append ("s\n");
 
		text.Append ("Last collect delta			");
		text.Append (lastDeltaTime.ToString ("0.000"));
		text.Append ("s (");
		text.Append ((1F/lastDeltaTime).ToString ("0.0"));
 
		text.Append (" fps)");
 
		if (showFPS) {
			text.Append ("\n"+(1F/Time.deltaTime).ToString ("0.0")+" fps");
		}
 
		GUI.Box (new Rect (5,5,310,80+(showFPS ? 16 : 0)),"");
		GUI.Label (new Rect (10,5,1000,200),text.ToString ());
		/*GUI.Label (new Rect (5,5,1000,200),
			"Currently allocated			"+(allocMem/1000000F).ToString ("0")+"mb\n"+
			"Peak allocated				"+(peakAlloc/1000000F).ToString ("0")+"mb "+
			("(last	collect"+(collectAlloc/1000000F).ToString ("0")+" mb)" : "")+"\n"+
			"Allocation rate				"+(allocRate/1000000F).ToString ("0.0")+"mb\n"+
			"Collection space			"+delta.ToString ("0.00")+"s\n"+
			"Last collect delta			"+lastDeltaTime.ToString ("0.000") + " ("+(1F/lastDeltaTime).ToString ("0.0")+")");*/
	}
 
	private float lastCollect = 0;
	private float lastCollectNum = 0;
	private float delta = 0;
	private float lastDeltaTime = 0;
	private int allocRate = 0;
	private int lastAllocMemory = 0;
	private float lastAllocSet = -9999;
	private int allocMem = 0;
	private int collectAlloc = 0;
	private int peakAlloc = 0;
 
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox