组件功能

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

Unity-WIKI 之 AllocationStats(内存分配)的更多相关文章

  1. 内存管理 垃圾回收 C语言内存分配 垃圾回收3大算法 引用计数3个缺点

    小结: 1.垃圾回收的本质:找到并回收不再被使用的内存空间: 2.标记清除方式和复制收集方式的对比: 3.复制收集方式的局部性优点: https://en.wikipedia.org/wiki/C_( ...

  2. [转]全面理解Unity加载和内存管理

    [转]全面理解Unity加载和内存管理 最近一直在和这些内容纠缠,把心得和大家共享一下: Unity里有两种动态加载机制:一是Resources.Load,一是通过AssetBundle,其实两者本质 ...

  3. Unity 3D中的内存管理

    本文欢迎转载,但烦请保留此行出处信息:http://www.onevcat.com/2012/11/memory-in-unity3d/ Unity3D在内存占用上一直被人诟病,特别是对于面向移动设备 ...

  4. 全面理解Unity加载和内存管理

     全面理解Unity加载和内存管理http://game.ceeger.com/forum/read.php?tid=4394&fid=2&uid=6507 1.用简单的“for”循环 ...

  5. java 内存分配全面解析

    JVM是什么? 首先要知道的是Java程序运行在JVM(Java Virtual Machine,Java虚拟机)上;可以把JVM理解成Java程序和操作系统之间的桥梁,JVM实现了 Java的平台无 ...

  6. Java虚拟机内存分配详解

    简介 了解Java虚拟机内存分布的好处 1.了解Java内存管理的细节,有助于程序员编写出性能更好的程序.比如,在新的线程创建时,JVM会为每个线程创建一个专属的栈 (stack),其栈是先进后出的数 ...

  7. JVM-垃圾收集算法、垃圾收集器、内存分配和收集策略

    对象已死么? 判断一个对象是否存活一般有两种方式: 1.引用计数算法:每个对象都有一个引用计数属性,新增一个引用时计数加1,引用释放时计数减1.计数为0时可以回收. 2.可达性分析算法(Reachab ...

  8. 【Unity】开发WebGL内存概念具体解释和遇到的问题

    自增加unity WebGL平台以来.Unity的开发团队就一直致力于优化WebGL的内存消耗. 我们已经在Unity使用手冊上有对于WebGL内存管理的详尽分析,甚至在Unite Europe 20 ...

  9. 使用TryGetComponent取代GetComponent以避免Editor中的内存分配

    作为Unity开发人员,可能或多或少都会遇到过一个常见的Unity的GC分配问题——在Editor中使用GetComponent方法来获取一个不存在的Component时会分配额外的内存.就像下图 需 ...

随机推荐

  1. jvisualvm远程监控jvm设置

    有些时候,需要对特定环境中的Java应用进行实时性能分析,大部分非开发和测试环境,一般都是用jvisualvm进行基本检测以最小化对系统的影响(其开启后,负载影响大约20%-30%),jvisualv ...

  2. dbcp 1.4 底层连接断开时内存泄露bug

    在dbcp 1.4中,如果底层的连接已经与数据库断开了,此时dbcp 1.4的实现并不释放内部连接,虽然早已提供了removeAbandoned和removeAbandonedTimeout参数,但是 ...

  3. 使用checkbox实现纯CSS下拉框

    在这个例子中,我们会看到一个纯CSS制作的下拉框.主要是要用到了HTML元素的checkbox 和CSS3选择器,并没有用到JavaScript.例子如下: Click to Expand Link ...

  4. 来自 CORS 预检通道的 CORS 头 'Access-Control-Allow-Headers' 的令牌 'appkey' 无效)。

    1.服务端: web.config文件中: <system.webServer> <httpProtocol> <customHeaders> <add na ...

  5. Lync 客户端:无法登陆到Lync,验证服务器中的证书时遇到问题

    安装完Lync客户端后,运行时Lync客户端时,报出如下错误: [原因解析] Lync客户端没有正确安装CA证书链. [解决办法] 第一种方法:将计算机加入域. 第二种方法:不加入域的处理方法: 1. ...

  6. FileInputStream类

    FileInputStream和FileOutPutStream类都是用来操作磁盘文件的.如果用户对文件读取需求比较简单,则可以使用FileInputStream类,该类继承InputStream类 ...

  7. 安卓开发_浅谈ListView(SimpleAdapter数组适配器)

    安卓开发_浅谈ListView(ArrayAdapter数组适配器) 学习使用ListView组件和SimapleAdapter适配器实现一个带图标的ListView列表 总共3部分 一.MainAc ...

  8. android 判断屏幕是否亮着

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); boolean screen = p ...

  9. Python基础(4)--字符串

    所有标准的序列操作对字符串都适用,但字符串是不可变的 本文地址:http://www.cnblogs.com/archimedes/p/python-string.html,转载请注明源地址. 字符串 ...

  10. iOS开发 使用RMStore简化内购代码 + 内购买订单验证

    现在很多的app里面都添加了应用内购买,网上关于苹果证书的生成和设置的教程比较多,这里就不多赘述了,推荐几个个人觉得说的比较详细的网址: http://www.jianshu.com/p/86ac7d ...