声明变量内存泄漏 [英] Declaring Variables Memory Leaks

查看:89
本文介绍了声明变量内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道使用VBScript时处理内存的最正确方法是什么.我应该在使用变量之前就声明所有变量吗?程序的开始?我了解全局vs本地,但是在我的脚本中,所有变量都是本地的.我知道99.9%的时间使用VBScript编写时,内存泄漏永远不会成为问题,但我也对清除和释放脚本中的内存的最佳"方法感到好奇. 最好"是指清除变量/对象的时间(在使用完之后/脚本结束后),等等.

I am wondering what would be the most correct way to deal with memory when using VBScript. Should declare all variables right before I use them? The beginning of the program? I understand global vs local, however in my script all variables are local. I know that memory leaks will never be a problem when writing in VBScript 99.9% of the time, but I am also curious as to the 'best' way to clear and release memory within a script. By 'best' I mean, the timing of clearing variables/objects (right after you are done using them vs the end of the script), etc.

一个例子:

Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
Dim arrList : Set arrList = CreateObject("System.Collections.ArrayList")
Dim objDict: Set objDic  = CreateObject( "Scripting.Dictionary")
Dim objEmail : Set objEmail = CreateObject("CDO.Message")

Dim someArray(), x, y, z, item

推荐答案

VBScript垃圾回收器在每一行的末尾运行以清除隐式变量,并在每个过程的末尾(end subend functionend property)以清除显式变量.对象相似,但是增加了约束.它的工作方式类似于VBA的垃圾收集器.相比之下,JScript会等到30,000个对象超出范围后再运行并释放内存.

The VBScript garbage collector runs at the end of every line to clear implicit variables and at the end of every procedure (end sub, end function, and end property) to clear explicit variables. Objects are similar but have added constraints. It works similar to VBA's garbage collector. By contrast JScript waits until 30,000 objects have gone out of scope before running and freeing memory.

隐式变量是一个未命名的变量-msgbox LCase(UCase("String")具有两个隐式变量-UCase("String")的结果,并传递给LCase(implicitVar1),该结果返回implicitVar2并传递给Msgbox. Explict变量可以通过DIM声明,也可以像在A=5中那样使用它来声明,从而创建一个名为A的显式变量.

An implicit variable is an unnamed variable - msgbox LCase(UCase("String") has two implicit variables - the result of UCase("String") and that is passed to LCase(implicitVar1) which returns implicitVar2 which is passed to Msgbox. An Explict variable is declared either by DIM or just by using it as in A=5 which creates an explicit variable called A.

另一方面,VBScript具有更简单的基于堆栈的垃圾收集器.清道夫进入范围时将其添加到堆栈中,当它们超出范围时将其清除,并且任何时候丢弃对象都会立即将其释放.

VBScript, on the other hand, has a much simpler stack-based garbage collector. Scavengers are added to a stack when they come into scope, removed when they go ou t of scope, and any time an object is discarded it is immediately freed.

https ://blogs.msdn.microsoft.com/ericlippert/2003/09/17/how-do-the-script-garbage-collectors-work/

VBScript的垃圾收集器完全不同.它在每个语句和过程的末尾运行,并且不搜索所有内存.而是,它跟踪语句或过程中分配的所有内容;如果超出范围,它将立即将其释放

VBScript’s garbage collector is completely different. It runs at the end of every statement and procedure, and does not do a search of all memory. Rather, it keeps track of everything allocated in the statement or procedure; if anything has gone out of scope, it frees it immediately

https://blogs .msdn.microsoft.com/ericlippert/2004/12/22/t4-vbscript-and-the-terminator/

https ://blogs.msdn.microsoft.com/ericlippert/2004/03/01/syntax-semantics-micronesian-cults-and-novice-programmers/

CPU是基于堆栈的计算机(而VBScript是基于堆栈的虚拟机).当CPU调用函数时,调用程序将参数放在堆栈和返回地址上,调整堆栈帧并执行跳转.被调用方函数在堆栈上创建局部变量,并将返回值放在堆栈上.返回时,将堆栈指针调整回原来的位置,从而自动释放所有上述内容.

The CPU is a stack based machine (and VBScript a stack based virtual machine). When the CPU calls a function the calling program puts the parameters on the stack and the return address, adjust the stack frame and does a jump. The callee function creates local variables on the stack and also places the return value on it. When it returns the stack pointer is adjusted back to where it was which automatically frees all the above.

这篇关于声明变量内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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