excel - VBA Macro Memory Leak (how to clear variable= -
i have vba macro script scrapes data. it's scrapes using msie.. believe msie core problem memory leakage.
i'm initializing variable like
set ie = createobject("internetexplorer.application")
i made little test see how memory being used.
i made loop makes 1 instance of ie , pings same website. memory doesn't seem leek.
then made loop ping different site , memory usage started increasing each request.
i made test (i'm posting below) creates new object in every iteration , deletes on end. deleting part doesn't seem work.
it seems instance of ie caching requests object getting bigger. assumption.
here's sample code used test leakage.
do while true dim ie 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 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
any input great!
i tested above code , destroying ie object correctly. in regards this
it seems instance of ie caching requests object getting bigger. assumption.
yes increases not always. see screenshot.
this screenshot of task manager ie 8 loops. shows increase if see brings down. believe seeing not memory leak.
edit
here code had in databank (i didn't write it) can run check memory usage.
sub sample() while true dim ie 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") 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 string) string dim process object, dmemory double each process in getobject("winmgmts:"). _ execquery("select workingsetsize win32_process name = '" & app_name & "'") dmemory = process.workingsetsize next if dmemory > 0 getprocessmemory = resizekb(dmemory) else getprocessmemory = "0 bytes" end if end function private function resizekb(byval b double) string dim bsize(8) string, 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 = ubound(bsize) 0 step -1 if b >= (1024 ^ i) resizekb = threenonzerodigits(b / (1024 ^ _ i)) & " " & bsize(i) exit end if next end function private function threenonzerodigits(byval value double) double if value >= 100 threenonzerodigits = formatnumber(value) elseif value >= 10 threenonzerodigits = formatnumber(value, 1) else threenonzerodigits = formatnumber(value, 2) end if end function
snapshot
Comments
Post a Comment