打破双亲委派么,怎么打破_打破统一垃圾收集规则
打破雙親委派么,怎么打破
Lancelot was always looking for bigger and
蘭斯洛特一直在尋找更大的
大標(biāo)題 (bigger titles)
to work on. And so he worked hard to get his big chance in the games industry.
繼續(xù)努力。 因此,他努力工作,以獲得在游戲行業(yè)的巨大機(jī)會(huì)。
It was not easy, that he knew.
他知道這并不容易。
These spots in the game industry were and still are reserved for a small minority of game programmers. And he wasn't sure he'd be up to the standards, anyway.
游戲行業(yè)中的這些位置過(guò)去是,現(xiàn)在仍然是為少數(shù)游戲程序員保留的。 而且他不確定自己是否會(huì)達(dá)到標(biāo)準(zhǔn)。
But he persisted and continued to sharpen his programming skills.
但是他堅(jiān)持并繼續(xù)提高他的編程技巧。
Lancelot started working on small games. Maybe at some point, he would get the big chance he was looking for, he thought.
蘭斯洛特開(kāi)始從事小型游戲。 他想,也許在某個(gè)時(shí)候,他會(huì)得到他尋找的巨大機(jī)會(huì)。
Years passed till he got the opportunity he was waiting for. He was asked to port a big VR game to a mobile platform. As excited as Lancelot was, he couldn't stop wondering if he was
幾年過(guò)去了,直到他獲得了等待的機(jī)會(huì)。 他被要求將大型VR游戲移植到移動(dòng)平臺(tái)上。 像蘭斯洛特一樣興奮,他不停地想知道自己是否
夠好了 (good enough)
for the task. It was daunting but he accepted the challenge. He knew that he could only grow out of that.
為任務(wù)。 這令人生畏,但他接受了挑戰(zhàn)。 他知道他只能從中成長(zhǎng)。
Lancelot's biggest concern was the need to massively improve the performance of the game. It was a double challenge, in fact. He had to
蘭斯洛特最大的擔(dān)憂是需要大幅提高游戲性能。 實(shí)際上,這是雙重挑戰(zhàn)。 他必須
將性能提高20% (improve performance by 20%)
for a considerably
相當(dāng)大的
功能較弱的平臺(tái) (less powerful platform)
.
。
After months of non-stop work, he finally managed to optimize the game enough to have a rock-solid performance baseline.
經(jīng)過(guò)幾個(gè)月的不間斷工作,他終于設(shè)法對(duì)游戲進(jìn)行了足夠的優(yōu)化,以達(dá)到堅(jiān)實(shí)的性能基準(zhǔn)。
However, an unexpected issue was just around the corner…
然而,一個(gè)意想不到的問(wèn)題迫在眉睫……
The unity profiler revealed him a considerable frame-rate drop every few seconds. And that worried him because that wouldn't allow him to ship the game. The game release was in jeopardy. That made him utterly uncomfortable.
統(tǒng)一的分析器向他顯示,每隔幾秒鐘便有相當(dāng)大的幀速率下降。 這讓他感到擔(dān)心,因?yàn)槟菍⒉辉试S他發(fā)布游戲。 游戲發(fā)行受到威脅。 那使他完全不舒服。
Based on his previous experience, Lancelot quickly suspected of the
根據(jù)他以前的經(jīng)驗(yàn),蘭斯洛特很快就懷疑
垃圾收集器 (garbage collector)
. After all, he knew that allocating temporary memory in the game too often could cause these performance spikes. Just like the trash can in everyone's kitchen, you know it's time to clean it when it reaches 80% of its capacity.
。 畢竟,他知道在游戲中過(guò)多分配臨時(shí)內(nèi)存可能會(huì)導(dǎo)致這些性能峰值。 就像每個(gè)人廚房中的垃圾桶一樣,當(dāng)垃圾量達(dá)到其容量的80%時(shí),就該清理它了。
And so he spent days fighting the bothersome memory allocations. He performed all types of optimizations he could think of. Object pools, data caching, data structure optimizations…
因此,他花了幾天的時(shí)間來(lái)解決煩人的內(nèi)存分配問(wèn)題。 他執(zhí)行了他能想到的所有類(lèi)型的優(yōu)化。 對(duì)象池,數(shù)據(jù)緩存,數(shù)據(jù)結(jié)構(gòu)優(yōu)化…
Spending these days optimizing got him a bit ahead in the performance journey. Lancelot was proud of his work, but his concerns only grew when he saw the garbage collector still running every 15 seconds.
這些天的花費(fèi)優(yōu)化使他在性能旅程中遙遙領(lǐng)先。 蘭斯洛特為自己的工作感到驕傲,但直到他看到垃圾收集器每15秒仍在運(yùn)行時(shí),他的擔(dān)憂才會(huì)增加。
Playing the game in VR with these performance drops would somehow make people look pale.
在這些性能下降的情況下在VR中玩游戲會(huì)使人顯得蒼白。
?How could that be happening?? he wondered.
?怎么會(huì)這樣?? 他想知道。
With more patience and digging, Lancelot discovered a second source of memory allocations he didn't see before. Those happened in a
經(jīng)過(guò)更多的耐心和挖掘,Lancelot發(fā)現(xiàn)了他之前從未見(jiàn)過(guò)的第二個(gè)內(nèi)存分配來(lái)源。 那些發(fā)生在
第三方圖書(shū)館 (3rd party library)
.
。
He had a look and soon realized he was in the worst position ever: that library was closed source. Not only that, he also tried using Unity's
他看了一下,很快就意識(shí)到自己處于有史以來(lái)最糟糕的位置:該庫(kù)是封閉源代碼。 不僅如此,他還嘗試使用Unity的
增量垃圾收集器 (incremental garbage collector)
but he couldn't afford to pay its performance price.
但他負(fù)擔(dān)不起性能價(jià)格。
Lancelot was running out of options.
Lancelot的選項(xiàng)已用完。
He felt desperate but managed to keep calm. He's been in worse situations after all.
他感到絕望,但設(shè)法保持鎮(zhèn)定。 畢竟他一直處在更糟糕的情況下。
He could
他可以
反向工程 (reverse engineer)
the library and do the memory allocation optimizations himself. The problem was that the license disallowed such things. And he was too young to go to jail.
庫(kù)并自己進(jìn)行內(nèi)存分配優(yōu)化。 問(wèn)題是許可證不允許這種事情。 而且他還太年輕,無(wú)法入獄。
The second option he considered was to
他考慮的第二個(gè)選擇是
預(yù)分配 (pre-allocate)
a lot of memory on the heap. He knew that unity triggered the garbage collection process when the heap usage reached a certain percentage. So increasing the heap should give him more time between garbage collections.
堆上有很多內(nèi)存。 他知道,當(dāng)堆使用率達(dá)到一定百分比時(shí),團(tuán)結(jié)會(huì)觸發(fā)垃圾回收過(guò)程。 因此增加堆應(yīng)該給他更多的時(shí)間來(lái)進(jìn)行垃圾回收。
Sadly, that was still not enough.
可悲的是,這還不夠。
It slowly felt as if he had no control over the situation. It was tough, but again, he
慢慢地,他覺(jué)得自己無(wú)法控制局勢(shì)。 很難,但再次,他
堅(jiān)持 (persisted)
.
。
So Lancelot came up with a
所以蘭斯洛特想出了一個(gè)
瘋狂的主意 (crazy idea)
. What if he disabled the garbage collection entirely? Was that even possible? He felt in his bones how risky such an idea was. He didn't want to add the possibility for the game to crash at unpredictable points. Last time he checked, that was no fun for the players. Maybe times did change, but better safe than sorry.
。 如果他完全禁用垃圾收集怎么辦? 那有可能嗎? 他覺(jué)得自己的想法多么危險(xiǎn)。 他不想增加游戲崩潰的可能性。 上一次他檢查時(shí),對(duì)于球員來(lái)說(shuō)這并不有趣。 也許時(shí)代確實(shí)發(fā)生了變化,但安全勝過(guò)遺憾。
On top of that, he worried about delaying the release of the game. He didn't want his players to miss this title for Christmas. He remembered how much fun he had playing EverQuest during these holidays. He wouldn't take that away from the players.
最重要的是,他擔(dān)心推遲游戲的發(fā)布。 他不想讓他的球員在圣誕節(jié)錯(cuò)過(guò)這個(gè)冠軍。 他記得在這些假期里玩EverQuest有多少樂(lè)??趣。 他不會(huì)從球員身上奪走這些。
Reached this point, he had no other choice than to disable the garbage collector.
達(dá)到這一點(diǎn),他除了禁用垃圾收集器外別無(wú)選擇。
He got into research mode and found that he could indeed
他進(jìn)入研究模式,發(fā)現(xiàn)他確實(shí)可以
手動(dòng)禁用垃圾收集 (manually disable the garbage collection)
. He ran dozens of experiments to see how long the game would hold without running out of memory. He did all sorts of tests to stress the system. Clicking everywhere, walking and jumping around, switching between different applications.
。 他進(jìn)行了數(shù)十次實(shí)驗(yàn),以觀察游戲在不耗盡內(nèi)存的情況下能保持多長(zhǎng)時(shí)間。 他做了各種各樣的測(cè)試來(lái)強(qiáng)調(diào)系統(tǒng)。 單擊各處,四處走動(dòng),在不同的應(yīng)用程序之間切換。
號(hào)碼 (Numbers )
started arriving in his spreadsheet: 25 minutes, 28 minutes, 30 minutes… He also noted how the heap usage increased over time to be certain he'd never exceed a safe budget.
開(kāi)始進(jìn)入他的電子表格:25分鐘,28分鐘,30分鐘……他還指出了堆使用量隨時(shí)間增加的方式,以確保他永遠(yuǎn)不會(huì)超出安全預(yù)算。
With those numbers, Lancelot established a generous
有了這些數(shù)字,蘭斯洛特建立了一個(gè)慷慨的
安全裕度 (safe margin)
and prepared a
并準(zhǔn)備了
原型 (prototype)
. He would run the garbage collection manually during loading screens and every few minutes.
。 他將在加載屏幕期間以及每隔幾分鐘手動(dòng)運(yùn)行垃圾收集。
He had hope again.
他又有了希望。
He politely asked QA to go through the game dozens of times.
他彬彬有禮地要求質(zhì)量檢查人員進(jìn)行數(shù)十遍游戲。
Memory was always within the budget. No crashes. No side effects.
內(nèi)存總是在預(yù)算之內(nèi)。 沒(méi)有崩潰。 沒(méi)有副作用。
This long journey brought him to the point where he was able to
漫長(zhǎng)的旅程將他帶到了他能夠
船 (ship )
the game.
游戲。
And guess what? Hundreds of players are now enjoying it over the Christmas period.
你猜怎么著? 在圣誕節(jié)期間,數(shù)百名玩家現(xiàn)在正在享受它。
In the beginning, he was not comfortable with this solution. It was a risky move and he knew it. But he managed to pull it off.
一開(kāi)始,他對(duì)這種解決方案不滿意。 這是一個(gè)冒險(xiǎn)的舉動(dòng),他知道。 但是他設(shè)法做到了。
Lancelot learned to be comfortable with the
蘭斯洛特學(xué)會(huì)了適應(yīng)
不舒服 (uncomfortable)
. He learned to be more
。 他學(xué)會(huì)了變得更多
務(wù)實(shí) (pragmatic)
. Because there are times when a programmer has to be.
。 因?yàn)橛袝r(shí)候必須要有程序員。
Does anything of the story ring a bell? If so, your intuition is probably right.
故事有什么響嗎? 如果是這樣,您的直覺(jué)可能是正確的。
That programmer was me.
那個(gè)程序員就是我。
For the times you need it, this is how you can manage the garbage collector:
在您需要的時(shí)候,這是管理垃圾收集器的方式:
That code snippet shows you how to disable automatic garbage collections. It runs the GC process manually every minute and possibly during screen transitions (fade to black).
該代碼段向您展示了如何禁用自動(dòng)垃圾收集。 它每分鐘以及可能在屏幕轉(zhuǎn)換(淡入黑色)期間手動(dòng)運(yùn)行GC過(guò)程。
Be aware of its possible side effects:
請(qǐng)注意其可能的副作用:
崩潰 (Crashes)
: if you don't play safe enough you'll run out of memory. Worse, the OS might kill your game when you switch between applications
:如果您的游戲不夠安全,則會(huì)耗盡內(nèi)存。 更糟糕的是,當(dāng)您在應(yīng)用程序之間切換時(shí),操作系統(tǒng)可能會(huì)殺死您的游戲
更長(zhǎng)的垃圾收集時(shí)間 (Longer garbage collection times)
: increasing the heap will make future garbage collections slower
:增加堆將使以后的垃圾收集變慢
If you need to produce generous amounts of garbage, here's a straightforward method that'll just work:
如果您需要產(chǎn)生大量的垃圾,這是一個(gè)可以使用的簡(jiǎn)單方法:
public class GenerousGarbageCreator : MonoBehaviour {[SerializeField] private int garbageCreationRate = 1024;private static int[] _garbage;void Update(){_garbage = new int[garbageCreationRate];} }This is what you'll be getting in the profiler:
這是您將在探查器中獲得的內(nèi)容:
Unity Garbage Collection: Time-Based Manual TriggerUnity垃圾收集:基于時(shí)間的手動(dòng)觸發(fā)器There you see an increasing memory usage. The growing heap usage is highlighted as ?mono?. Luckily for us, we're running the manual garbage collector every 3 seconds.
在那里,您看到內(nèi)存使用率在增加。 不斷增加的堆使用量突出顯示為“ mono”。 對(duì)我們來(lái)說(shuō)幸運(yùn)的是,我們每3秒運(yùn)行一次手動(dòng)垃圾收集器。
You can clearly see this garbage generation-clearance cycle in the profiler graph. For those game developers who studied physics, you might recognize it as a sawtooth wave shape.
您可以在探查器圖中清楚地看到此垃圾生成清除周期。 對(duì)于那些研究物理的游戲開(kāi)發(fā)人員,您可能會(huì)認(rèn)為它是鋸齒波形。
If you want the source code of this project, you know where to find it (spoiler: here).
如果您想要該項(xiàng)目的源代碼,則知道在哪里可以找到它(擾流器:此處)。
For more general memory optimizations, you might be interested in Unity Addressables. With addressables you get to reduce your total memory usage so you can trigger garbage collections less frequently. In turn, this will reduce the performance spikes your players will experience.
有關(guān)更常規(guī)的內(nèi)存優(yōu)化,您可能對(duì)Unity可尋址對(duì)象感興趣。 使用可尋址對(duì)象,您可以減少總內(nèi)存使用量,從而可以更不頻繁地觸發(fā)垃圾回收。 反過(guò)來(lái),這將減少您的播放器會(huì)遇到的性能峰值。
I'm looking forward to working with you all in 2020.
我期待在2020年與大家一起工作。
Ruben
魯本
翻譯自: https://habr.com/en/post/484710/
打破雙親委派么,怎么打破
總結(jié)
以上是生活随笔為你收集整理的打破双亲委派么,怎么打破_打破统一垃圾收集规则的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [SQL]将子查询作为查询条件
- 下一篇: 精力管理-健康生活 高效工作