在 JavaScript 中,当完成通过 new ActiveXObject 创建的对象后,我是否需要将其设置为 null?
在 WSH 中运行并创建对象(例如 Scripting.FileSystemObject 或任何任意 COM 对象)的 Javascript 程序中,完成后是否需要将变量设置为 null?例如,我是否建议这样做:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fileStream = fso.openTextFile(filename);
fso = null; // recommended? necessary?
... use fileStream here ...
fileStream.Close();
fileStream = null; // recommended? necessary?
效果与仅仅让变量超出范围不同吗?
In a Javascript program that runs within WSH and creates objects, let's say Scripting.FileSystemObject or any arbitrary COM object, do I need to set the variable to null when I'm finished with it? Eg, am I recommended to do this:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fileStream = fso.openTextFile(filename);
fso = null; // recommended? necessary?
... use fileStream here ...
fileStream.Close();
fileStream = null; // recommended? necessary?
Is the effect different than just letting the vars go out of scope?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 null 分配给对象变量会减少引用计数器,以便内存管理系统可以在需要时立即丢弃资源。当变量超出范围时,引用计数器将自动递减。因此,几乎在所有情况下手动执行都是浪费时间。
理论上,如果函数在其第一部分使用一个大对象 A,在其第二部分使用另一个大对象 B,则如果中间将 A 设置为 null,则该函数的内存效率可能会更高。但由于这不会迫使彩信销毁 A,因此该声明仍然可能是浪费。
如果你做了一些奇特的类设计,你可能会得到循环引用。然后可能需要用手打破圆圈 - 但也许首先避免这样的循环会更好。
有传言称古代数据库访问对象存在错误,可以通过改变变量来避免这些错误。我不会将我的编程规则建立在这样的巫术之上。
(互联网上有大量的 VBscript 代码充满了“Set X = Nothing”;当被问到时,作者倾向于谈论“习惯”和其他语言(C、C++))
Assigning null to an object variable will decrement the reference counter so that the memory management system can discard the resource - as soon as it feels like it. The reference counter will be decremented automagically when the variable goes out of scope. So doing it manually is a waste of time in almost all cases.
In theory a function using a big object A in its first and another big object B in its second part could be more memory efficient if A is set to null in the middle. But as this does not force the mms to destroy A, the statement could still be a waste.
You may get circular references if you do some fancy class design. Then breaking the circle by hand may be necessary - but perhaps avoiding such loops in the first place would be better.
There are rumours about ancient database access objects with bugs that could be avoided by zapping variables. I wouldn't base my programming rules on such voodoo.
(There are tons of VBscript code on the internet that is full of "Set X = Nothing"; when asked, the authors tend to talk about 'habit' and other languages (C, C++))
根据 Ekkehard.Horner 所说...
VBScript、JScript 和 ASP 等脚本是在为您管理内存的环境中执行的。因此,显式地将对象引用设置为 Null 或 Empty,并不一定会将其从内存中删除……至少不会立即删除。 (实际上,它通常几乎是瞬时的,但实际上,任务被添加到环境中的队列中,并在稍后的某个时间点执行。)在这方面,它实际上没有您想象的那么有用。
在编译的代码中,在程序(或某些情况下的代码段)结束之前清理内存非常重要,以便将任何分配的内存返回到系统。这可以防止出现各种问题。除了缓慢运行的代码之外,这在程序退出时最为重要。在 ASP 或 WSH 等脚本环境中,内存管理会在脚本退出时自动进行清理工作。因此,即使您自己没有明确执行此操作,所有对象引用也会为您设置为 null,这使得在这种情况下不必要出现整个混乱。
就脚本执行期间的内存问题而言,如果您构建的数组或字典对象足够大而导致问题,那么您要么远远超出了脚本的范围,要么在代码中采用了错误的方法。换句话说,这在 VBScript 中永远不应该发生。事实上,环境对数组和字典对象的大小施加了限制,以便从一开始就防止这些问题。
Building on what Ekkehard.Horner has said...
Scripts like VBScript, JScript, and ASP are executed within an environment that manages memory for you. As such, explicitly setting an object reference to Null or Empty, does not necessarily remove it from memory...at least not right away. (In practice it's often nearly instantaneous, but in actuality the task is added to a queue within the environment that is executed at some later point in time.) In this regard, it's really much less useful than you might think.
In compiled code, it's important to clean up memory before a program (or section of code in some cases) ends so that any allocated memory is returned to the system. This prevents all kinds of problems. Outside of slowly running code, this is most important when a program exits. In scripting environments like ASP or WSH, memory management takes care of this cleanup automatically when a script exits. So all object references are set to null for you even if you don't do it explicitly yourself which makes the whole mess unnecessary in this instance.
As far as memory concerns during script execution, if you are building arrays or dictionary objects large enough to cause problems, you've either gone way beyond the scope of scripting or you've taken the wrong approach in your code. In other words, this should never happen in VBScript. In fact, the environment imposes limits to the sizes of arrays and dictionary objects in order to prevent these problems in the first place.
如果您有长时间运行的脚本,这些脚本在顶部/开始使用主进程中不需要的对象,则将这些对象设置为 null 可能会更快地释放内存,并且不会造成任何损害。正如其他发帖者所提到的,可能没有什么实际好处。
If you have long running scripts which use objects at the top/start, which are unneeded during the main process, setting these objects to null may free up memory sooner and won't do any harm. As mentioned by other posters, there may be little practical benefit.