Memory keeps creaping up...why?

  • Thread starter Thread starter Jerry
  • Start date Start date
J

Jerry

I have a simple subroutine (see below) which reads a table and populates the items of a combobox. As the subroutine is executed, I'm watching the taskmanager and the memory in use increases; however, once I get to the Error_Exit: logic and start to close and dispose my memory variables, the memory is not released and does not decrease in the task manager. Why is that? Eventually, my program is giving me "Insufficient memory" errors.

****************************************************************************************

On Error GoTo Error_Handling

Dim cnn As New Odbc.OdbcConnection
Dim cmd As New Odbc.OdbcCommand
Dim rdr As Odbc.OdbcDataReader

cnn.ConnectionString = "dsn=" & accpac_companyid
cnn.Open()
cmd.Connection = cnn
cmd.CommandText = "select schedkey, scheddesc from cssktb order by schedkey"
rdr = cmd.ExecuteReader

Do While rdr.Read
combo.Items.Add(rdr("schedkey").ToString.Trim)
Loop

Error_Exit:
If Not IsNothing(rdr) Then
rdr.Close()
cmd.Connection.Close()
cmd.Dispose()
cnn.Close()
cnn.Dispose()
End If

rdr = Nothing
cmd = Nothing
cnn = Nothing
Exit Sub

Error_Handling:
Select Case Err.Number
Case Else
MsgBox("The following error occured in LoadSchedules()" & vbCrLf & Err.Description)
End Select

Resume Error_Exit
 
You should try profiling with a memory profiler to see who is holding the objects, if actually somebody is holding them.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
I have a simple subroutine (see below) which reads a table and populates the items of a combobox. As the subroutine is executed, I'm watching the taskmanager and the memory in use increases; however, once I get to the Error_Exit: logic and start to close and dispose my memory variables, the memory is not released and does not decrease in the task manager. Why is that? Eventually, my program is giving me "Insufficient memory" errors.

****************************************************************************************

On Error GoTo Error_Handling

Dim cnn As New Odbc.OdbcConnection
Dim cmd As New Odbc.OdbcCommand
Dim rdr As Odbc.OdbcDataReader

cnn.ConnectionString = "dsn=" & accpac_companyid
cnn.Open()
cmd.Connection = cnn
cmd.CommandText = "select schedkey, scheddesc from cssktb order by schedkey"
rdr = cmd.ExecuteReader

Do While rdr.Read
combo.Items.Add(rdr("schedkey").ToString.Trim)
Loop

Error_Exit:
If Not IsNothing(rdr) Then
rdr.Close()
cmd.Connection.Close()
cmd.Dispose()
cnn.Close()
cnn.Dispose()
End If

rdr = Nothing
cmd = Nothing
cnn = Nothing
Exit Sub

Error_Handling:
Select Case Err.Number
Case Else
MsgBox("The following error occured in LoadSchedules()" & vbCrLf & Err.Description)
End Select

Resume Error_Exit
 
I've tried profiling the code with AntsProfiler (and many others), but I
have NO IDEA what I'm looking at there. Am I wrong in assuming the memory
in use should return to the value before the subroutine executes if all the
variables are terminated properly?

"Miha Markic" <miha at rthand com> wrote in message You should try profiling with a memory profiler to see who is holding the objects, if actually somebody is holding them.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
I have a simple subroutine (see below) which reads a table and populates the items of a combobox. As the subroutine is executed, I'm watching the taskmanager and the memory in use increases; however, once I get to the Error_Exit: logic and start to close and dispose my memory variables, the memory is not released and does not decrease in the task manager. Why is that? Eventually, my program is giving me "Insufficient memory" errors.

****************************************************************************************

On Error GoTo Error_Handling

Dim cnn As New Odbc.OdbcConnection
Dim cmd As New Odbc.OdbcCommand
Dim rdr As Odbc.OdbcDataReader

cnn.ConnectionString = "dsn=" & accpac_companyid
cnn.Open()
cmd.Connection = cnn
cmd.CommandText = "select schedkey, scheddesc from cssktb order by schedkey"
rdr = cmd.ExecuteReader

Do While rdr.Read
combo.Items.Add(rdr("schedkey").ToString.Trim)
Loop

Error_Exit:
If Not IsNothing(rdr) Then
rdr.Close()
cmd.Connection.Close()
cmd.Dispose()
cnn.Close()
cnn.Dispose()
End If

rdr = Nothing
cmd = Nothing
cnn = Nothing
Exit Sub

Error_Handling:
Select Case Err.Number
Case Else
MsgBox("The following error occured in LoadSchedules()" & vbCrLf & Err.Description)
End Select

Resume Error_Exit
 
Well, no, if your combo is a global variable and you don't clear its items.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
I've tried profiling the code with AntsProfiler (and many others), but I
have NO IDEA what I'm looking at there. Am I wrong in assuming the memory
in use should return to the value before the subroutine executes if all the
variables are terminated properly?

"Miha Markic" <miha at rthand com> wrote in message You should try profiling with a memory profiler to see who is holding the objects, if actually somebody is holding them.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
I have a simple subroutine (see below) which reads a table and populates the items of a combobox. As the subroutine is executed, I'm watching the taskmanager and the memory in use increases; however, once I get to the Error_Exit: logic and start to close and dispose my memory variables, the memory is not released and does not decrease in the task manager. Why is that? Eventually, my program is giving me "Insufficient memory" errors.

****************************************************************************************

On Error GoTo Error_Handling

Dim cnn As New Odbc.OdbcConnection
Dim cmd As New Odbc.OdbcCommand
Dim rdr As Odbc.OdbcDataReader

cnn.ConnectionString = "dsn=" & accpac_companyid
cnn.Open()
cmd.Connection = cnn
cmd.CommandText = "select schedkey, scheddesc from cssktb order by schedkey"
rdr = cmd.ExecuteReader

Do While rdr.Read
combo.Items.Add(rdr("schedkey").ToString.Trim)
Loop

Error_Exit:
If Not IsNothing(rdr) Then
rdr.Close()
cmd.Connection.Close()
cmd.Dispose()
cnn.Close()
cnn.Dispose()
End If

rdr = Nothing
cmd = Nothing
cnn = Nothing
Exit Sub

Error_Handling:
Select Case Err.Number
Case Else
MsgBox("The following error occured in LoadSchedules()" & vbCrLf & Err.Description)
End Select

Resume Error_Exit
 
All you should need to do is call close on the reader and the connection.
After that the garbage collector should take care of the unused objects.

I'm a tad confused as to why you are using the old style VB 6 error
handling instead of using a Try Catch block. I'd try to run away as fast as
possible from using that type of error handling. It's clunky and doesn't
really give you very good information when you're trying to debug an error.

If worse comes to worse you'll probably need to get a memory dump and see
if there are a lot of uncollected/rooted objects in memory.

It's also possible that you are leaking objects somewhere else rather than
in the section of code you have displayed.
-------------------------------------
Chris Alton, Microsoft Corp.
SQL Server Developer Support Engineer
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: "Jerry" <[email protected]>
Subject: Memory keeps creaping up...why?
Date: Tue, 23 Oct 2007 09:53:37 -0400
Lines: 143

I have a simple subroutine (see below) which reads a table and populates
the items of a combobox. As the subroutine is executed, I'm watching the
taskmanager and the memory in use increases; however, once I get to the
Error_Exit: logic and start to close and dispose my memory variables, the
memory is not released and does not decrease in the task manager. Why is
that? Eventually, my program is giving me "Insufficient memory" errors.
 
Back
Top