Memory considerations when building WEBGL targets

Source: Internet
Author: User

Memory Considerations when targeting WebGL

Build WebGL memory considerations at the target

Memory in Unity WebGL can a constraining factor restricting the complexity of the content can run, so we would like To provide some explanation on what memory is used in WebGL.

For unity WebGL, memory limits the complexity of the content being run. Below we will explain how memory is used in WebGL.

Your WebGL content would run inside a browser, so any memory have to is 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 A-Z or a-bit process, whether the browser uses separate processes for Each tab or have 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.

Because WebGL content is run in the browser, the required memory is allocated from the browser's memory space. Depending on the browser, operating system, and device used, the memory available for WEBGL content may vary considerably. The determinants include whether the browser is 32-bit or 64-bit, whether the browser uses a separate process for each tab, whether the WebGL content and other tabs use shared memory, and how much memory the browser JavaScript engine needs to parse the WebGL content code.

There is multiple areas where Unity WebGL content would require the browser to allocate significant amounts of memory:

In the following areas, the Unity WebGL content requires the browser to allocate a large amount of memory:

Unity Heap

Unity Heap Memory

The memory Unity uses to store all it's state, managed and native objects and currently loaded assets and scenes. This was 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 TOT Al_memory value written to the generated HTML file). You can use the Unity Profiler to profiles and sample the contents of this memory. This memory is created as a TypedArray of bytes in JavaScript code, and requires the browser is able to allocate a CO Nsecutive block of memory of this size. You'll want this space to being as small as possible (so, the browser would be able to allocate it even if memory are FRA gmented), but large enough to fit all the data required to play any scene of your content.

For these memory, unity is used to store all its states, managed native objects, and currently loaded resource scenarios. This is similar to the memory used by the unity player on any other platform. You can configure the size of the memory in the Unity WebGL Player settings (for quick iterations, you can also edit the total_memory value of the HTML file that was written to the build). You can also use Unity Profiler to sample and debug the contents of this memory. The memory is created in the form of Typedarray bytes of JavaScript code and requires the browser to allocate contiguous blocks of memory by its size. We want the space to be as small as possible (so that the browser can still allocate the space when the memory is fragmented), but still enough to hold the data needed to play any content scene.

Asset Data

Resource data

When you create a unity WebGL build, Unity would write out a. Data file containing all the scenes and assets needed for you R content. Since WebGL does not has a real file system, this file would be downloaded before your content can start, and the Uncompre Ssed data kept in a consecutive blocks 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 in reducing File size for information on how to optimize the build size of your assets.

When you create a unity WebGL build, Unity will output a. data file that contains all the scenarios and resources required for WEBGL content. Since WebGL does not have a real file system, the. data file needs to be loaded before starting to run the content, and the uncompressed files are kept in contiguous browser memory blocks all the time the content is running. To reduce download time and memory consumption, you should make this data as small as possible. See document reducing file size for information about optimizing the size of your resource build.

Another thing you can does to reduce load times and the amount of memory used for assets are to packs your asset data into the Etbundles. By doing so, you get the full control of when your assets need to be downloaded, and can unload them if you no longer NE Ed them, which would free any memory used by them. Note that assetbundles'll be loaded directly into the Unity heap and would not result in additional allocations by the BR Owser (unless you use Asset Bundle Caching using WWW. Loadfromcacheordownload, which is using a memory-mapped Virtual File System, backed by the browser ' s IndexedDB).

There is another way to reduce the load time and memory footprint of a resource, which is to package resource data into Assetbundles. By doing so, you have complete control over when to load resources and unload them when they are not needed to free up the memory they occupy. Note that the assetbundles will be loaded directly into the unity heap memory without causing additional memory allocations from the browser (unless you are using WWW. Loadfromcacheordownload uses asset Bundle caching technology, which uses a memory-mapped virtual file system based on a browser-indexed database.

Memory needed to parse the code

The memory required to parse the code

Another issue related to memory are the memory required by the browser ' s JavaScript engine. Unity would 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 by several gigabytes of memory when loading the content in some cases. We expect that technologies like WebAssembly would eventually eliminate this problem, but until then, the best Advis E We can give is to do and you can to keep the size of the emitted code low. See the comments in distribution size here for more information on what to do.

Another memory-related issue is the memory required by the browser's JavaScript engine. Unity generates very large files that contain millions of lines of JavaScript code that are much larger than the amount of code that is typically run in a browser. Some JavaScript engines allocate a fairly large data structure to parse and optimize the code, and in some cases may cause the memory spike to rise to thousands of megabytes when loading content. We hope that future technologies such as webassembly will eventually solve this problem. But until then, the best advice we can give is to do what you can to reduce the amount of code released. Please click here to view the comments related to the release size for more information on solving the problem.

Dealing with memory issues

Coping with memory problems

When you see an error related to memory in a Unity WebGL build, it's important to understand whether it's the browser wh Ich is failing to allocate memory or if the Unity WebGL runtime was failing to allocate a free block of memory within the P Re-allocated block of the Unity heap. If The browser is failing to allocate memory and then it could help 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 was failing to allocate a block inside the Unity heap, you may want to increase the Size of that instead.

When you see memory-related errors in the Unity WebGL build, it's important to differentiate between the following two points, whether the browser cannot allocate memory or if the Unity WebGL Runtime cannot allocate free memory in a pre-allocated unity heap memory block. If the browser is unable to allocate memory, it may be helpful to try to reduce the size used by the memory area above (for example, to reduce the size of the unity heap memory). On the other hand, if the unity runtime is unable to allocate memory blocks in the unity WebGL heap memory, you can increase the heap memory size accordingly.

Unity would try to interpret error messages-to-tell which of the-it is (and provide-suggestions on-what). Since different browsers may report different messages, that's not always easy, however, and we'll be interpreting a ll of them. When you see a generic ' Out of memory ' error from the browser, it's likely to being an issue of the browser running out of M Emory (where you might want to use a smaller Unity heap). Also, sometimes see browsers simply crashing when loading Unity content without showing a human-parseable error me Ssage. This can has many reasons, but was frequently caused by JavaScript engines requiring too much memory to parse and optimize The generated code.

Unity will attempt to parse the browser error message to tell you which one of the two errors above (and how to respond to the recommendations). Because different browsers may report different error messages, sometimes parsing is difficult and cannot be fully analyzed. When you see an error like "Out of memory" in your browser, it could be a problem with the browser running out of memory (you might want to use a smaller unity heap memory). In addition, you sometimes encounter a browser crash when loading unity content without displaying a readable error message. There are a number of possible reasons, but usually it is because the JavaScript engine requires too much memory to parse and optimize the resulting code.

Garbage Collection Considerations

Garbage collection Mechanism considerations

When you allocate managed objects in Unity, they would need to being garbage collected when they is no longer used. See we documentation on automatic memory management for more information. In WebGL, this is the same. Managed, Garbage collected memory is allocated inside the Unity heap.

For managed objects in unity, garbage collection is required when they are no longer in use. Check the documentation for automatic memory management for more information. In WebGL, there is the same mechanism. Managed garbage collection memory is allocated in the Unity heap memory.

One distinction in WebGL, however, concerns the points in time time 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 regist ERs for loaded object references. This isn't currently possible in JavaScript. For this reason, the GC would only have run in WebGL in situations where the stack was known to being empty (which is currently once After every frame). This isn't a problem for most content which deals with managed memory conservatively and have relatively few GC allocation s within each frame (you can debug this using the Unity Profiler).

One difference in WebGL is the point in time at which garbage collection (GC) occurs. In order to implement garbage collection, GC typically needs to pause all running threads, check the stack, and register loaded object references. This is not yet possible in JavaScript. As a result, the GC runs in WebGL only if the stack is empty (there is now one at a time after each frame). This is not an issue for conservative processing of managed memory and for the relatively small number of GC allocations in each frame (you can debug with the Unity Parser).

However, if you had code like the following:

However, if your code looks like this:

String hugestring = "";

for (int i = 0; i < 100000; i++)

{

hugestring + = "Foo";

}

, then this code would fail running in WebGL, as it would not get a chance to run the GC between iterations of the loop, T o Free up memory used by all the intermediate string Objects-which would eventually cause it-run out of memory in the Unity Heap.

The code will not run in WebGL. Because there is no opportunity in the for loop to run the garbage collection mechanism to free up memory used by all intermediate string objects-these string objects will eventually cause the code to light up the unity heap memory.

Link:

Https://docs.unity3d.com/540/Documentation/Manual/webgl-memory.html

Memory considerations when building WEBGL targets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.