Re: String Memory Recovery

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




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?




















.



Relevant Pages

  • Re: Multiplicity, Change and MV
    ... as a String, for example. ... A lecturer teaches more than one course. ... As to your comment about loops, Mr Badour's comments and your question about ... different primary keys) the representation within the file structure is much ...
    (comp.databases.theory)
  • Re: COMPARE HLL/ASM
    ... million passes over the test string. ... so RDTSC seems to be the only choice. ... million loops each. ... It compiles with the GNU C compiler or the ...
    (alt.lang.asm)
  • Re: Splitting a large string variable into lines <= 70 chars
    ... I kinda agree with you that perhaps the string search methods and functions like InStrRev and LastIndexOf will not be the fastest way. ... Wait till they grumble it's slow and then throw in a faster routine and your a hero again! ... All he'd done was reduce the number of iterations his code spent in the wait loops. ... Instead, do not modify the original string at all during the loop, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Trouble Using System.Array.ForEach
    ... both a loop and an if-then, as well as some string manipulation. ... loops are very important and useful, but in this case I think I could do ... Nathan Sokalski ... I am trying to avoid) or the ForEach method. ...
    (microsoft.public.dotnet.languages.vb)
  • RE: VBA output to text editor
    ... just running empty loops took 2-3 ... RecA As String * 2 ... Sub BuildFile() ... Dim e As Integer, f As Integer, i As Long ...
    (microsoft.public.excel.programming)