Memory Considerations when targeting WebGL
構建WebGL目標時的內存考量
Memory in Unity WebGL can be a constraining factor restricting the complexity of the content you can run, so we would like to provide some explanation on how memory is used in WebGL.
對於Unity WebGL來說,內存限制了所運行內容的復雜度。下面我們將解釋一下在WebGL中內存是如何被使用的。
Your WebGL content will run inside a browser, so any memory has to be allocated by the browser within the browser’s memory space. The amount of available memory can vary a lot depending on the browser, OS and device used. Determining factors include whether the browser is a 32 or 64 bit process, whether the browser uses separate processes for each tab or has your content share a memory space with all other open tabs, and how much memory the browser’s JavaScript engine requires to parse your code.
因為WebGL內容是運行於瀏覽器當中的,所以其所需內存都是從瀏覽器內存空間里分配的。根據所用瀏覽器、操作系統和設備的不同,WebGL內容可用的內存可能會差別很大。決定因素包括:瀏覽器是32位的還是64位的,瀏覽器為每個標簽頁使用獨立的進程還是讓WebGL內容和其它標簽頁使用共享的內存,以及瀏覽器JavaScript引擎解析WebGL內容代碼需要多少內存。
There are multiple areas where Unity WebGL content will require the browser to allocate significant amounts of memory:
在以下幾個方面,Unity WebGL內容要求瀏覽器分配大量內存:
Unity Heap
Unity堆內存
This is the memory Unity uses to store all it’s state, managed and native objects and currently loaded assets and scenes. This is similar to the memory used by Unity players on any other platform. You can configure the size of this in the Unity WebGL player settings (But for faster iteration, you can also edit the TOTAL_MEMORY value written to the generated html file). You can use the Unity Profiler to profile and sample the contents of this memory. This memory will be created as a TypedArray of bytes in JavaScript code, and requires the browser be able to allocate a consecutive block of memory of this size. You will want this space to be as small as possible (so that the browser will be able to allocate it even if memory is fragmented), but large enough to fit all the data required to play any scene of your content.
對於這些內存,Unity用於存儲其所有狀態、托管的原生對象和當前加載的資源場景。這類似於Unity播放器在其他任何平台上所使用的內存。可以在Unity WebGL播放器設置中配置該內存的大小(為了快速迭代,也可以編輯已寫入生成的html文件的TOTAL_MEMORY值)。您也可以使用Unity Profiler對該內存的內容進行抽樣和調試。該內存以JavaScript代碼的TypedArray字節形式被創建,並要求瀏覽器能夠按其大小分配連續的內存塊。我們希望該空間盡可能小(這樣當內存零碎時瀏覽器仍然能夠分配該空間),但仍足以容納播放任何內容場景時所需的數據。
Asset Data
資源數據
When you create a Unity WebGL build, Unity will write out a .data file containing all the scenes and assets needed for your content. Since WebGL does not have a real file system, this file will be downloaded before your content can start, and the uncompressed data will be kept in a consecutive block of browser memory for the whole time your content is run. So, to keep both download times and memory usage low, you should try to keep this data as small as possible. See the documentation page on Reducing File size for information on how to optimize the build size of your assets.
當您創建Unity WebGL構建時,Unity將會輸出一個.data文件,其包含WebGL內容所需的所有場景和資源。由於WebGL沒有真實的文件系統,所以在開始運行內容之前,.data文件需要先被加載,同時,在內容運行的所有時間里,這些未壓縮的文件將一直被保存在連續的瀏覽器內存塊里。為了降低下載時間和內存占用,您應該讓這些數據盡可能小。請參見文檔降低文件大小,獲得優化資源構建大小的相關信息。
Another thing you can do to reduce load times and the amount of memory used for assets is to pack your asset data into AssetBundles. By doing so, you get full control of when your assets need to be downloaded, and you can unload them when you no longer need them, which will free any memory used by them. Note that AssetBundles will be loaded directly into the Unity heap and will not result in additional allocations by the browser (unless you use Asset Bundle Caching using WWW.LoadFromCacheOrDownload, which is using a memory-mapped Virtual File System, backed by the browser’s IndexedDB).
有另外一個方法可以降低資源的加載時間和內存占用,其是將資源數據打包進AssetBundles。這樣做,您便能完全控制什么時候加載資源,以及在不需要的時候卸載它們以釋放它們所占用的內存。請注意,AssetBundles會被直接加載到Unity堆內存中,而不會造成瀏覽器的額外內存分配(除非您通過WWW.LoadFromCacheOrDownload使用Asset Bundle Caching技術,該技術采用了基於瀏覽器索引數據庫的內存映射虛擬文件系統)。
Memory needed to parse the code
解析代碼所需的內存
Another issue related to memory is the memory required by the browser’s JavaScript engine. Unity will emit very large files of millions of lines of generated JavaScript code, which is an order of magnitude larger than common uses of JavaScript code in browsers. Some JavaScript engines may allocate some rather large data structures to parse and optimize this code, which may results in memory spikes of up to several Gigabytes of memory when loading the content in some cases. We expect that future technologies like WebAssembly will eventually eliminate this problem, but until then, the best advise we can give is to do what you can to keep the size of the emitted code low. See the comments on distribution size here for more information on how to do that.
跟內存相關的另一個問題是瀏覽器JavaScript引擎所需的內存。Unity會生成包含數以百萬行JavaScript代碼的非常大的文件,其數量級比瀏覽器里通常運行的代碼量可要大得多。一些JavaScript引擎會分配相當大的數據結構以解析和優化這些代碼,某些情況下可能導致加載內容時內存峰值上升到幾千兆字節。我們希望未來諸如WebAssembly之類的技術能最終解決這個問題。但在那之前,我們能給的最好建議是盡您所能以降低發行的代碼量。請點擊這里以查看跟發布大小相關的評論,獲得解決問題的更多信息。
Dealing with memory issues
應對內存問題
When you see an error related to memory in a Unity WebGL build, it is important to understand whether it is the browser which is failing to allocate memory or if the Unity WebGL runtime is failing to allocate a free block of memory within the pre-allocated block of the Unity heap. If the browser is failing to allocate memory, then it may help to try to reduce the size used by one or more of the memory areas above (for instance by reducing the size of the Unity heap). On the other hand, if the Unity runtime is failing to allocate a block inside the Unity heap, you may want to increase the size of that instead.
當您在Unity WebGL構建當中看到內存相關的錯誤,區分以下兩點很重要,是瀏覽器無法分配內存,還是Unity WebGL運行時無法在預先分配好的Unity堆內存塊中分配空閑內存。如果是瀏覽器無法分配內存,那么嘗試降低以上內存區域所使用的大小可能會有幫助(比如降低Unity堆內存的大小)。另一方面,如果是Unity運行時無法在Unity WebGL堆內存中分配內存塊,則可以相應增加堆內存的大小。
Unity will try to interpret error messages to tell which of the two it is (and provide suggestions on what to do). Since different browsers may report different messages, that is not always easy, however, and we may not be interpreting all of them. When you see a generic “Out of memory” error from the browser, it is likely to be an issue of the browser running out of memory (where you might want to use a smaller Unity heap). Also, you may sometimes see browsers simply crashing when loading Unity content without showing a human-parseable error message. This can have many reasons, but is frequently caused by JavaScript engines requiring too much memory to parse and optimize the generated code.
Unity會嘗試分析瀏覽器錯誤信息以告訴您是以上兩個錯誤當中的哪個(並給出怎么應對的建議)。因為不同瀏覽器可能會報不同的錯誤信息,所以有時分析很困難,無法完全分析。當您看到瀏覽器產生“內存不足”之類的錯誤時,可能是瀏覽器內存用光造成的問題(這時您可能想使用小一些的Unity堆內存)。此外,您有時會碰到加載Unity內容時瀏覽器直接崩潰,而沒有顯示可閱讀的錯誤信息。有很多可能原因,但通常是因為JavaScript引擎需要過多的內存以分析和優化產生的代碼所導致的。
Garbage Collection considerations
垃圾回收機制考量
When you allocate managed objects in Unity, they will need to be garbage collected when they are no longer used. See our documentation on automatic memory management for more information. In WebGL, this is the same. Managed, garbage collected memory is allocated inside the Unity heap.
對於Unity中的托管對象,當它們不再被使用時,需要做垃圾回收處理。請查看文檔自動內存管理,獲取更多信息。在WebGL中,也有同樣的機制。托管的垃圾回收內存分配於Unity堆內存中。
One distinction in WebGL, however, concerns the points in time when garbage collection (GC) can take place. To perform garbage collection, the GC would normally need to pause all running threads and inspect their stacks and registers for loaded object references. This is not currently possible in JavaScript. For this reason, the GC will only run in WebGL in situations where the stack is known to be empty (which is currently once after every frame). This is not a problem for most content which deals with managed memory conservatively and has relatively few GC allocations within each frame (you can debug this using the Unity profiler).
WebGL中的一個區別在於垃圾回收(GC)發生的時間點。為了實施垃圾回收,GC通常需要暫停所有運行中的線程、檢查堆棧和注冊已加載的對象引用。而目前這在JavaScript中還不能實現。由此,僅當堆棧為空(目前在每一幀后有一次)的時候GC才會在WebGL中運行。對於保守處理托管內存和在每幀中GC分配相對較少的大多數WebGL內容,這不是問題(您可以使用Unity分析器進行調試)。
However, if you had code like the following:
然而,如果您的代碼看起來如下:
string hugeString = "";
for (int i = 0; i < 100000; i++)
{
hugeString += "foo";
}
, then this code would fail running on WebGL, as it would not get a chance to run the GC between iterations of the loop, to free up memory used by all the intermediate string objects - which would eventually cause it to run out of memory in the Unity heap.
則該代碼在WebGL中將無法運行。因為在for循環中沒有機會運行垃圾回收機制以釋放所有中間字符串對象所使用的內存——這些字符串對象最終會導致代碼用光Unity堆內存。
鏈接:
https://docs.unity3d.com/540/Documentation/Manual/webgl-memory.html
