Re: String Memory Recovery
- From: "Bill McCarthy" <TPASoft.com Are Identity Thieves>
- Date: Thu, 25 Jun 2009 20:53:00 +1000
Hi David,
I decided to run a couple of simple tests on this. Having two methods, Foo1(ByVal x as String), and Foo2(ByRef x as string). In both of those methods I just set a form field value to the len(x). So timing calling these methods in large loops, the larger the string being passed in, the larger the difference between ByRef and ByVal. This is because ByVal requires a new BSTR to be created.
As these were large loops in the order of millions, it's pretty safe to assume that VB released the created strings immediately. In the first test case I used local variable, later I moved that variable up to a form field level to ensure the compiler could not safely optimize it out. The results were the same and showed the ByVal call taking significantly longer. As the compiler could not know the value of that variable on each iteration, it has to create a new string, but as the memory didn't significantly grow it's safe to say it was releasing them on each iteration. Whether or not this applies outside of loops can only be speculated but one would presume the compiler would do the same thing.
Next I ran the tests with a Const string and also with a string literal. There was no difference between the Const variable and literal string in terms of results to each other, but compared to a string variable, they were significantly slower with the ByRef calls. In fact, the ByRef and ByVal calls came in much of a muchness. This makes sense, as the ByRef calls would also require the creation of a new BSTR.
so if you had a loop, this :
Dim s as String
s = "...some large string ........."
For i = 0 to count
Foo2 s
Next i
Would be faster than either:
Const s = "...some large string ........."
For i = 0 to count
Foo2 s
Next i
or
For i = 0 to count
Foo2 "...some large string ........."
Next i
This tells us that indeed a new string is being created for the literals and for Const strings as well.
"David" <dw85745NOT@xxxxxxxxxxxxx> wrote in message news:uCWWI%23q8JHA.4976@xxxxxxxxxxxxxxxxxxxxxxx
If a string is declared locally at the termination of the procedure string memory should be recovered.
=====================
However, when does the compiler recover memory for a string used in an object declaration:
Set rsTemp = DaoDb.OpenRecordset("MyTBLName")
or in a parameter for a called procedure?
Call MyProcedure("MyTBLName")
--------------------
Logic says:
1) For object declaration it would be after execution of the statement -- Set rsTemp
2) For the Called procedure it would also be after the called procedure is executed.
OR
would both strings be considered local to the procedure in which they reside and not be cleared until that procedures stack is popped?
----------------------
Also, is there any advantage to clearing a string in a local procedure that is used early in the procedure and not used again?
.
- Follow-Ups:
- Re: String Memory Recovery
- From: Ralph
- Re: String Memory Recovery
- References:
- String Memory Recovery
- From: David
- String Memory Recovery
- Prev by Date: iTnef of MAPI functions with VB6
- Next by Date: Re: Speed Speed Speed - Cutting down on wasted cycles
- Previous by thread: Re: String Memory Recovery
- Next by thread: Re: String Memory Recovery
- Index(es):
Relevant Pages
|