真UNITY3D_聊聊C#的GC Alloc和回收优化等问题

其实我也不懂,不过很多人都说有这个问题

就好像当年读书或者刚毕业的时候,很多人都说数据库的死锁问题,A表锁B表,B表锁C表,然后就死锁了

但是工作多年,一次死锁都没见过

就好像做游戏大家常说的内存泄漏,做服务器长说的注入攻击

不好意思,本人工作多年,上面说的资深程序员大事件,别说没见过,就是自己想做也做不出来。

不信你模拟做做,别说预防了,其实在当今(2006~2018)国内程序环境,你想犯错都不是那么容易的事情(其实还是很多人能做到的,不过恐怕你没法达到这群人的门槛)

记住,你想在C#里面写爆内存,爆CPU的代码,写线程不安全的代码,写跨域的代码,难度其实不亚于你自己造台特斯拉

其实我是碰到过一次死锁的,中午午休回来上班,发现这个问题,当时整个公司60个程序员,没一人能解决,拖了2个小时,最后重启服务器解决,当时是在一个工厂上班,该工厂停工2小时,直接损失300W

不过我还想想聊聊C#的GC问题(其实CSDN上某博主的图文文章说的更清晰)

(后补)

关于C#的GC图文不看也可,只要记住3个问题

1.int, float,double等是值类型引用,比较省内存,这说了等于没说,你觉得到底uint省内存还是int省?

2.object,string,object也就是list, dictionary等,特别注意string是引用处理,特别注意,所以的高质量代码其实就是不写字串代码

3.综上所述,你和你同事的代码,其实几乎都是引用类型,也就是你们TM的都在用面向对象,慢就一个字

所以,就别想着通过替换值变量来减少GC了

举个栗子

//注意!!看到这样的代码要警醒了,这TM普通的双引号已经是字串GC Alloc
//可没法避免的是,你的游戏里面有千千万万这样的代码
//字串长短是否也影响内存呢??
//所以是不是写.SendMessage(1)这样的代码最好呢?答案显然不是的
gameObject.SendMessage("Start Stage");

 

我的UWA报告

其实很明显的,我就知道和字串有关

看看Camera的Update()代码

        dis += Input.GetAxis("Mouse ScrollWheel") * 5;
        Debug.Log("dis=" + dis);
        if (dis < 10 || dis > 40)
        {

RoleCtrl同理,都改成AppDebug了,打包后不会输出日志,不输出也可能有损耗,AppDebug里面其实甚至是打包后不执行,坐等明天报告

这根本不是内存泄漏,也不是GC Alloc的问题,这其实就是我本身代码逻辑写错了而已

要是你还是坚持想找到 Unity的内存漏洞,C#的内存问题,Mono的内存Bug,我建议你去造台特斯拉还应该花的时间比较少

(补)

 

也想做做实验看看自己能不能模拟出一个GC Allocted问题

======================================================================

事实是,如果你如果想着考100分,那么你顶多只得60分,如果你像得到100分,那么你至少要以200分的标准要求自己

聊GC的时候,我想先来看看人家的标准

为什么我们标准总是人家的好,为什么我们不能接近标准又却妄想达标,未学会跑得先学会走,是这个道理吧

                                                            一个来自DoTween插件的标准:

Features

Speed and efficiency 效能

Not only very fast, but also very efficient: everything is cached and reused to avoid useless GC allocations.

IntelliSense and type-safety 类型安全,据说多线程有用

All code is complete with XML comments and organized to get the most out of IntelliSense. Also, everything is type-safe: no strings anywhere.

Shortcuts 短写,想搞GC前先想想自己的代码是不是以one,name,chengshi等命名

Shortcut extensions that directly extend common objects like this:

// Move a transform to position 1,2,3 in 1 second
								transform.DOMove(new Vector3(1,2,3), 1);
								// Scale the Y of a transform to 3 in 1 second
								transform.DOScaleY(3, 1);
								// Pause a transform's tween
								transform.DOPause();

Extremely accurate 精准,也就是3D的定位和每一帧计算逻辑完全正确

Time is calculated in a very precise way. This means that 1000 loops of 1 second each will play exactly as long as a single loop of 1000 seconds.

Logical and easy to use API API

An API made to boost efficiency, intuitiveness and ease of use.

Animate everything (almost)

DOTween can animate every numeric value and also some non-numeric ones. It can even animate strings, with support for rich-text.

Modules 模块化

You can activate/deactivate references to Unity's systems (physics/audio/UI/etc) via DOTween Modules.

Snapping, axis constraints and other options

Choose additional options on how to tween your values, like snapping (snaps values to integers) or axis constraints.

Full control 提供多种调用方法

Play, Pause, Rewind, Restart, Complete, Goto and tons of other useful methods to control your tweens.

Grouping

Combine tweens into Sequences to create complex animations (which don't need to be in a, uh, sequence: they can also overlap each other).

Blendable tweens tween动画的融合,这个真心比较吊,非线性编辑器了解一下

Some tweens can blend between each other in realtime, thanks to powerful DOBlendable shortcuts.

Paths

Animate stuff along both linear and curved paths, with additional options for the orientation of your traveling agents.

Change values and duration while playing

Change a tween's start/end values or duration at any moment, even while playing.

Safe mode

Activate the optional safe mode and let DOTween take care of unexpected occurrences, like a tween's target being destroyed while playing.

Yield for coroutines

Various "WaitFor…" methods to use inside coroutines, that allow you to wait for a tween to be completed, killed, started, or for it to reach a given position or loops (and if you have Unity 5.3 or later, CustomYieldInstructions are also implemented).

Multiple rotation modes

Rotation tweens can take the shortest route, the full one, or use local or world-based axes.

Shared methods

Is it a Tweener or a Sequence? Who cares? They both inherit from Tween, thus you can store them and control them in the same way.

Plugins

DOTween is built with an extensible architecture in mind, which allows you to create your own tween plugins as separate files.

Extras

Extra virtual methods to do stuff like calling a function after a given delay.

All the basics

Callbacks, Loops, Ease (AnimationCurves and custom ease functions included), SpeedBased and many other tweening options. Also, choice of update type: regular, fixed, late, plus options to make it timeScale-independent.

 

太多了,后面翻译不过来了,但其实DOTWEEN也是代码啊,为什么人家就这么写代码,而我们就能调用的SendMessage,OnUpdate,Callback就以为自己会写代码了??

但你真的会写代码的时候,你就会自动自觉,自己去研究GC的了,无需操心

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

avi9111

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值