组件功能

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. H5调用Android播放视频

    webView.loadUrl("http://10.0.2.2:8080/assets/RealNetJSCallJavaActivity.htm"); js调用的Java文件中 ...

  2. Linux初学者指南

    1.为啥我们要学习Linux? 我们干嘛要学习Linux? Linux能给我们带来什么价值呢? Linux给我的感觉就是稳定,免费,性能好. 稳定,体现在哪里?我们使用PC机,安装的操作系统一般是wi ...

  3. 调用存储过程从EntityFramework

    Prerequisites The prerequisite for running these examples are the following sample tables with test ...

  4. Servlet API遍程常用接口和类

    本文主要总结Servlet  API遍程常用接口和类 Servlet API http://tomcat.apache.org/tomcat-5.5-doc/servletapi/index.html ...

  5. 开始学习Oracle了

    开始学习Oracle了,加油 参考书Oracle开发实战经典,李兴华老师编著

  6. macbook pro 重装系统

    重装前系统版本:10.11.6 因为我在系统更新时强行关机,后来在编译代码的时候就一直有奇怪的错误,所以选择重装系统. 前提条件:一定要有网络 1.关机状态下按住command + r ,按一下开机键 ...

  7. C# C/S WPF 远程操作服务器上面的文件

    作的时候用的是WPF,需要做一个上传附件的功能,服务器上有一个文件夹,附件都上传到里面,只知道URL路径. 文件夹是在服务器上的IIS里面(就比如说你发布一个网站,把文件夹建在网站下面,当然这个网站啥 ...

  8. 朝花夕拾-android 从手机选择图片或拍照设置头像

    Demo源码位置:http://git.oschina.net/zj2012zy/Android-Demo/tree/master/AndroidDemo/headset 一般需要用户信息的好多的也需 ...

  9. Objective-C之代理设计模式小实例

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  10. SPC.NET,为5年的开发做个结尾

    从08年到如今从事asp.net开发已五年,起初只是一个简单的喜好,想做个东西出来.于是SPC.NET的前生诞生了,直至今日. 不得不说,一个人开发一个软件是很辛苦的事情,以下是SPC.NET的一些特 ...