网络工作者是否有自己的垃圾收集器?

时间:2022-04-25 19:32:17

I have some processing which isn't necessarily cpu-intensive, but lots of temporary objects are created which results in unsavory Garbage Collector hiccups during animation etc.

我有一些不一定是cpu密集的处理,但是创建了许多临时对象,导致在动画期间出现令人讨厌的垃圾收集器打嗝。

Will offloading that temp-object creation process to web workers help alleviate that? In other words - will the GC hiccups be isolated to the web worker thread and not affect my main thread, or is GC something that will affect both threads?

将临时对象创建过程卸载到Web工作者是否有助于缓解这种情况?换句话说 - GC打嗝是否会被隔离到Web工作线程并且不会影响我的主线程,或者GC是否会影响两个线程?

1 个解决方案

#1


4  

The ECMAScript specification does not specify any form memory management, garbage collection is only referred to in non-normative parts.

ECMAScript规范没有指定任何表单内存管理,垃圾收集仅在非规范部分中引用。

Similarly the web worker spec does not say much about garbage collection except wrt. to how long some objects must live.

类似地,除了wrt之外,web worker规范没有说明垃圾收集。一些物体必须存活多久。

So this is implementation-specific behavior. Even if implementations implement a per-worker GC and avoid shared overhead in the general case they might still trigger a global collection of all workers due to memory pressure, especially on memory-constrained systems.

所以这是特定于实现的行为。即使实现实现了每个工作者GC并避免了一般情况下的共享开销,由于内存压力,它们仍可能触发所有工作程序的全局集合,尤其是在内存受限的系统上。

That said, it is more likely that you will achieve some GC isolation with workers than without. But you have to take care to avoid messaging overhead between workers and the main thread, since serializing messages (for the structured clone algorithm) can produce additional garbage. Using transferables or shared memory buffers can avoid this.

也就是说,你很可能会与工人实现一些GC隔离。但是你必须注意避免工作者和主线程之间的消息传递开销,因为序列化消息(对于结构化克隆算法)会产生额外的垃圾。使用transferables或共享内存缓冲区可以避免这种情况。

#1


4  

The ECMAScript specification does not specify any form memory management, garbage collection is only referred to in non-normative parts.

ECMAScript规范没有指定任何表单内存管理,垃圾收集仅在非规范部分中引用。

Similarly the web worker spec does not say much about garbage collection except wrt. to how long some objects must live.

类似地,除了wrt之外,web worker规范没有说明垃圾收集。一些物体必须存活多久。

So this is implementation-specific behavior. Even if implementations implement a per-worker GC and avoid shared overhead in the general case they might still trigger a global collection of all workers due to memory pressure, especially on memory-constrained systems.

所以这是特定于实现的行为。即使实现实现了每个工作者GC并避免了一般情况下的共享开销,由于内存压力,它们仍可能触发所有工作程序的全局集合,尤其是在内存受限的系统上。

That said, it is more likely that you will achieve some GC isolation with workers than without. But you have to take care to avoid messaging overhead between workers and the main thread, since serializing messages (for the structured clone algorithm) can produce additional garbage. Using transferables or shared memory buffers can avoid this.

也就是说,你很可能会与工人实现一些GC隔离。但是你必须注意避免工作者和主线程之间的消息传递开销,因为序列化消息(对于结构化克隆算法)会产生额外的垃圾。使用transferables或共享内存缓冲区可以避免这种情况。