垃圾收集未使用的模块 [英] Garbage collect unused modules

查看:105
本文介绍了垃圾收集未使用的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用动态导入来加载用户在浏览器中编写的脚本.首先将脚本内容放入Blob,然后使用动态import()将脚本作为模块加载.随着时间的流逝,我希望这些脚本会发生更改并被销毁,从而可以对相应的模块进行垃圾回收.但是,基于Chrome中的内存配置文件,这是没有发生的.

I am using dynamic import to load scripts written by the user in the browser. I start by placing the script contents into a blob, then I use dynamic import() to load the script as a module. Over time, I expect these scripts to change and be destroyed, and thus for the corresponding modules to be garbage collected. However, based on memory profiling in Chrome, that is not happening.

似乎与ModuleMap有关的原因.这是我不再使用所有脚本后拍摄的内存快照的屏幕截图.

The reason why seems to be related to something called the ModuleMap. Here's a screenshot from a memory snapshot I took after all scripts were no longer in use.

如您所见,Window对象为这些模块提供了一条保留路径.只要是这种情况,我肯定会最终耗尽内存,因为这些模块是在用户每次编辑脚本时都会创建的.

As you can see, the Window object is is providing a retaining path down to these modules. As long as that is the case, I'm sure to eventually run out of memory, since these modules are created each time the user edits their script.

我想知道一旦这些模块不再使用,是否有办法让Chrome(和其他浏览器)卸载这些模块.

I'd like to know if there's a way to get Chrome (and other browsers) to unload these modules once they are no longer in use.

推荐答案

我从

I'm getting the sense from this spec that maybe the answer is that you cannot cause modules to be unloaded. This is because any given module must only be parsed once per Document worker. There's no way for me to promise that I'll never use the module from a given URL again once I've thrown it away, thus there's no way for the ModuleMap to allow things to be collected.

确实,通读Chromium源代码,我看不到对

And, indeed, reading through the Chromium source code, I don't see any calls to UnregisterModuleScript. It's entirely possible that this is not all the relevant code, but if it is, it stands to reason that any given instance of ModuleMap is going to hang on to its modules forever.

从理论上讲,我似乎可以从WebWorkers获得所需的行为,因为它们具有不同的全局范围.如果有人可以告诉我我是否在吠叫正确的树,那将会很有帮助.

It seems like in theory I might be able to get the desired behavior from WebWorkers, since they have a different global scope. If anyone can tell me whether I'm barking up the right tree, that would be helpful.

这篇关于垃圾收集未使用的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆