组件功能

allocationstats是一个简单的辅助工具,用于显示您的应用程序分配多少内存。它采用GC.GetTotalMemory来跟踪内存使用

使用方法

添加 Allocmem.cs 到场景中的任何GameObject。当你按下播放按钮,会显示一个小窗口包含以下信息。

Currently allocated(当前分配):显示GC分配的总内存

Peak allocated(峰值):显示了内存分配,()内的值是GC最后一次应用程序运行期间分配的最大内存

Allocation rate(分配率):显示了应用程序分配内存(以mb为单位),比如 0.3秒MB内存分配,在这个时候我应该修复。

Allocation rate(收集次数/频率):显示相距多远GC的集合间隔(秒)

Last collect delta(最后收集):显示帧率有多高,当GC上次调用,调用GC通常使帧率下降。

AllocMem.cs

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; }

WIKI地址

http://wiki.unity3d.com/index.php/AllocationStats

最新文章

  1. Swift 必备开发库10个
  2. SQL ALTER TABLE 语句在项目中的使用
  3. display:inline-block的空白bug问题
  4. 10.关联(Association)
  5. 2015南阳CCPC D - Pick The Sticks dp
  6. 用in判断input中的placeholder属性是否在这个对象里
  7. mac下的改装人生——关于mc700安装双硬盘以后的win重装问题
  8. C# 多个线程一直跑着While(true)
  9. DDD分层架构之领域实体(验证篇)
  10. form表单参数传递和url参数传递的区别
  11. 5、faker.js数据模拟
  12. 对接 第三方物流APP 手机版
  13. IOS端 margin-top 和 margin-bottom 使用负数时的区别
  14. WebRTC 配置环境
  15. 未能正确加载“EditorPackage”包(转)
  16. Benefit UVA - 11889(已知LCM和其中一个数,求另一个数)
  17. Pytorch多GPU并行处理
  18. angular学习笔记(三十一)-$location(1)
  19. Codeforces 912E - Prime Gift
  20. [转]Asp.net Mvc 与WebForm 混合开发

热门文章

  1. CSS应用内容补充及小实例
  2. 调用另一个Activity
  3. Java编程思想读书笔记之内部类
  4. javascript数组浅谈1
  5. 同源策略 JSONP(实践篇)
  6. Engine中如何实现先居中显示要素再闪烁
  7. JAVA基础学习day22--IO流四-对象序列化、管道流、RandomAccessFile、DataStream、ByteArrayStream、转换流的字符编码
  8. Swift Standard Library: Documented and undocumented built-in functions in the Swift standard library – the complete list with all 74 functions
  9. Stronger (What Doesn't Kill You)
  10. android 5.X之使用Palette