Results 1 to 4 of 4

Thread: Clear memory in vb6

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    248

    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
    Name:  ryv2S.jpg
Views: 630
Size:  24.6 KB
    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
    Name:  1a1hf.jpg
Views: 605
Size:  24.6 KB

    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?

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,000

    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.

  3. #3
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    2,034

    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>

  4. #4
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    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
  •  



Click Here to Expand Forum to Full Width