参考链接:

http://www.cnblogs.com/mrkelly/p/5391156.html#3415238

http://www.cnblogs.com/murongxiaopifu/p/4284988.html#GC

一.减少使用foreach

首先回顾一下IEnumerable和IEnumerator

    public interface IEnumerable{IEnumerator GetEnumerator();}
    public interface IEnumerator{bool MoveNext();object Current { get; }void Reset();}

如果一个集合要使用foreach进行遍历,那么需要实现IEnumerable这个接口。

下面回到正题,测试一下:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;public class TestCSharp : MonoBehaviour {private List<int> list = new List<int>();private Dictionary<int, int> dict = new Dictionary<int, int>();private int sum;void Start () {for (int i = 0; i < 1000; i++){list.Add(i);dict.Add(i, i);}sum = 0;Profiler.BeginSample("ForList");for (int i = 0; i < list.Count; i++){sum += i;}Profiler.EndSample();Debug.Log(sum);sum = 0;Profiler.BeginSample("ForeachList");foreach (var item in list){sum += item;}Profiler.EndSample();Debug.Log(sum);sum = 0;Profiler.BeginSample("EnumeratorList");var listEnumerator = list.GetEnumerator();while (listEnumerator.MoveNext()){sum += listEnumerator.Current;}Profiler.EndSample();Debug.Log(sum);///sum = 0;Profiler.BeginSample("ForDict");//字典可以看作两个list,一个list存key,一个list存value//两个list是一一对应的关系List<int> keys = new List<int>(dict.Keys);for (int i = 0; i < dict.Count; i++){sum += dict[keys[i]];}Profiler.EndSample();Debug.Log(sum);sum = 0;Profiler.BeginSample("ForeachDict");foreach (var item in dict){sum += item.Value;}Profiler.EndSample();Debug.Log(sum);sum = 0;Profiler.BeginSample("EnumeratorDict");var dictEnumerator = dict.GetEnumerator();while (dictEnumerator.MoveNext()){sum += dictEnumerator.Current.Value;}Profiler.EndSample();Debug.Log(sum);}}

结论:从gc和速度来看,使用GetEnumerator()来遍历集合是最好的。这里我建议使用for遍历list,使用GetEnumerator遍历字典。

二.减少使用引用类型

引用类型存放在内存堆上,当引用类型不再被使用时,就会触发gc。

Ps:string是引用类型

[Unity优化]减少gc相关推荐

  1. 【酷熊科技】工作积累 ----------- 【Unity】减少GC回收

    [Unity]减少GC回收 两种情况GC会被触发: 堆的内存不足时自动调用GC. 手动的调用GC. 减少GC回收要注意一下问题:SFT池GCNL 字符串连接的处理.使用StringBuilder或St ...

  2. Unity优化之GC——合理优化Unity的GC (难度3 推荐5)

    原文链接: http://www.cnblogs.com/zblade/p/6445578.html 最近有点繁忙,白天干活晚上抽空写点翻译,还要运动,所以翻译工作进行的有点缓慢 =.= 本文续接前面 ...

  3. Unity优化之GC——合理优化Unity的GC

    介绍: 在游戏运行的时候,数据主要存储在内存中,当游戏的数据在不需要的时候,存储当前数据的内存就可以被回收以再次使用.内存垃圾是指当前废弃数据所占用的内存,垃圾回收(GC)是指将废弃的内存重新回收再次 ...

  4. [Unity优化]减少顶点数目

    参考链接: http://www.ceeger.com/Components/class-LODGroup.html http://www.ceeger.com/Manual/OcclusionCul ...

  5. Unity3d面向英特尔 x86 平台的 Unity* 优化指南: 第 2 部分

    目录 优化 脚本优化 脚本视锥剔除和协同例程 智能内存管理 缓存频繁使用的对象和组件 使用 Unity 物理系统的最佳实践 禁用完全透明对象 返回至教程第 1 部分: 面向英特尔 x86 平台的 Un ...

  6. Unity优化 详谈GetComponent

    文章转自:http://www.manew.com/thread-104010-1-1.html?_dsign=aaa7cc41 0x00 前言 在很长一段时间里,Unity项目的开发者的优化指南上基 ...

  7. IGDATaiwan上Unity 优化讲座III

    IGDATaiwan上Unity 优化讲座III (罗志达) https://www.youtube.com/user/IGDATaiwan 议程: https://2019.tgdf.tw/agen ...

  8. Unity优化方面的一些小总结

    前言: 我做了3年的Unity了,但是却没有深入优化模块的内容,只能怪自己做项目的时候做的内容太杂乱了.去面试的时候被面试官问道优化方面的内容的时候自己一脸懵逼.本来有机会去MK的,让自己错失了. 所 ...

  9. unity 优化大全

    看到一篇了好文章,虽然中间有些地方有点儿小问题,但是瑕不掩瑜,给赞.原文链接:http://www.jianshu.com/p/1a2b5d045077 最简单的优化建议: 1.PC平台的话保持场景中 ...

最新文章

  1. Spring常见的十八中异常Exception
  2. AngularJS路由使用案例
  3. 二叉排序树删除子节点以及遍历
  4. linux下cache分析工具,cachestat、cachetop、pcstat-linux系统缓存命中率分析工具
  5. linux 日志 转存,如何记录linux终端下的操作日志(转)
  6. DotNet Framework 小技巧
  7. python可以参加哪些竞赛_找出Python竞赛中可达到的分数的程序
  8. Feed系统架构资料收集
  9. linux服务器架设--学习笔记
  10. 4. 创建 bundle
  11. Java编译器使用入门
  12. MT4外汇操盘跟单软件
  13. ASCII栅格的显示
  14. Google VR开发-Cardboard VR SDK反畸变实现
  15. freeBSD 14 CURRENT 笔记本核显独显混合安装 nvidia驱动
  16. [附源码]计算机毕业设计Node.js红叶装潢公司员工管理系统(程序+LW)
  17. Desktop Computer操作系统之GUI发展
  18. pip3 install -i sklearn 安装报错
  19. 小酌Django4——博客文章展示
  20. 核心层,汇聚层,接入层,交换机性能差别

热门文章

  1. 练习Vue烘培坊项目
  2. 假期必看的6部震撼狙击手电影
  3. 主键的概念作用及特点
  4. vue 富文本存储_VUE 富文本(vue2-editor)
  5. python str与bytes转换
  6. HTML屏蔽右键屏蔽的一些简单易用的代码
  7. 邹欣:你的工作就是最好的面试
  8. 杰理之芯片丝印说明【篇】
  9. 普通家庭,在老家一个女人带一个孩子一个月要多少钱?
  10. ncnn/net.h: No such file or directory的解决方法