6

如何释放内存?

说我有一个字符串

  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.

我可以对其他变量使用相同的方法,例如 Long、int 等吗?

4

2 回答 2

6

我假设您指的是标题所示的 VB6 和 VBA,而不是关键字所示的 VB.Net。

在 VB6 和 VBA 中,字符串变量的内存消耗由字符串长度的固定部分和终止符以及字符串内容本身的可变长度部分组成。请参阅http://www.aivosto.com/vbtips/stringopt2.html#memorylayout以获得对此的良好解释。

因此,当您将字符串变量设置为空字符串或 vbNullString 时,您将释放字符串的可变部分而不是固定部分。

其他类型如 long、int、bool 和 date 会消耗固定数量的内存。

你不能在 VB 中完全“释放”局部变量(想想看,有没有任何编程语言可以做到这一点?),而且在大多数情况下,你不会在意,因为局部变量本身(固定部分)通常很小。
我能想到的唯一一种情况是局部变量的内存消耗可能会变大,那就是如果你有深度递归/宽递归的递归函数调用。

于 2012-09-27T07:18:47.987 回答
0

我走了一条不同的路线:我希望 MemoryUsage 有用。显然不是...

我运行一个遍历多个文件的 vba 脚本(因为访问无法处理任何太大的文件);并将它们附加到表格中,对其进行转换,然后吐出摘要。

该脚本循环遍历文件并针对每个文件运行宏。

快速的答案是从任务管理器中提取内存使用量,然后如果它超过 1 GB;暂停子程序,以免损坏记录进入。

我们如何做到这一点?

使用 readfile 函数插入此内存使用函数。

您将需要在代码中创建一个 if 语句,说明:

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
于 2020-03-31T18:44:10.763 回答