How do I keep load module dynamically but without constant memory usage increase?

97 Views Asked by At

There is a need to load and serialize esm module dynamically on deno. As of I know, since there is no way to clear cache on await import(), I tried importing inside WebWorker, then kill it after it's done.

Profiler tells me that all of the imported source code is piled up inside the worker, but after killing the worker I don't see process' memory usage decline. (checked using docker status)

Below is simplified example code.

// worker.ts
self.onmessage = async (evt: MessageEvent) => {
  const mod = await import(evt.data);
  self.postMessage(JSON.stringify(mod));
}
// manager.ts
let worker = new Worker("./worker.ts");

// trying to clear source code piled up
setInterval(() => {
  worker.terimnate();
  worker = new Worker("./worker.ts")
}, 10_000);

worker.onmessage = (evt: MessageEvent) => console.log(e.data);

worker.postMessage('https://deno.land/x/[email protected]/index.ts')
1

There are 1 best solutions below

0
On

Could you try setting hard memory limits, such as

deno run --v8-flags='--max-heap-size=50,--max-old-space-size=50' \ https://deno.land/[email protected]/examples/welcome.ts