forked from Workbooks.Open causes memory leak
Background
I'm investigating a cause of memory leak happened on the following code and found that it happens when excel doesn't respond. After a while running this code I see excel's process is 'not responding' on task manager and process starts devouring memory.
Sub Test01()
Dim fso As FileSystemObject
Set fso = New FileSystemObject
Dim f As File
Dim wb As Workbook
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Dim Files As Variant
Set Files = fso.GetFolder("*****").Files
For Each f In Files
Set wb = Workbooks.Open(f.Path)
wb.Close savechanges:=False
Set wb = Nothing
Next
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
When I add DoEvents
in loop excel never freezes and memory usage increases slowly.
For Each f In Files
DoEvents
Set wb = Workbooks.Open(f.Path)
wb.Close savechanges:=False
Set wb = Nothing
Next
Question
Why memory usage increases when process is freezing?
Remarks
Please note that;
folder contains 3000 excel files so that loop is done 3000 times
file size of excel files is less than 15kb