释放内存:如何使用变量删除变量-VBA VB ACCESS
如何释放内存 说我有一根绳子释放内存:如何使用变量删除变量-VBA VB ACCESS,vba,Vba,如何释放内存 说我有一根绳子 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等)使用相同的方法吗
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等)使用相同的方法吗?我假设您指的是标题所示的VB6和VBA,而不是关键字所示的VB.Net 在VB6和VBA中,字符串变量的内存消耗包括字符串长度的固定部分以及字符串内容本身的终止符和可变长度部分。请参阅,以获得对此的良好解释 因此,当您将字符串变量设置为空字符串或vbNullString时,将释放字符串的变量部分,而不是固定部分 其他类型(如long、int、bool和date)消耗固定数量的内存 你不能在VB中完全“释放”局部变量(想想看,有什么编程语言可以做到这一点吗?),而且在大多数情况下,你不会在意,因为局部变量本身(固定部分)通常非常小。
我能想到的唯一一种情况是,如果使用深度递归/广度递归进行递归函数调用,本地变量的内存消耗可能会变得很大。我走了一条不同的路线: 我希望记忆会有用。显然不是 我运行一个vba脚本,该脚本遍历多个文件(因为access不能处理太大的文件);并将它们附加到一个表中,对其进行转换,然后写出摘要 脚本在文件中循环并针对每个文件运行宏 快速的解决方法是从任务管理器中提取内存使用量,如果它超过1GB;暂停子例程,以便没有损坏的记录进入 我们如何做到这一点 使用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
dim内存尽可能长
内存=内存使用率
'1000000~1GB
如果内存>1000000,则
端接头
如果结束
=================================================
[文件路径]=“C:\..\ShellOutputfile.txt”
函数内存使用情况()尽可能长
暗线一样长
作为字符串的Dim linestring
Shell“tasklist/fi”和“IMAGENAME EQ MSACCESS.EXE”&“&”>“&”[文件路径]”
'获取\u列表\u数据
lines=CInt(获取列表数据(“[path to file]”,1,0))
linestring=获取列表数据(“[文件路径]”,2,4)
linestring=右侧(linestring,11)
linestring=Replace(linestring,“K”和“)”K
linestring=Replace(linestring,“”,“”)
行=CLng(行字符串)
内存使用=行
端函数
=============================
公共函数get_将_数据(路径为字符串,选项为整数,读取类型为整数)列为字符串
'在变量中进行解析,您需要该变量的值。
读取常数=1,写入常数=2,外观常数=8
尺寸tmp_变量_str作为字符串
Dim fso,ts,fileObj,文件名
将文本行变暗为字符串
将tmp_结果设置为字符串
将TMP_路径设置为字符串
Dim TMP选择为整数
将tor设置为整数
作为整数的Dim计数器
'读取类型决定使用什么循环
'读取类型=0;绕过;>0,您正在选择要读取的行。
计数器=0
TMP_路径=路径
tmp_var_str=var_str
tmp_结果=“”
tor=阅读类型
'选项=1(数行)
'选项=2(读取特定行)
tmpchoice=选择
'创建文件,并获取文件的文件对象。
如果右(路径,1)=“\”,则TMP\U路径=左(路径,Len(路径)-1)
filename=TMP_PATH'&“\Profit_Recognition.ini”
设置fso=CreateObject(“Scripting.FileSystemObject”)
设置fileObj=fso.GetFile(文件名)
'打开文本流以进行输出。
设置ts=fileObj.OpenAsTextStream(ForReading,TristateUseDefault)
当ts.AtEndOfStream为True时执行此操作
如果tmpchoice=1,则
计数器=计数器+1
textline=ts.ReadLine
tmp_结果=CStr(计数器)
如果结束
如果tmpchoice=2,则
计数器=计数器+1
tmp_结果=ts.ReadLine
如果计数器=tor,则
退出Do
如果结束
如果结束
环
获取\u列表\u数据=tmp\u结果
端函数
这个问题毫无意义。CLR的内存管理器使用LIFO
-Read-和+1隐式地管理局部变量(创建/释放),但有一点需要更正:在VBA中,实际上不能将字符串变量设置为无。对象类型只能使用Nothing
,而String
是VBA中的基本类型s=vbNullString
是您需要的(从答案中的aivosto.com链接)