vba 强制垃圾回收

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1775470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:01:44  来源:igfitidea点击:

Forcing Garbage Collection

excelvbaexcel-vbaexcel-2000

提问by Zen

Is there a way to force garbage collection in VBA/Excel 2000?

有没有办法在 VBA/Excel 2000 中强制垃圾收集?

This question refers to the Macro language in Excel. Not using VB .NET to manipulate Excel. So GC.collect() won't work

这个问题是指Excel中的宏语言。不使用 VB .NET 来操作 Excel。所以 GC.collect() 不起作用

回答by Ta01

You cannot take advantage of garbage collection provided by the .NET Framework when using straight VBA. Perhaps this articleby Eric Lippert will be helpful

使用直接 VBA 时,您无法利用 .NET Framework 提供的垃圾回收。也许Eric Lippert 的这篇文章会有所帮助

回答by stenci

You can't force GC in VBA, but it's good to set to Nothing the global variables.

您不能在 VBA 中强制 GC,但最好将全局变量设置为 Nothing。

The article mentioned by kd7 says it's useless to set to Nothing the local variables before they go out of scope, but doesn't talk about the global variables.

kd7提到的文章说局部变量在超出范围之前设置为Nothing是没用的,但没有谈到全局变量。

In VBA the global variables defined in a module remain alive through the whole Excel session, i.e. until the document containing the VBA module that defines them closed.

在 VBA 中,模块中定义的全局变量在整个 Excel 会话中都保持活动状态,即直到包含定义它们的 VBA 模块的文档关闭为止。

So don't put useless Set O = Nothingwhen O is local, but do it when it's global.

所以不要Set O = Nothing在 O 是局部的时候把无用,而是在它是全局的时候做。

回答by yu_sha

VBA/Excel does not have garbage collection, like old VB. Instead of GC, it uses reference counting. Memory is freed when you set a pointer to nothing (or when variable goes out of scope). Like in old VB it means that circular references are never freed.

VBA/Excel 没有垃圾收集,就像旧的 VB 一样。它使用引用计数代替 GC。当您将指针设置为空时(或变量超出范围时),内存将被释放。就像在旧 VB 中一样,这意味着永远不会释放循环引用。