打破双亲委派么,怎么打破_打破统一垃圾收集规则
打破雙親委派么,怎么打破
Lancelot was always looking for bigger and
蘭斯洛特一直在尋找更大的
大標題 (bigger titles)
to work on. And so he worked hard to get his big chance in the games industry.
繼續努力。 因此,他努力工作,以獲得在游戲行業的巨大機會。
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.
游戲行業中的這些位置過去是,現在仍然是為少數游戲程序員保留的。 而且他不確定自己是否會達到標準。
But he persisted and continued to sharpen his programming skills.
但是他堅持并繼續提高他的編程技巧。
Lancelot started working on small games. Maybe at some point, he would get the big chance he was looking for, he thought.
蘭斯洛特開始從事小型游戲。 他想,也許在某個時候,他會得到他尋找的巨大機會。
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
幾年過去了,直到他獲得了等待的機會。 他被要求將大型VR游戲移植到移動平臺上。 像蘭斯洛特一樣興奮,他不停地想知道自己是否
夠好了 (good enough)
for the task. It was daunting but he accepted the challenge. He knew that he could only grow out of that.
為任務。 這令人生畏,但他接受了挑戰。 他知道他只能從中成長。
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
蘭斯洛特最大的擔憂是需要大幅提高游戲性能。 實際上,這是雙重挑戰。 他必須
將性能提高20% (improve performance by 20%)
for a considerably
相當大的
功能較弱的平臺 (less powerful platform)
.
。
After months of non-stop work, he finally managed to optimize the game enough to have a rock-solid performance baseline.
經過幾個月的不間斷工作,他終于設法對游戲進行了足夠的優化,以達到堅實的性能基準。
However, an unexpected issue was just around the corner…
然而,一個意想不到的問題迫在眉睫……
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.
統一的分析器向他顯示,每隔幾秒鐘便有相當大的幀速率下降。 這讓他感到擔心,因為那將不允許他發布游戲。 游戲發行受到威脅。 那使他完全不舒服。
Based on his previous experience, Lancelot quickly suspected of the
根據他以前的經驗,蘭斯洛特很快就懷疑
垃圾收集器 (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.
。 畢竟,他知道在游戲中過多分配臨時內存可能會導致這些性能峰值。 就像每個人廚房中的垃圾桶一樣,當垃圾量達到其容量的80%時,就該清理它了。
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…
因此,他花了幾天的時間來解決煩人的內存分配問題。 他執行了他能想到的所有類型的優化。 對象池,數據緩存,數據結構優化…
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.
這些天的花費優化使他在性能旅程中遙遙領先。 蘭斯洛特為自己的工作感到驕傲,但直到他看到垃圾收集器每15秒仍在運行時,他的擔憂才會增加。
Playing the game in VR with these performance drops would somehow make people look pale.
在這些性能下降的情況下在VR中玩游戲會使人顯得蒼白。
?How could that be happening?? he wondered.
?怎么會這樣?? 他想知道。
With more patience and digging, Lancelot discovered a second source of memory allocations he didn't see before. Those happened in a
經過更多的耐心和挖掘,Lancelot發現了他之前從未見過的第二個內存分配來源。 那些發生在
第三方圖書館 (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
他看了一下,很快就意識到自己處于有史以來最糟糕的位置:該庫是封閉源代碼。 不僅如此,他還嘗試使用Unity的
增量垃圾收集器 (incremental garbage collector)
but he couldn't afford to pay its performance price.
但他負擔不起性能價格。
Lancelot was running out of options.
Lancelot的選項已用完。
He felt desperate but managed to keep calm. He's been in worse situations after all.
他感到絕望,但設法保持鎮定。 畢竟他一直處在更糟糕的情況下。
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.
庫并自己進行內存分配優化。 問題是許可證不允許這種事情。 而且他還太年輕,無法入獄。
The second option he considered was to
他考慮的第二個選擇是
預分配 (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.
堆上有很多內存。 他知道,當堆使用率達到一定百分比時,團結會觸發垃圾回收過程。 因此增加堆應該給他更多的時間來進行垃圾回收。
Sadly, that was still not enough.
可悲的是,這還不夠。
It slowly felt as if he had no control over the situation. It was tough, but again, he
慢慢地,他覺得自己無法控制局勢。 很難,但再次,他
堅持 (persisted)
.
。
So Lancelot came up with a
所以蘭斯洛特想出了一個
瘋狂的主意 (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.
。 如果他完全禁用垃圾收集怎么辦? 那有可能嗎? 他覺得自己的想法多么危險。 他不想增加游戲崩潰的可能性。 上一次他檢查時,對于球員來說這并不有趣。 也許時代確實發生了變化,但安全勝過遺憾。
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.
最重要的是,他擔心推遲游戲的發布。 他不想讓他的球員在圣誕節錯過這個冠軍。 他記得在這些假期里玩EverQuest有多少樂??趣。 他不會從球員身上奪走這些。
Reached this point, he had no other choice than to disable the garbage collector.
達到這一點,他除了禁用垃圾收集器外別無選擇。
He got into research mode and found that he could indeed
他進入研究模式,發現他確實可以
手動禁用垃圾收集 (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.
。 他進行了數十次實驗,以觀察游戲在不耗盡內存的情況下能保持多長時間。 他做了各種各樣的測試來強調系統。 單擊各處,四處走動,在不同的應用程序之間切換。
號碼 (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.
開始進入他的電子表格:25分鐘,28分鐘,30分鐘……他還指出了堆使用量隨時間增加的方式,以確保他永遠不會超出安全預算。
With those numbers, Lancelot established a generous
有了這些數字,蘭斯洛特建立了一個慷慨的
安全裕度 (safe margin)
and prepared a
并準備了
原型 (prototype)
. He would run the garbage collection manually during loading screens and every few minutes.
。 他將在加載屏幕期間以及每隔幾分鐘手動運行垃圾收集。
He had hope again.
他又有了希望。
He politely asked QA to go through the game dozens of times.
他彬彬有禮地要求質量檢查人員進行數十遍游戲。
Memory was always within the budget. No crashes. No side effects.
內存總是在預算之內。 沒有崩潰。 沒有副作用。
This long journey brought him to the point where he was able to
漫長的旅程將他帶到了他能夠
船 (ship )
the game.
游戲。
And guess what? Hundreds of players are now enjoying it over the Christmas period.
你猜怎么著? 在圣誕節期間,數百名玩家現在正在享受它。
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.
一開始,他對這種解決方案不滿意。 這是一個冒險的舉動,他知道。 但是他設法做到了。
Lancelot learned to be comfortable with the
蘭斯洛特學會了適應
不舒服 (uncomfortable)
. He learned to be more
。 他學會了變得更多
務實 (pragmatic)
. Because there are times when a programmer has to be.
。 因為有時候必須要有程序員。
Does anything of the story ring a bell? If so, your intuition is probably right.
故事有什么響嗎? 如果是這樣,您的直覺可能是正確的。
That programmer was me.
那個程序員就是我。
For the times you need it, this is how you can manage the garbage collector:
在您需要的時候,這是管理垃圾收集器的方式:
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).
該代碼段向您展示了如何禁用自動垃圾收集。 它每分鐘以及可能在屏幕轉換(淡入黑色)期間手動運行GC過程。
Be aware of its possible side effects:
請注意其可能的副作用:
崩潰 (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
:如果您的游戲不夠安全,則會耗盡內存。 更糟糕的是,當您在應用程序之間切換時,操作系統可能會殺死您的游戲
更長的垃圾收集時間 (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:
如果您需要產生大量的垃圾,這是一個可以使用的簡單方法:
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:
這是您將在探查器中獲得的內容:
Unity Garbage Collection: Time-Based Manual TriggerUnity垃圾收集:基于時間的手動觸發器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.
在那里,您看到內存使用率在增加。 不斷增加的堆使用量突出顯示為“ mono”。 對我們來說幸運的是,我們每3秒運行一次手動垃圾收集器。
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.
您可以在探查器圖中清楚地看到此垃圾生成清除周期。 對于那些研究物理的游戲開發人員,您可能會認為它是鋸齒波形。
If you want the source code of this project, you know where to find it (spoiler: here).
如果您想要該項目的源代碼,則知道在哪里可以找到它(擾流器:此處)。
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.
有關更常規的內存優化,您可能對Unity可尋址對象感興趣。 使用可尋址對象,您可以減少總內存使用量,從而可以更不頻繁地觸發垃圾回收。 反過來,這將減少您的播放器會遇到的性能峰值。
I'm looking forward to working with you all in 2020.
我期待在2020年與大家一起工作。
Ruben
魯本
翻譯自: https://habr.com/en/post/484710/
打破雙親委派么,怎么打破
總結
以上是生活随笔為你收集整理的打破双亲委派么,怎么打破_打破统一垃圾收集规则的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [SQL]将子查询作为查询条件
- 下一篇: 精力管理-健康生活 高效工作