Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

do i need to clean up objects in VBScript?

178 views
Skip to first unread message

st...@yohanan.org

unread,
Sep 19, 1998, 3:00:00 AM9/19/98
to
in vbscript under ASP, do i need to clean up any objects i use within a
subroutine or function before returning from it? for example, do i need to
set the objects to Nothing in the following routines or will the interpreter
do the clean-up for me when i return from them?

thanks!

<%
sub foo()
dim objDict
set objDict = CreateObject("Scripting.Dictionary")
...
set objDict = nothing
end foo

function bar()
dim objRS
set objRS = Server.CreateObject("ADODB.Recordset")
...
call objRS.Close()

set objRS = nothing

bar = <some value>
end bar
%>

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum

Bob Butler

unread,
Sep 19, 1998, 3:00:00 AM9/19/98
to
st...@yohanan.org wrote in message <6u107h$3cc$1...@nnrp1.dejanews.com>...

>in vbscript under ASP, do i need to clean up any objects i use within a
>subroutine or function before returning from it? for example, do i need to
>set the objects to Nothing in the following routines or will the
interpreter
>do the clean-up for me when i return from them?
<cut>

the interpreter should clean up, but it would never hurt to do it yourself
and then you don't have to worry about whether it happens or not or if
different revs handle it the same way. the bottom line is that you should
always clean up any resources you allocate yourself whether the system would
do it for you or not.


st...@yohanan.org

unread,
Sep 20, 1998, 3:00:00 AM9/20/98
to
In article <6u1rb8$n...@wellspring.us.dg.com>,

thanks, bob.

i am pretty anal about setting them to Nothing but (1) i wanted to be sure
that if i accidentally slipped and missed a couple in a function or two it
wouldn't kill me, and (2) was trying to see if i could skip it altogether if
it was common-knowledge that it was only needed in certain (assumed rare)
cases.

it gets to be a real pain and adds a lot of clutter to a routine which has a
lot of error-checking code that needs to clean-up a smattering of objects at
every exit point; at the end of the routine it isn't too much of a problem,
however.

Eric Lippert (Microsoft Scripting Dev)

unread,
Sep 23, 1998, 3:00:00 AM9/23/98
to
You don't _need_ to clean up. VBScript has a garbage collector. However,
it is a good idea to do , because you are smarter than the garbage
collector. You know exactly when you are done with an object and can
reclaim it's memory. The garbage collector has to be conservative.

For example,

Sub Foo()
Dim MyObj
set MyObj = CreateObject("Extremely.Expensive.Object")
MyObj.Blah(123)
' MyObj is never used for the rest of the function
Set MyObj = Nothing
' [A huge amount of code here]
End Sub

Now suppose Foo is called by the server dozens of times on dozens of
threads, often at the same time. If you don't set MyObj to Nothing, then
MyObj will be alive until Foo ends, a potentially very long time if Foo is
an expensive function. That means that the total time that large amounts of
resources are consumed becomes much larger as the system gets loaded. By
throwing away the object when you know you are done with it, you greatly
reduce the time that the resources are held.

--> This is particularly important if the resources held by your object are
synchronized. <-- You want synchronized resources to be freed ASAP so that
some other blocked thread can grab them.

Eric


st...@yohanan.org wrote in message <6u107h$3cc$1...@nnrp1.dejanews.com>...
>in vbscript under ASP, do i need to clean up any objects i use within a
>subroutine or function before returning from it? for example, do i need to
>set the objects to Nothing in the following routines or will the
interpreter
>do the clean-up for me when i return from them?
>

>thanks!
>
><%
>sub foo()
> dim objDict
> set objDict = CreateObject("Scripting.Dictionary")
> ...
> set objDict = nothing
>end foo
>
>function bar()
> dim objRS
> set objRS = Server.CreateObject("ADODB.Recordset")
> ...
> call objRS.Close()
>
> set objRS = nothing
>
> bar = <some value>
>end bar
>%>
>

st...@yohanan.org

unread,
Sep 25, 1998, 3:00:00 AM9/25/98
to
thanks for the reply, eric. that's pretty much wwhat i was expecting however
i've not seen a logical-enough document of it. if you can, however, clear up
the one last question i have. is setting an object to Nothing just prior to
exiting the routine necessary, or does the garbage collector recognize just as
quickly that the variable has gone out of scope and harvest the memory. for
example, in the following code are the lines which set the object to Nothing
redundant?

sub foo()
dim obj
set obj = CreateObject("some.object")

...

if (some condition is true) then
set obj = Nothing
exit sub
end if

...

set obj = Nothing
end foo

thanks!


In article <OQb7WXz...@uppssnewspub04.moswest.msn.net>,

0 new messages