Unity WebGL内存:Unity堆

本文深入探讨Unity WebGL中的Unity堆,包括其内存结构、托管内存管理、分配和垃圾收集。了解到Unity堆不可收缩,理解如何选择合适的堆大小以避免内存不足。文章还讨论了Unity堆内动态内存、静态内存、未分配内存的分类,以及如何通过Profiler API监控内存使用情况。
摘要由CSDN通过智能技术生成

In the previous Unity WebGL blog post, we explained that memory works differently compared to other platforms; we told you the Unity Heap should be as small as possible and we also emphasized that there are other allocations in browser memory.

在先前的Unity WebGL 博客文章中 ,我们解释了与其他平台相比,内存的工作方式有所不同。 我们告诉您Unity Heap应该尽可能小,并且我们还强调了浏览器内存中还有其他分配。

This time we would like to dive into what’s inside the Unity Heap so that when it comes to reducing its size, you can do that based on actual data, instead of having to find out a small value that works with your content by trial and error.

这次,我们希望深入了解Unity Heap的内部功能,以便在减小其大小时可以根据实际数据进行操作,而不必通过反复试验找出适合您的内容的较小值。

So, let’s see what’s the Unity Heap, what’s inside it, and how to profile it.

因此,让我们看一下什么是Unity Heap,其内部是什么以及如何对其进行概要分析。

什么是Unity Heap? (What is the Unity Heap?)

First of all, just a reminder that Unity Heap is not to be confused with the Browser Heap, in fact it’s a block of memory inside the Browser Heap. You will find more information in the previous blog post.

首先,仅提醒您不要将Unity Heap与Browser Heap混淆,实际上,这是Browser Heap内部的一块内存。 您可以在以前的 博客文章中 找到更多信息 。

In general terms, the Heap is an area of memory used for dynamic allocations in which native applications allocate via malloc/free or new/delete.

一般而言,堆是用于动态分配的内存区域,本机应用程序通过malloc / free或new / delete进行分配。

In Unity, we use our own Memory Allocators for better memory utilization as well as profiling and debugging purposes, but at the low-level, we still use malloc/free.

在Unity中,我们使用自己的内存分配器来提高内存利用率以及进行概要分析和调试,但是在低级别,我们仍然使用malloc / free。

In Unity WebGL, we refer to this memory, which contains all run-time Unity engine objects, as Unity Heap. Allocations in the Unity Heap are done using dlmalloc.

在Unity WebGL中,我们将此内存(包含所有运行时Unity引擎对象)称为Unity Heap。 Unity 堆中的 分配使用 dlmalloc 完成 。

On console platforms, the size of the heap is defined by the hardware specs and by how much memory is reserved by the OS, so applications must ensure they will not exceed the maximum amount of memory available at run-time.

在控制台平台上,堆的大小由硬件规格和操作系统保留的内存量定义,因此应用程序必须确保它们不会超过运行时可用的最大内存量。

Similarly on WebGL, we must define the size of the Unity Heap in advance (at build-time). What that means is that the Unity Heap can never shrink or grow after initialization.

同样在WebGL上,我们必须预先定义Unity Heap的大小(在构建时)。 这意味着 在初始化之后Unity Heap永远不会收缩或增长

Unity堆中有什么? (What is in the Unity Heap?)

On Unity WebGL, we can categorize allocations in the Unity Heap as follows:

在Unity WebGL上,我们可以对Unity Heap中的分配进行如下分类:

  • Static memory area

    静态存储区

  • Stack

    叠放

  • Dynamic Memory

    动态记忆体

  • Unallocated memory

    未分配的内存

image00

The first blocks to be allocated are the stack and the area for all static objects. The size of the stack is usually 5mb and the size of the static area depends on the code compiled, which basically means the version of Unity and the project.

要分配的第一个块是所有静态对象的堆栈和区域。 堆栈的大小通常为5mb,静态区域的大小取决于所编译的代码,这基本上意味着Unity和项目的版本。

Once those are allocated, all remaining memory is available for Dynamic allocations that will occur at run-time.

分配完这些之后,所有剩余的内存可用于在运行时进行的动态分配。

As code is executed, the Dynamic area will start to occupy more space in the Unity Heap and if it grows too much, it will cause the Unity content to run out of memory.

在执行代码时,动态区域将开始在Unity Heap中占用更多空间,并且如果其增长过多,将导致Unity内容用完内存。

image03

Over time, even though some objects will be freed and others allocated, the size of the Dynamic area will not shrink because there is no compacting mechanism. Instead, this will create gaps of free memory within the Dynamic area.

随着时间的流逝,即使将释放一些对象并分配其他对象,由于没有压缩机制,动态区域的大小也不会缩小。 相反,这将在动态区域内创建可用内存的间隙。

image05

So, just be aware that there can be fragmentation.

因此,请注意可能会有碎片。

By this time, you might be wondering, what about Managed Memory?

到此时,您可能会想知道托管内存又如何呢?

Well…one (or more) of those run-time allocations in the Dynamic area is the Managed Heap where all your scripting objects will be created. So, the Managed Heap is inside the Unity Heap which is inside the Browser JS VM Heap. It does sound a bit complicated so think about it as the movie Inception, or The Matrix. A Heap inside another Heap, and so on…

好吧……“动态”区域中的一个(或多个)运行时分配是“托管堆”,将在其中创建所有脚本对象。 因此,托管堆位于Unity堆内部,而Unity堆位于Browser JS VM堆内部。 听起来确实有点复杂,所以可以将其视为电影《盗梦空间》或《黑客帝国》。 另一个堆中的一个堆,依此类推...

image06

托管内存 (Managed Memory)

This is the memory used to store all scripting objects. It’s called managed because when an object is no longer referenced, its memory will be automatically reclaimed by the garbage collector (Boehm).

这是用于存储所有脚本对象的内存。 之所以称为“托管”,是因为当不再引用对象时,其内存将被垃圾收集器( Boehm ) 自动回收 。

The first important thing to understand here is that this memory is allocated from the Unity Heap (or from the OS on other platforms). Second, this memory is never returned to the system, therefore the Managed Heap can only grow. In fact, when an object is garbage collected, its memory is kept inside the Managed heap for future use.

在此首先要了解的是,此内存是从Unity Heap(或从其他平台上的OS)分配的。 其次,该内存永远不会返回给系统,因此 托管堆只能增长 。 实际上,当对象被垃圾回收时,其内存将保留在托管堆中,以备将来使用。

In Unity WebGL terms, when we say that the memory is not returned to the system we actually mean it is not returned to the pool of available memory blocks in the Unity Heap.

用Unity WebGL术语来说,当我们说内存没有返回到系统时,实际上是指内存没有返回到Unity Heap中的可用内存块池中。

It’s also important to mention that unlike the Unity Heap, which is a single block of memory, Boehm GC can allocate multiple buffers. In addition, each of them can be subdivided into smaller blocks if necessary. However, when a scripting object is created, a contiguous amount of memory large enough for the object is required. If that is not available among the free blocks managed by Boehm GC, a new one block will be taken from the Unity Heap.

还有一点很重要,要提到的是,与Unity Heap是单个内存块不同, Boehm GC可以分配多个缓冲区 。 此外,如有必要,可以将它们每个细分为较小的块。 但是,创建脚本对象时,需要足够大的对象连续内存。 如果Boehm GC管理的空闲块中没有该块,那么将从Unity Heap中获取一个新的块。

For more information about managed memory, read the manual.

有关托管内存的更多信息,请阅读 手册

当您用完托管内存后会发生什么? (What happens when you run out of Managed Memory?)

If Boehm GC fails to find a free block of memory for a new object and then fails to allocate from the Unity Heap, the Unity WebGL content will stop execution with an out-of-memory error suggesting to increase WebGLMemorySize.

如果Boehm G

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值