VBAmacros内存泄漏(如何清除variables=

我有一个VBAmacros脚本,刮一些数据。 这是使用MSIE擦伤它..我相信MSIE是内存泄漏的核心问题。

我正在初始化variables

Set IE = CreateObject("InternetExplorer.Application") 

我做了一个小testing,看看如何使用内存。

我做了一个循环,只有一个IE的实例,并ping同一个网站。 内存似乎不是韭菜。

然后,我做了一个循环,总是ping不同的网站和内存使用量开始增加与每个请求。

我也做了一个testing(我在下面发布),在每个迭代中创buildNEW对象,并在最后删除它。 删除部分似乎不工作。

看来IE的实例正在caching请求,所以对象越来越大。 这只是一个假设。

这是我用来testing泄漏的示例代码。

 Do While True Dim IE As Object Set IE = CreateObject("InternetExplorer.Application") IE.Navigate "https://www.google.hr/#hl=hr&gs_nf=1&cp=3&gs_id=8&xhr=t&q=" & Counter IE.Visible = True Do While IE.readyState <> 4 Or IE.Busy = True Application.Wait Now() + TimeValue("00:00:01") DoEvents Loop Application.Wait Now() + TimeValue("00:00:01") Counter = Counter + 1 Range("A" & Counter).Value = "https://www.google.hr/#hl=hr&gs_nf=1&cp=3&gs_id=8&xhr=t&q=" & Counter IE.Quit Set IE = Nothing Loop 

任何input将是伟大的!

我testing了上面的代码,并正确销毁IE对象。 也是在这方面

看来IE的实例正在caching请求,所以对象越来越大。 这只是一个假设。

是的,有时会增加,但并不总是。 看截图。

在这里输入图像说明

这是IE 8环的任务pipe理器的截图。 它显示出增加,但如果你看到它也把它降下来。 所以我相信你所看到的不是内存泄漏。

编辑

这是我在我的数据库(我没有写它)的一些代码,但你可以运行它来检查内存使用情况。

 Sub Sample() Do While True Dim IE As Object Set IE = CreateObject("InternetExplorer.Application") IE.Navigate "https://www.google.hr/#hl=hr&gs_nf=1&cp=3&gs_id=8&xhr=t&q=" & Counter IE.Visible = False Debug.Print GetProcessMemory("iexplore.exe") Do While IE.readyState <> 4 Or IE.Busy = True Application.Wait Now() + TimeValue("00:00:01") DoEvents Loop Application.Wait Now() + TimeValue("00:00:01") Counter = Counter + 1 Range("A" & Counter).value = "https://www.google.hr/#hl=hr&gs_nf=1&cp=3&gs_id=8&xhr=t&q=" & Counter IE.Quit Set IE = Nothing Loop End Sub Private Function GetProcessMemory(ByVal app_name As String) As String Dim Process As Object, dMemory As Double For Each Process In GetObject("winmgmts:"). _ ExecQuery("Select WorkingSetSize from Win32_Process Where Name = '" & app_name & "'") dMemory = Process.WorkingSetSize Next If dMemory > 0 Then GetProcessMemory = ResizeKb(dMemory) Else GetProcessMemory = "0 Bytes" End If End Function Private Function ResizeKb(ByVal b As Double) As String Dim bSize(8) As String, i As Integer bSize(0) = "Bytes" bSize(1) = "KB" 'Kilobytes bSize(2) = "MB" 'Megabytes bSize(3) = "GB" 'Gigabytes bSize(4) = "TB" 'Terabytes bSize(5) = "PB" 'Petabytes bSize(6) = "EB" 'Exabytes bSize(7) = "ZB" 'Zettabytes bSize(8) = "YB" 'Yottabytes For i = UBound(bSize) To 0 Step -1 If b >= (1024 ^ i) Then ResizeKb = ThreeNonZeroDigits(b / (1024 ^ _ i)) & " " & bSize(i) Exit For End If Next End Function Private Function ThreeNonZeroDigits(ByVal value As Double) As Double If value >= 100 Then ThreeNonZeroDigits = FormatNumber(value) ElseIf value >= 10 Then ThreeNonZeroDigits = FormatNumber(value, 1) Else ThreeNonZeroDigits = FormatNumber(value, 2) End If End Function 

快照

在这里输入图像说明