【发布时间】:2015-11-15 22:47:44
【问题描述】:
我有一个带有多个 userforms 的 excel 程序,用于从主工作表中的数据创建文本文件,以及将数据导入到多个其他工作表中,以及从导入的数据中创建图表。
我一直遇到内存泄漏问题,只是确保在subfunctions 结束之前将所有对象设置为空。这似乎没有帮助。
所有函数运行完成后,内存使用率似乎一直很高,并且运行频率越高,它们的速度就越慢。
我怀疑内存泄漏可能是以下原因之一:
- 导入数据时使用
TextToColumn方法。不确定函数完成后这是否会堵塞内存。
代码如下所示:
Set Temp = Workbooks.Open(FileLocation)
With ThisWorkbook.Sheets(ShtName)
Temp.Sheets(1).Cells.Copy .Cells
.Columns(1).TextToColumns Destination:=.Range("A1"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Space:=True, _
TrailingMinusNumbers:=True
.Columns(1).Delete
Temp.Close SaveChanges:=False
End With
Set Temp = Nothing
-
创建数据数组(大约 1 x 60)并将它们写入文本文件。也不确定这是否会在函数结束后清除。
DataList = Application.Transpose( _ Application.Transpose( _ .Range(.Cells(R, Lines_C_datastart), _ .Cells(R, Lines_C_dataend)))) If SheetWriter(ShtName, DataList()) Then SheetCreator = True 全局变量。我创建了一些短函数来表现得像全局常量,但它们根据特定标志在两个值之间变化。在这些我
Set函数返回值到一个值。
我想知道这是否会导致内存问题,因为没有机会将Set 它返回到Nothing。它们长这样,而且有很多:
Function INPUT_FOLDERNAME() As String
Select Case SIMTYPE_KEY
Case T_SIMTYPE_A: INPUT_FOLDERNAME = "input_files_A"
Case T_SIMTYPE_B: INPUT_FOLDERNAME = "input_files_B"
End Select
End Function
Some of them are ListBox object functions that I made just for convenience in working with the names (not having to have the module name included) and I'm thinking these may pose a problem:
Function LINES_BOX() As MSForms.ListBox
Set LINES_BOX = Exporter.LINES_BOX
End Function
(This way, I just use LINES_BOX anywhere in the project instead of Exporter.LINES_BOX.)
If anyone can see a particular issue that may be contributing to memory leak, or knows what the common causes are, any help would be appreciated.
【问题讨论】:
-
Setting objects to nothing is rarely necessary - 我见过的绝大多数对象没有被释放的情况是由于表单处理不当造成的。你有什么迹象表明你正在泄漏内存?
-
基本上每次运行程序都可以看到exvel实例的物理内存使用量跳起来一直保持。有时高达 350K。该程序也会变慢并变得迟缓。 Tbh,我不确定程序使用的正常内存量是多少。
标签: vba excel memory-leaks