Nate said:
The leveling off of the memory consumption versus continual degredation
leads me to believe that Microsoft hasn't changed their kb to your point of
view because it would be incorrect. What we're probably looking at is a Jet
cache in memory, which is refreshed on subsequent calls (hence the stability
following call #1).
See [Microsoft's] Russ Gray's post here:
http://groups-beta.google.com/group/microsoft.public.data.ado/msg/3e0021430755ed35
And the following:
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q247140
Nate, Where's that healthy (or otherwise) cynicism of yours from the
other thread? You distrust their VB garbage collector based on no
evidence (design faults in DAO, Excel and other components cannot be
attributed to the garbage collector), and no testing or observations of
your own. Yet in this thread you observe the memory leak for yourself
but then you go running back to Microsoft to try to explain it away.
Think about it, I mean, think about it *yourself*: if the Jet cache is
the explanation, why does usage climb when the workbook is open but
remain stable when the workbook is closed? You may be seeing some
stabilization/recovery but do you get (close to) everything back when
done?
I changed my test (I'll post the exact code and output at the end) to
do 50 iterations, the first 10 with the workbook open, the next 10 with
it closed, the next 10 with it open etc. As before I was using a
connection to an mdb jet database. I closed and released the ADO
Connection on each iteration.
As expected, with the workbook open I saw the usage steadily increase
on each of the first 10 iterations. For iteration 11, the first with
the workbook closed, there was a recovery but at a level much increased
from the original amount used by Excel. For iterations 11 to 20 with
the workbook closed the usage was *exactly* the same each time. For
iteration 21, with the workbook open again, there was an immediate and
significant rise in usage and it kept rising for the next nine
iterations. This pattern continued to the end.
I immediately ran the code again and the usage hadn't recovered much
from the last iteration i.e. even closing and releasing the Excel
application didn't get the memory back.
This looks exactly like a memory leak to me i.e. I have closed and
released everything and AFAIK flushed the Jet cache in the process but
I haven't got the memory back. If not a memory leak then what is it?
And how to I get this non-leaking yet absent memory back?
someone recently told me that the proliferation of myths flies in the
face of 'the Usenet way', so I might advise against posting as such.
Actually, I told you can expect people to jump on your case if your
sole piece of 'advice' is based on superstition rather than fact.
I may have made a mistake somewhere but I'm making my assertions in
good faith based on my observations. I am not trying to be contrary for
the sake of it. I am open-minded to being wrong and, as I said before,
I hope I am wrong. I've posted my code and my output (below). In what
way do you think I am trying to invent or repeat mythology?
Here is my output in full:
Memory used by Excel: 2166784 <<< original
Memory used by Excel: 9183232 <<< iteration 1: workbook open
Memory used by Excel: 9490432
Memory used by Excel: 9814016
Memory used by Excel: 10125312
Memory used by Excel: 10440704
Memory used by Excel: 10760192
Memory used by Excel: 11083776
Memory used by Excel: 11407360
Memory used by Excel: 11710464
Memory used by Excel: 12013568
Memory used by Excel: 8048640 <<< iteration 11: workbook closed
Memory used by Excel: 8048640
Memory used by Excel: 8048640
Memory used by Excel: 8048640
Memory used by Excel: 8048640
Memory used by Excel: 8048640
Memory used by Excel: 8048640
Memory used by Excel: 8048640
Memory used by Excel: 8048640
Memory used by Excel: 8048640
Memory used by Excel: 12644352
Memory used by Excel: 12939264
Memory used by Excel: 13238272
Memory used by Excel: 13541376
Memory used by Excel: 13844480
Memory used by Excel: 14151680
Memory used by Excel: 14450688
Memory used by Excel: 14757888
Memory used by Excel: 15065088
Memory used by Excel: 15372288
Memory used by Excel: 11395072
Memory used by Excel: 11395072
Memory used by Excel: 11395072
Memory used by Excel: 11395072
Memory used by Excel: 11395072
Memory used by Excel: 11395072
Memory used by Excel: 11395072
Memory used by Excel: 11395072
Memory used by Excel: 11395072
Memory used by Excel: 11395072
Memory used by Excel: 15953920
Memory used by Excel: 16236544
Memory used by Excel: 16535552
Memory used by Excel: 16838656
Memory used by Excel: 17145856
Memory used by Excel: 17444864
Memory used by Excel: 17752064
Memory used by Excel: 18055168
Memory used by Excel: 18358272
Memory used by Excel: 18657280
I immediate ran the code a second time:
Memory used by Excel: 14700544
Memory used by Excel: 19308544
Memory used by Excel: 19603456
Memory used by Excel: 19902464
Memory used by Excel: 20213760
Memory used by Excel: 20525056
Memory used by Excel: 20832256
Memory used by Excel: 21143552
Memory used by Excel: 21430272
Memory used by Excel: 21745664
Memory used by Excel: 22061056
Memory used by Excel: 22061056
Memory used by Excel: 22061056
Memory used by Excel: 22061056
Memory used by Excel: 22061056
Memory used by Excel: 22061056
Memory used by Excel: 22061056
Memory used by Excel: 22061056
Memory used by Excel: 22061056
Memory used by Excel: 22061056
Memory used by Excel: 22061056
Memory used by Excel: 22413312
Memory used by Excel: 22745088
Memory used by Excel: 23068672
Memory used by Excel: 23384064
Memory used by Excel: 23736320
Memory used by Excel: 24092672
Memory used by Excel: 24449024
Memory used by Excel: 24805376
Memory used by Excel: 25169920
Memory used by Excel: 25522176
Memory used by Excel: 25522176
Memory used by Excel: 25522176
Memory used by Excel: 25522176
Memory used by Excel: 25522176
Memory used by Excel: 25522176
Memory used by Excel: 25522176
Memory used by Excel: 25522176
Memory used by Excel: 25522176
Memory used by Excel: 25522176
Memory used by Excel: 25522176
Memory used by Excel: 25890816
Memory used by Excel: 26247168
Memory used by Excel: 26603520
Memory used by Excel: 26980352
Memory used by Excel: 27336704
Memory used by Excel: 27697152
Memory used by Excel: 28053504
Memory used by Excel: 28409856
Memory used by Excel: 28770304
Memory used by Excel: 29126656
Here is my code:
Option Explicit
Private Declare Function PdhVbOpenQuery Lib "pdh.dll" _
(ByRef QueryHandle As Long) As Long
Private Declare Function PdhCloseQuery Lib "pdh.dll" _
(ByVal QueryHandle As Long) As Long
Private Declare Function PdhVbAddCounter Lib "pdh.dll" _
(ByVal QueryHandle As Long, ByVal CounterPath As String, _
ByRef CounterHandle As Long) As Long
Private Declare Function PdhRemoveCounter Lib "pdh.dll" _
(ByVal CounterHandle As Long) As Long
Private Declare Function PdhCollectQueryData Lib "pdh.dll" _
(ByVal QueryHandle As Long) As Long
Private Declare Function PdhVbGetDoubleCounterValue Lib "pdh.dll" _
(ByVal CounterHandle As Long, ByRef CounterStatus As Long) As
Double
Private Declare Function PdhVbIsGoodStatus Lib "pdh.dll" _
(ByVal StatusValue As Long) As Long
Private Declare Function PdhVbGetOneCounterPath Lib "pdh.dll" _
(ByVal PathString As String, ByVal PathLength As Long, _
ByVal DetailLevel As Long, ByVal CaptionString As String) As Long
Private Declare Function PdhVbCreateCounterPathList Lib "pdh.dll" _
(ByVal DetailLevel As Long, ByVal CaptionString As String) As Long
Private Declare Function PdhVbGetCounterPathFromList Lib "pdh.dll" _
(ByVal Index As Long, ByVal Buffer As String, _
ByVal BufferLength As Long) As Long
Private Declare Function PdhVbGetCounterPathElements Lib "pdh.dll" _
(ByVal PathString As String, ByVal MachineName As String, _
ByVal ObjectName As String, ByVal InstanceName As String, _
ByVal ParentInstance As String, ByVal CounterName As String, _
ByVal BufferSize As Long) As Long
Private Sub Command1_Click()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim i As Integer
Dim j As Integer
Dim max As Integer
Dim r As Long
Dim hPDHQuery As Long 'Handle to performance monitor
query
Dim hPDHCounter As Long 'Handle to performance monitor
counter
Dim strCounterPath 'Path to performance monitor
counter
Dim lngCounterStatus As Long 'Status of counter when checked
Dim dblPrivateBytes As Double 'Value of counter when checked
' Open Excel
Dim xl As Object
Set xl = CreateObject("Excel.Application")
'Initialize PDH query object.
r = PdhVbOpenQuery(hPDHQuery)
'Initialize counter.
strCounterPath = "\\JAMIEC2\Process(Excel)\Private Bytes"
r = PdhVbAddCounter(hPDHQuery, strCounterPath, hPDHCounter)
'Gather data.
r = PdhCollectQueryData(hPDHQuery)
'Get counter value and process data.
dblPrivateBytes = PdhVbGetDoubleCounterValue(hPDHCounter, _
lngCounterStatus)
If PdhVbIsGoodStatus(lngCounterStatus) Then
Debug.Print "Memory used by Excel: " & CLng(dblPrivateBytes)
Else
Debug.Print "Invalid data."
End If
max = 10 * 5
ReDim alngPrivateBytes(max)
Dim wb As Object
Dim strTargetWB As String
For i = 1 To 5
For j = 1 To 10
If j = 1 Then
If i Mod 2 = 1 Then
Set wb = xl.Workbooks.Open("C:\db.xls")
Else
wb.Close False
End If
End If
Set cn = New ADODB.Connection
cn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Tempo\New_Jet_DB.mdb"
cn.CursorLocation = adUseClient
cn.Open
strTargetWB = "C:\db" & ((i - 1) * 10) + j & ".xls"
On Error Resume Next
Kill strTargetWB
On Error GoTo 0
cn.Execute _
"SELECT * INTO" & _
"[Excel 8.0;Database=" & strTargetWB & ";]" & _
".test FROM [Excel 8.0;Database=C:\db.xls;].[Sheet1$]"
r = PdhCollectQueryData(hPDHQuery)
'Get counter value and process data.
dblPrivateBytes = PdhVbGetDoubleCounterValue(hPDHCounter, _
lngCounterStatus)
'Process data.
If lngCounterStatus = 0 Then
Debug.Print "Memory used by Excel: " &
CLng(dblPrivateBytes)
alngPrivateBytes(((i - 1) * 10)) = CLng(dblPrivateBytes)
Else
Debug.Print "Invalid data."
alngPrivateBytes(0) = 0
End If
cn.Close
Set cn = Nothing ' just for Nate <g>
Next
Next
' Debug.Print "Total increase for " & max & " iterations: " & _
' (CStr(alngPrivateBytes(max) - alngPrivateBytes(0)))
' Debug.Print "Average increase per iteration: " & _
' (CStr((alngPrivateBytes(max) - alngPrivateBytes(0))) / max)
'Clean up.
r = PdhRemoveCounter(hPDHCounter)
r = PdhCloseQuery(hPDHQuery)
' Close Excel
xl.Quit
Set xl = Nothing
End Sub
Jamie.
--