将对象设置为“无"的好处是什么? [英] What are the benefits of setting objects to "Nothing"

查看:62
本文介绍了将对象设置为“无"的好处是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,Stack Overflow社区的某些成员将在关闭过程中使用Set Object = Nothing.我能够找到为什么这对于Access实例很有用,但是对于Excel来说却没有令人满意的答案,所以我的问题是 在VBA中将对象设置为Nothing有什么好处?

I've noticed that some members of the Stack Overflow community will use Set Object = Nothing in closing procedures. I was able to find why this is useful for instances of Access, but no answer has been satisfying when it comes to doing this for Excel, so my question is What are the benefits of setting objects to Nothing in VBA?

在下面的示例代码中,将我的对象wsTest设置为等于Nothing是否浪费空间? 否则,如果这样做实际上是好的做法,为什么?

In the sample code below, is setting my objects ws and Test equal to Nothing a waste of space? Else, if doing so is in fact good practice, why?

Dim ws as Worksheet
Dim Test as Range

Set ws = Sheets("Sheet1")
Set Test = ws.Range("A1")

    ‘Utilize Test variable (copy, paste, etc)

Set Test = Nothing
Set ws = Nothing

Exit Sub 

推荐答案

如果这是托管的.NET代码(是垃圾收集的),则您必须Release每个COM您曾经访问过的对象,以免宿主进程(EXCEL.EXE)可能仍在后台运行,从而占用内存并无法完全拆除.

If this was managed .NET code (which is garbage-collected), you'd have to Release every single COM object you ever accessed, lest the host process (EXCEL.EXE) would likely remain running in the background, consuming memory and unable to completely tear down.

但这是VBA代码(引用计数为 ),此外,VBA代码使用的是宿主应用程序控制的对象-这些对象将在宿主应用程序关闭以及发生这种情况时消失VBA执行上下文早已消失.

But this is VBA code (which is reference-counted), moreover VBA code that uses objects that the host application controls - these objects will die when the host application shuts down, and when that happens the VBA execution context is long gone.

换句话说,所有这些Set ... = Nothing指令都是完全多余的.

In other words, all these Set ... = Nothing instructions are completely redundant.

在某些特定情况下,当您使用第三方API/类型库时,对象可能无法完全清除.例如,您可能正在创建一个Access.Application实例,并发现Excel退出后,"ghost" ACCESS.EXE进程仍在任务管理器中保持打开状态:这表明​​您正在以某种方式,某处和Set ... = Nothing可以帮助防止这种情况.

In some specific cases, when you're dealing with a 3rd-party API / type library, it's possible that objects don't entirely clean up. For example you might be creating an Access.Application instance, and find that a "ghost" ACCESS.EXE process remains open in Task Manager well after Excel exited: that's a sign that you're leaking an object reference somehow, somewhere, and Set ... = Nothing can help prevent that.

但是,我不建议像这样系统地使所有对象引用为空.仅当 not 这样做时才引起问题.即使到那时,也将是一个或两个对象将所有内容拖下,而不是全部.如果ACCESS.EXE正常关闭,则没有理由按照此类说明来弄乱您的代码.

However I wouldn't recommend systematically nulling all object references like that. Only when not doing it causes a problem. And even then, it's going to be one or two objects dragging everything down, not all of them. If ACCESS.EXE shuts down properly, there's no reason to clutter up your code with such instructions.

避免在全局状态下存储对象引用也有帮助.如果一切都是本地的,理论上讲,只要本地范围退出,所有涉及的对象都会被销毁.

Avoiding storing object references in global state helps, too. If everything is local, in theory all objects involved are destroyed as soon as the local scope exits.

这篇关于将对象设置为“无"的好处是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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