Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a worklet after use

My program creates a lot of worklets from code generated at runtime. They are only used for a short amount of time. Is there a way to remove or unregister old, unused worklets to regain memory? Or is this done automatically by GC?

https://developer.mozilla.org/en-US/docs/Web/API/Worklet/addModule

like image 456
dogefromage Avatar asked Jul 01 '26 05:07

dogefromage


2 Answers

It sounds to me like once they're loaded they'll stay there until the context it's attached to is destroyed or the browser decides it's time to dispose of the WorkletGlobalScope, and so I see no way to remove them after use.

But I'm no expert and have only dabbled in worklets. Perhaps someone else will have a more nuanced reading of the spec.


From the specification:

11.3.3 The worklet's lifetime The lifetime of a Worklet has no special considerations; it is tied to the object it belongs to, such as the Window.

Each Document has a worklet global scopes, which is a set of WorkletGlobalScopes, initially empty.

The lifetime of a WorkletGlobalScope is, at a minimum, tied to the Document whose worklet global scopes contain it. In particular, destroying the Document will terminate the corresponding WorkletGlobalScope and allow it to be garbage-collected.

Additionally, user agents may, at any time, terminate a given WorkletGlobalScope, unless the specification defining the corresponding worklet type says otherwise. For example, they might terminate them if the worklet agent's event loop has no tasks queued, or if the user agent has no pending operations planning to make use of the worklet, or if the user agent detects abnormal operations such as infinite loops or callbacks exceeding imposed time limits.

Finally, specifications for specific worklet types can give more specific details on when to create WorkletGlobalScopes for a given worklet type. For example, they might create them during specific processes that call upon worklet code, as in the example.

Source: https://html.spec.whatwg.org/multipage/worklets.html#worklets-lifetime

like image 163
Slbox Avatar answered Jul 02 '26 18:07

Slbox


If you are using AudioWorkletProcessor you can schedule your Worklet nodes for garbage collection. According to the MDN:

A node that transforms its input. A processor implementing such a node should return false from the process method to allow the presence of active input nodes and references to the node to determine whether it can be garbage-collected. An example of a node with this behavior is the GainNode. As soon as there are no inputs connected and references retained, gain can no longer be applied to anything, so it can be safely garbage-collected.

I highly recommend Audion browser extension for web audio debugging purposes.

like image 37
Kutalia Avatar answered Jul 02 '26 18:07

Kutalia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!