组件功能

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. ASP.NET Web API通过ActionFilter来实现缓存

    using System; using System.Collections.Generic; using System.Linq; using System.Threading; using Sys ...

  2. mysql并发insert deadlock分析以及解决,无delete/update/for update

    关于并发insert操作发生deadlock这个情况,一直有很多争议,而且网上的帖子所有的例证和模拟其实不一定反映了真实的情况,例如:https://www.percona.com/blog/2012 ...

  3. JAVA书写规范

    java程序书写规范 命名规范    1.一般概念        1.尽量使用完整的英文描述符        2.采用适用于相关领域的术语        3.采用大小写混合使名字可读        4 ...

  4. Mssql链接mysql数据库

    最近在做mysql数据库实时同步到mssql数据库的方案,花了一周时间,测试通过了,在实际机器上测试出现了若干问题.第一个问题就是在mssql上链接mysql的问题. 第一步,安装 Mysql ODB ...

  5. Failed to upgrade AX 2012 R3 Retail channel database from CU9 to CU11 if SQL Server version was lower than 2012

    I tried to upgrade AX 2012 R3 Retail channel database from CU9 to CU11 for client. after generated n ...

  6. 解决SharePoint 文档库itemadded eventhandler导致的上传完成后,编辑页面保持报错的问题,错误信息为“该文档已经被编辑过 the file has been modified by...”

    在文档库中添加itemadded 后,在上传文件后,会自动打开文档属性的编辑页面,在保存的时候就会报错,说这个文档已经被编辑过了.这是应为默认itemadded实践是异步执行的,会在edit页面打开之 ...

  7. Atitit.工作流系统的本质是dsl 图形化的dsl  4gl

    Atitit.工作流系统的本质是dsl 图形化的dsl  4gl 1. 工作流系统的本质是dsl 图形化的dsl  4gl1 2. 为什么每个项目系统都需要工作流1 3. 工作流dsl与java .n ...

  8. Android--Apache HttpClient

    前言 上一篇文章介绍了使用HttpURLConnection来完成对于HTTP协议的支持.现在介绍一个新的方式来访问Web站点,那就是HttpClient. HttpClient是Apache开源组织 ...

  9. Mac 下使用sourcetree操作git教程

    SourceTree 是 Windows 和Mac OS X 下免费的 Git 和 Hg 客户端,同时也是Mercurial和Subversion版本控制系统工具.支持创建.克隆.提交.push.pu ...

  10. js闭包之初步理解( JavaScript closure)

    闭包一直是js中一个比较难于理解的东西,而平时用途又非常多,因此不得不对闭包进行必要的理解,现在来说说我对js闭包的理解. 要理解闭包,肯定是要先了解js的一个重要特性, 回想一下,那就是函数作用域, ...