使用mono進行ios開發也有一年了,一直有個頭疼的問題是閃退,而且閃退的時候並沒有拋出明確的錯誤。
前兩天在調試一個bug的時候,在序列化的時候又莫名其妙的閃退,後來在一位大神(博客地址)的指導下,發現了解決方案!
遇到這種閃退,一般在Application output中輸出錯誤如下:
monoeg_g_log + get_numerous_trampoline + ==================================================================================================================================
而且這種錯誤是隨機的,有時候正常運行,有時候不正常,在上文輸出錯誤內容裡我們看到trampoline這個單詞,而第一句monoeg_g_log 這句話是說系統在嘗試記錄錯誤到錯誤日志,基於這種情況下,我們可以判定這是mono的默認蹦床(trampoline)數小於了你應用需要的蹦床數。
關於蹦床數的解釋,這裡Rolf Bjarne Kvinge給了相應的解釋:相應鏈接
On device we generate all the necessary code at build time in a process known as Ahead of Time compilation (similar to Microsoft's ngen), because we're not allowed to jit code on devices. Unfortunately there are a few things that cannot be determined statically - for instance generic interfaces might need different vtables depending on which type the interface is instantiated with. (For this case it is technically possible to determine the maximum number of vtables, but the number would be potentially enormous - multiply the number of generic interfaces times the number of types in your app...). We cannot allocate memory for these vtables dynamically at runtime, so we've picked a reasonable default and allow the user to increase this value if they run into issues. This is the basic theory for the trampolines (the exact problem is a bit different, depending on the type of trampolines, but that's not really important). So you can add as many trampolines as you want, but memory usage will increase. That's also all there is to it: the app will not get slower (unless if the increased memory usage causes it to run slower, due to out-of-memory warnings, etc). It also means that you only have to increase the number of trampolines of the type you're actually having problems with, if you increase the others you'll increase the size of your executable needlessly.
大體意思是(英語不大好,盡力翻譯了):
因為mono不允許在蘋果設備上即時編譯代碼,所以在編譯的時候mono會通過AOT編譯技術直接編譯為ARM匯編代碼。但在編譯的時候仍有一些無法靜態確定的事情:例如泛型接口可能需要不同的虛擬表(運行時存放執行方法的集合),這取決於接口被實例化時的類型。(對於這種情況,從技術上講確定虛擬表的最大數量是可能的,但是這數量可能會是龐大的--
看完這個,我想你對蹦床數有了一定了解,那如何設置呢!
打開xamarin studio ,在項目文件上右鍵option,展開下圖,在arguments參數的地方,輸入:-aot "nrgctx-trampolines=4096" -aot "nimt-trampolines=4096" -aot "ntrampolines=4096"
請看下圖:
下面進行翻譯一下:
Ran of trampolines of type
of trampolines of type
of trampolines of type