Node是否需要收集缓存垃圾?

时间:2022-06-29 23:58:29

NodeJS require function, that loads modules, has a "cache" (which is a object).

NodeJS需要加载模块的函数,有一个“缓存”(它是一个对象)。

Entries is this cache are garbage collected once I'm no longer using the module? (resulting in loading from disk if used again)

条目是这个缓存是垃圾收集一旦我不再使用该模块? (如果再次使用,将导致从磁盘加载)

I think the answer is "no", but I didn't found any references on the web

我认为答案是“不”,但我没有在网上找到任何参考

1 个解决方案

#1


7  

Entries is this cache are garbage collected once I'm no longer using the module?

条目是这个缓存是垃圾收集一旦我不再使用该模块?

No. Modules loaded with require() are cached indefinitely whether you are done using them or not.

否。无论您是否使用过,都会无限期地缓存使用require()的模块。

Memory for Javascript variables/objects that is used by the module is garbage collected subject to all the normal rules of garbage collection (when there is no live code that still has a reference to the variable/object). But, the module cache keeps a reference to the loaded module itself so the code or any module level variables are not garbage collected unless a module is manually removed from the cache.

模块使用的Javascript变量/对象的内存根据垃圾收集的所有常规规则进行垃圾收集(当没有仍然具有对变量/对象的引用的实时代码时)。但是,模块高速缓存保留对已加载模块本身的引用,因此除非从高速缓存中手动删除模块,否则不会对代码或任何模块级变量进行垃圾回收。

Here's a link to the node.js doc on the topic.

这是关于该主题的node.js doc的链接。

Caching

Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.

模块在第一次加载后进行缓存。这意味着(除其他外)每次调用require('foo')将获得完全相同的返回对象,如果它将解析为同一个文件。

If you want to manually remove a module from the cache, that is described here:

如果要从缓存中手动删除模块,请在此处进行说明:

unloading code/modules

Though, this will allow all module-level variables to be garbage collected, given the way node.js is structured I don't think it will actually unload the code from memory.

虽然,这将允许所有模块级变量被垃圾收集,鉴于node.js的结构方式,我认为它实际上不会从内存中卸载代码。

#1


7  

Entries is this cache are garbage collected once I'm no longer using the module?

条目是这个缓存是垃圾收集一旦我不再使用该模块?

No. Modules loaded with require() are cached indefinitely whether you are done using them or not.

否。无论您是否使用过,都会无限期地缓存使用require()的模块。

Memory for Javascript variables/objects that is used by the module is garbage collected subject to all the normal rules of garbage collection (when there is no live code that still has a reference to the variable/object). But, the module cache keeps a reference to the loaded module itself so the code or any module level variables are not garbage collected unless a module is manually removed from the cache.

模块使用的Javascript变量/对象的内存根据垃圾收集的所有常规规则进行垃圾收集(当没有仍然具有对变量/对象的引用的实时代码时)。但是,模块高速缓存保留对已加载模块本身的引用,因此除非从高速缓存中手动删除模块,否则不会对代码或任何模块级变量进行垃圾回收。

Here's a link to the node.js doc on the topic.

这是关于该主题的node.js doc的链接。

Caching

Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.

模块在第一次加载后进行缓存。这意味着(除其他外)每次调用require('foo')将获得完全相同的返回对象,如果它将解析为同一个文件。

If you want to manually remove a module from the cache, that is described here:

如果要从缓存中手动删除模块,请在此处进行说明:

unloading code/modules

Though, this will allow all module-level variables to be garbage collected, given the way node.js is structured I don't think it will actually unload the code from memory.

虽然,这将允许所有模块级变量被垃圾收集,鉴于node.js的结构方式,我认为它实际上不会从内存中卸载代码。