UNITY引擎变量调用产生不必要内存分配

https://unity3d.com/de/learn/tutorials/topics/performance-optimization/optimizing-garbage-collection-unity-games?playlist=44069

Unity function calls

It’s important to be aware that whenever we call code that we didn’t write ourselves, whether that’s in Unity itself or in a plugin, we could be generating garbage. Some Unity function calls create heap allocations, and so should be used with care to avoid generating unnecessary garbage.

There is no list of functions that we should avoid. Every function can be useful in some situations and less useful in others. As ever, it’s best to profile our game carefully, identify where garbage is being created and think carefully about how to handle it. In some cases, it may be wise to cache the results of the function; in other cases, it may be wise to call the function less frequently; in other cases, it may be best to refactor our code to use a different function. Having said that, let’s look at a couple of common examples of Unity functions that cause heap allocations and consider how best to handle them.

Every time we access a Unity function that returns an array, a new array is created and passed to us as the return value. This behaviour isn’t always obvious or expected, especially when the function is an accessor (for example, Mesh.normals).

In the following code, a new array is created for each iteration of the loop.

void ExampleFunction()
{for (int i = 0; i < myMesh.normals.Length; i++){Vector3 normal = myMesh.normals[i];}
}

It’s easy to reduce allocations in cases like this: we can simply cache a reference to the array. When we do this, only one array is created and the amount of garbage created is reduced accordingly.

The following code demonstrates this. In this case, we call Mesh.normals before the loop runs and cache the reference so that only one array is created.

void ExampleFunction()
{Vector3[] meshNormals = myMesh.normals;for (int i = 0; i < meshNormals.Length; i++){Vector3 normal = meshNormals[i];}
}

Another unexpected cause of heap allocations can be found in the functions GameObject.name orGameObject.tag. Both of these are accessors that return new strings, which means that calling these functions will generate garbage. Caching the value may be useful, but in this case there is a related Unity function that we can use instead. To check a GameObject’s tag against a value without generating garbage, we can use GameObject.CompareTag().

In the following example code, garbage is created by the call to GameObject.tag:

private string playerTag = "Player";void OnTriggerEnter(Collider other)
{bool isPlayer = other.gameObject.tag == playerTag;
}

If we use GameObject.CompareTag(), this function no longer generates any garbage:

private string playerTag = "Player";void OnTriggerEnter(Collider other)
{bool isPlayer = other.gameObject.CompareTag(playerTag);
}

GameObject.CompareTag isn’t unique; many Unity function calls have alternative versions that cause no heap allocations. For example, we could use Input.GetTouch() and Input.touchCount in place of Input.touches, or Physics.SphereCastNonAlloc() in place of Physics.SphereCastAll().

posted on 2017-08-23 17:57 时空观察者9号 阅读(...) 评论(...) 编辑 收藏

UNITY引擎变量调用产生不必要内存分配相关推荐

  1. [转载]内存分配 知识,全局,局部,静态变量

    [转载]内存分配 知识,全局,局部,静态变量 预备知识-程序的内存分配 一个由C/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)- 由编译器自动分配释放 ,存放函数的参数值,局部变 ...

  2. Unity引擎UI模块知识Tree

    Unity引擎在4.6版本之前是没有内置的UI解决方案的.当时最流行的就是NGUI的框架,目前主流的UI解决方案就是NGUI和UGUI,近几年来,更是以UGUI为主,同时也有团队开始使用FairyGU ...

  3. JavaScript内存分配及垃圾回收机制

    JavaScript内存分配及垃圾回收机制 简介 像C语言这样的高级语言一般都有底层的内存管理接口,比如 malloc()和free().另一方面,JavaScript创建变量(对象,字符串等)时分配 ...

  4. 深入浅出内存管理--kmalloc支持的最大内存分配

    首先我们来看下kmalloc的实现,本文基于kernel 4.0版本: static __always_inline void *kmalloc(size_t size, gfp_t flags) { ...

  5. java map 内存分配_Android O Bitmap 内存分配

    我们知道,一般认为在Android进程的内存模型中,heap分为两部分,一部分是native heap,一部分是Dalvik heap(实际上也是native heap的一部分). Android B ...

  6. Android的内存分配与回收

    想写一篇关于android的内存分配和回收文章的想法来源于追查一个魅族手机图片滑动卡顿问题,我们想了很多办法还是没有避免他不停的GC,所以就打算详细的看看内存分配和GC的原理,为什么会不断的GC,GC ...

  7. JVM源码简析(楔子)-对象内存分配过程和PS回收器中YGC触发FGC的现象

    前言 想要搞明白Java对象内存申请过程的原因,是因为第一次接触线上GC日志的时候,发现了一些很奇怪的现象,就是young gc触发了full gc.为了搞清楚这个现象,得先要来个测试去复现. 复现现 ...

  8. Unity 引擎报错集锦

    这里记录的是unity引擎本身的bug,没有特殊说明,所用引擎是2018.3.0f2 2019.1.2 ab包里面的animator的normalized time不准确,在不循环的情况下, 某个st ...

  9. Unity引擎基础补充

    Unity引擎基础补充 本博客将对之前的Unity脚本,3D数学基础博客内容做一定的补充.所以部分知识点可能并不全. 使用Unity的API,我们要清楚各个参数其代表的含义. 文章目录 Unity引擎 ...

最新文章

  1. Adobe推出HTML5动画设计工具Edge
  2. yum标准化安装nginx最新版
  3. go语言html css,html – 在Golang中加载图像和CSS
  4. keep-alive和多路复用
  5. 如何最快的销售砖头(2.0版)
  6. Android 4.0 ICS SystemUI浅析——SystemUI启动流程
  7. 限制只允许某个进程调用库
  8. 拓端tecdat|R语言中贝叶斯网络(BN)、动态贝叶斯网络、线性模型分析错颌畸形数据
  9. mongodb之副本集搭建
  10. 《HBase权威指南》一3.4 行锁
  11. libpng库的移植与使用
  12. 矩阵乘法运算-JAVA实现
  13. windows7在无法访问Internet的情况下,修改公共网络为家庭网络(或工作网络)
  14. 淘宝优惠券商城赚钱是真的吗?购物最便宜的app
  15. 报错 SocketTimeoutException: null
  16. 【数学模型】基于Volterra理论的捕食模型
  17. 描述文件是什么?也算是APP的一种吗?
  18. Python基础入门之列表生成式
  19. Linux下Socket编程之TCP原理
  20. 程序员需要学习英语吗?

热门文章

  1. Python极简入门:数据类型、条件语句、循环语句、异常处理
  2. 为什么做软件类项目,会出现人多,事少,工作量大的情况?
  3. Vue中使用el-popover实现悬浮弹窗显示图片预览
  4. DevExpress的进度条控件ProgressBarControl的使用-以ZedGraph添加曲线进度为例
  5. SVN中怎样忽略当前文件不提交
  6. Could not find a version that satisfies the requirement PIL
  7. 动态系统开发方法DSDM
  8. java 进程描述_java 进程和线程
  9. python limit_Python MySQL Limit
  10. linux 串口编程_ARM-Linux开发与MCU开发有何不同?上篇