释放内存:如何删除变量一次 - VBA VB ACCESS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12613834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Free up Memory: How to delete variables once am don with them- VBA VB ACCESS
提问by Padawan
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.
我可以对其他变量(即 Long、int 等)使用相同的方法吗?
采纳答案by GTG
I'm assuming you are referring to VB6 and VBA as indicated by your title, not VB.Net, as indicated by a keyword.
我假设您指的是标题所示的 VB6 和 VBA,而不是关键字所示的 VB.Net。
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#memorylayoutfor a good explanation of this.
在 VB6 和 VBA 中,字符串变量的内存消耗由字符串长度的固定部分和终止符以及字符串内容本身的可变长度部分组成。请参阅http://www.aivosto.com/vbtips/stringopt2.html#memorylayout以获得对此的很好解释。
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.
因此,当您将字符串变量设置为空字符串或 vbNullString 时,您将释放字符串的可变部分而不是固定部分。
Other types like long, int, bool and date consume a fixed amount of memory.
其他类型如 long、int、bool 和 date 消耗固定数量的内存。
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.
你不能在 VB 中完全“释放”局部变量(想想看,有没有任何编程语言可以做到这一点?),而且在大多数情况下,你不会关心,因为局部变量本身(固定部分)通常很小。
我能想到的本地变量的内存消耗可能会变大的唯一情况是,如果您有具有深度递归/宽递归的递归函数调用。
回答by Philip Carl Vago
I went a differs route : I was hoping MemoryUsage would be useful. It wasn't, apparently...
我走了一条不同的路线:我希望 MemoryUsage 会有用。显然不是……
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.
我运行了一个遍历多个文件的 vba 脚本(因为访问不能处理太大的东西);并将它们附加到表格中,对其进行转换,然后吐出一个摘要。
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.
快速答案是从任务管理器中提取内存使用情况,然后如果超过 1 GB;暂停子程序,以免损坏的记录进入。
How do we do this?
我们如何做到这一点?
Insert this memory usage Function with the readfile function.
使用 readfile 函数插入此内存使用函数。
You will need to create an if statement in your code that says:
您需要在代码中创建一个 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