-
Jan 4th, 2019, 06:09 AM
#1
Thread Starter
Addicted Member
Clear memory in vb6
Code:
Option Explicit
Dim VBFlexGridCells() As String
Private Sub store_values()
On Error GoTo store_values_EH
Dim IndexLong As Long
Dim i As Integer
Dim entry As String
Dim rw As Integer
Dim aa As String
Dim bb As String
Dim cc As String
Dim dd As String
Dim ee As String
Dim jj As Integer
Dim ff As String
Dim ll As String
Dim mm As String
Dim nn As String
Dim kk As String
rw = 1
For rw = 0 To 15000
aa = String(10000, "w")
bb = String(125, "w")
cc = String(125, "w")
dd = String(125, "w")
ee = String(125, "w")
ff = String(125, "w")
kk = String(125, "w")
ll = String(125, "w")
mm = String(125, "w")
nn = String(125, "w")
entry = aa & Chr(9) & bb & Chr(9) & cc & Chr(9) & dd & Chr(9) & ee & Chr(9) & ff & Chr(9) & kk & Chr(9) & ll & Chr(9) & mm & Chr(9) & nn
IndexLong = IndexLong + 1
ReDim Preserve VBFlexGridCells(10, IndexLong)
Dim abc() As String
abc = Split(entry, vbTab)
For jj = LBound(abc) To UBound(abc)
VBFlexGridCells(jj, IndexLong) = abc(jj)
Next
Next
Exit Sub
store_values_EH:
MsgBox Err.Description & Space(10) & "store_values"
End Sub
Before the execution of the code above, the memory used by our vb6 program :17,720 KB
After the execution of the code above , the memory used by our vb6 program :386,836 KB
Our approximation of memory usage after code execution :320 MB
Actual memory usage and approximated memory usage are in same range ~300MB
however, when string cc is increased to take in 126 chars there is huge spike in memory usage.
cc = String(126, "w")
The memory used by our vb6 program after code execution:700.04 MB
Our approximation of memory usage after code execution :320 MB
Memory usage shoots from 320 Mb to 700 MB .similarly, when other strings are increased to 126 chars , memory shoots up in the range of GB's and results in "out of memory" errors.Also there seems to be some issue with vb6 string append (http://www.aivosto.com/articles/stringopt2.html "Building huge strings" )
Is there any option to detect and clear (de-allocate) this unused memory in vb6?
-
Jan 4th, 2019, 06:24 AM
#2
Re: Clear memory in vb6
When you append to a string, the space for the new size has to be allocated to include what you're appending, the old string copied to the new area, and the additional characters added. This has always been the case, so appending repeatedly is a serious slow down, and is a challenge when dealing with large strings, since you'll need more than twice the current memory allocation for a string to append to it.
The VB6 solution I normally use is to take advantage of using the Mid$ statement to insert characters into a pre-existing string. You preallocate a huge string, keep track of how much is used, and "append" by inserting characters or strings into the string buffer at the end of the used portion.
There are classes that people have written to encapsulate this type of operation. Not only can it help alleviate your memory issue, it can be thousands of times faster than using the existing string concatenation operation.
-
Jan 4th, 2019, 07:13 AM
#3
Re: Clear memory in vb6
Mem usage discrepancy is probably connected with small BSTR cache ole automation implements to boost small strings allocation/concatenation. Probably 125 chars is some kind of threshold for the BSTR cache to use some internal arena allocator but there is no documentation on the internals of the implementation. Wine project could have something similar implemented if they reversed oleaut enough. . .
Anyway, check out SetOaNoCache function and OANOCACHE environment variable.
Edit: It is in fact using a hash table of arena allocators keyed on string size as impl in Wine. No threshold in sight though.
cheers,
</wqw>
Last edited by wqweto; Jan 4th, 2019 at 07:16 AM.
-
Jan 4th, 2019, 08:46 AM
#4
Re: Clear memory in vb6
I think this is why StringBuilders were invented. (not to mention the performance implications...)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|