6

How do i free up Memory?

Say I have a string

  Dim TestStri As String
  TestStri = "Test"

  ' What do i have to type up to get rid of the variable?

  ' I know
  TestStri = Nothing
  ' will give it the default value, but the variable is still there.

Can I use the same Method for other variables i.e. Long, int etc.

aek8
  • 319
  • 1
  • 8
  • 22
Padawan
  • 382
  • 6
  • 9
  • 22
  • question makes no sense. – Mitch Wheat Sep 27 '12 at 03:57
  • 1
    Local variables are `managed` (created/released) by the CLR's memory manager implicitly using `LIFO` - Read - http://stackoverflow.com/questions/1561296/stack-vs-heap-in-net and http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap – KV Prajapati Sep 27 '12 at 03:59

2 Answers2

6

I'm assuming you are referring to VB6 and VBA as indicated by your title, not VB.Net, as indicated by a keyword.

In VB6 and VBA the memory consumption of a string variable consists of a fixed part for the string's length and a terminator and a variable length part for the string contents itself. See http://www.aivosto.com/vbtips/stringopt2.html#memorylayout for a good explanation of this.

So, when you set the string variable to an empty string or vbNullString, you will be freeing up the variable part of the string but not the fixed part.

Other types like long, int, bool and date consume a fixed amount of memory.

You can't "free" local variables in VB completely (come to think of it, is there ANY programming language where you can do that?), and for the most part, you wouldn't care because the local variables themselves (the fixed portion) is usually very small.
The only case I can think of where the memory consumption of local varibles could get big is if you have recursive function calls with deep recursion/wide recursion.

GTG
  • 4,914
  • 2
  • 23
  • 27
  • 3
    +1 but slight correction: in VBA, you can't actually set a `String` variable to `Nothing`. You can only use `Nothing` for object types whereas `String` is a primitive type in VBA. `s = vbNullString` is what you need (from the aivosto.com link in the answer) – barrowc Sep 27 '12 at 22:27
0

I went a differs route : I was hoping MemoryUsage would be useful. It wasn't, apparently...

I run a vba script that goes through multiple files (since access cannot handle anything too large); and append them to a table, transform it and then spit out a summary.

The script loops through files and runs macros against each of them.

The quick answer is to pull the memory usage from the task manager and then if it exceeds 1 GB; pause the subroutine so no corrupt records get in.

How do we do this?

Insert this memory usage Function with the readfile function.

You will need to create an if statement in your code that says:

dim memory as long

memory = memory_usage

' 1000000 ~ 1 GB

If memory > 1000000 then 

   End Sub

end if

=================================================

[path to file] = "C:\….\ShellOutputfile.txt"

Function Memory_Usage() as Long

Dim lines As Long
Dim linestring As String

Shell "tasklist /fi " & """IMAGENAME EQ MSACCESS.EXE""" & ">" & """[path to file]"""

'get_list_data

lines = CInt(get_listing_data("[path to file]", 1, 0))
linestring = get_listing_data("[path to file]", 2, 4)

linestring = Right(linestring, 11)

linestring = Replace(linestring, " K", "") ' K
linestring = Replace(linestring, " ", "")

lines = CLng(linestring)

Memory_Usage = lines

End Function

=============================

Public Function get_listing_data(PATH As String, Choice As Integer, typeofreading As Integer) As String

    ' parse in the variable, of which value you need.

    Const ForReading = 1, ForWriting = 2, ForAppending = 8

    Dim tmp_var_str As String
    Dim fso, ts, fileObj, filename
    Dim textline As String
    Dim tmp_result As String
    Dim TMP_PATH As String

    Dim tmpchoice As Integer
    Dim tor As Integer

    Dim counter As Integer
    ' type of reading determines what loop is used
    ' type of reading = 0; to bypass; > 0, you are choosing a line to read.
    counter = 0

    TMP_PATH = PATH
    tmp_var_str = var_str
    tmp_result = ""
    tor = typeofreading

    ' choice = 1 (count the lines)
    ' choice = 2 (read a specific line)

    tmpchoice = Choice

    ' Create the file, and obtain a file object for the file.

    If Right(PATH, 1) = "\" Then TMP_PATH = Left(PATH, Len(PATH) - 1)
    filename = TMP_PATH '& "\Profit_Recognition.ini"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fileObj = fso.GetFile(filename)

    ' Open a text stream for output.
    Set ts = fileObj.OpenAsTextStream(ForReading, TristateUseDefault)

    Do While ts.AtEndOfStream <> True

        If tmpchoice = 1 Then
            counter = counter + 1
            textline = ts.ReadLine
            tmp_result = CStr(counter)
        End If

        If tmpchoice = 2 Then
            counter = counter + 1

            tmp_result = ts.ReadLine

            If counter = tor Then

                Exit Do

            End If

        End If

    Loop


    get_listing_data = tmp_result

End Function
Simon.S.A.
  • 6,240
  • 7
  • 22
  • 41