473,395 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Memory Leaking in VB.NET

Hi all,

I have a VB.NET Dll that is invoked via BizTalk 2002 AIC over Http
protocol.

the Dll is making a connection using a 3rd party connector to a Unidata
database (old legacy stuff) All I am doing is creating an instance of
the class and then using its two methods (Connect CloseConnection)

and then setting the instance to Nothing.

However I am experiencing a big memory leak when I run stress test on
my DLL (the DLLHOST.EXE for the particular DLL goes up and up until it
crashes the server)

I have researched Garbage collector and used it but it doesnt help...

Here Is my code

Dim moudconn As New CUniData
moudconn.Connect(strResult)

moudconn.CloseConnection()

moudconn = Nothing

GC.Collect()
Dispose()

Can anybode see anything I am doing wrong??

Feb 21 '06 #1
7 1735
Hi Brano,

Does your moudconn object have a dispose method?

If so call that before setting it to Nothing,

moudconn.Dispose()
moudconn = Nothing

... Hope this helps.

Nick.

"Brano" <bp****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi all,

I have a VB.NET Dll that is invoked via BizTalk 2002 AIC over Http
protocol.

the Dll is making a connection using a 3rd party connector to a Unidata
database (old legacy stuff) All I am doing is creating an instance of
the class and then using its two methods (Connect CloseConnection)

and then setting the instance to Nothing.

However I am experiencing a big memory leak when I run stress test on
my DLL (the DLLHOST.EXE for the particular DLL goes up and up until it
crashes the server)

I have researched Garbage collector and used it but it doesnt help...

Here Is my code

Dim moudconn As New CUniData
moudconn.Connect(strResult)

moudconn.CloseConnection()

moudconn = Nothing

GC.Collect()
Dispose()

Can anybode see anything I am doing wrong??

Feb 21 '06 #2
Hi, It doesnt actually...

But I can access this class and reprogram it.

I have tried to add the microsoft code to do that which is:

Public MustInherit Class CUniDataBase
Implements IDisposable

' NB - ALWAYS CLOSE CONNECTION ON EXIT

' Pointer to an external unmanaged resource.
Private handle As IntPtr
' Other managed resource this class uses.
Private component As Component
' Track whether Dispose has been called.
Private disposed As Boolean = False

' The class constructor.
Public Sub New(ByVal handle As IntPtr)
Me.handle = handle
End Sub

' Implement IDisposable.
' Do not make this method virtual.
' A derived class should not be able to override this method.
Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
' This object will be cleaned up by the Dispose method.
' Therefore, you should call GC.SupressFinalize to
' take this object off the finalization queue
' and prevent finalization code for this object
' from executing a second time.
GC.SuppressFinalize(Me)
End Sub

' Dispose(bool disposing) executes in two distinct scenarios.
' If disposing equals true, the method has been called directly
' or indirectly by a user's code. Managed and unmanaged
resources
' can be disposed.
' If disposing equals false, the method has been called by the
' runtime from inside the finalizer and you should not
reference
' other objects. Only unmanaged resources can be disposed.
Private Overloads Sub Dispose(ByVal disposing As Boolean)
' Check to see if Dispose has already been called.
If Not Me.disposed Then
' If disposing equals true, dispose all managed
' and unmanaged resources.
If disposing Then
' Dispose managed resources.
component.Dispose()
End If

' Call the appropriate methods to clean up
' unmanaged resources here.
' If disposing is false,
' only the following code is executed.
CloseHandle(handle)
handle = IntPtr.Zero
End If
disposed = True
End Sub

' Use interop to call the method necessary
' to clean up the unmanaged resource.
<System.Runtime.InteropServices.DllImport("Kernel3 2")> _
Private Shared Function CloseHandle(ByVal handle As IntPtr) As
[Boolean]
End Function

' This finalizer will run only if the Dispose method
' does not get called.
' It gives your base class the opportunity to finalize.
' Do not provide finalize methods in types derived from this
class.
Protected Overrides Sub Finalize()
' Do not re-create Dispose clean-up code here.
' Calling Dispose(false) is optimal in terms of
' readability and maintainability.
Dispose(False)
MyBase.Finalize()
End Sub
But if I cann the dispose now I get Object reference not set to an
instance of an object error...

;o(

Feb 21 '06 #3
Brano,

Review this snippet and see if you can make use of the concepts within.

Public MustInherit Class CUniDataBase
Implements IDisposable

Private disposedValue As Boolean = False ' To detect redundant calls
Private myLegacy As Object ' Pointer to legacy database
Private myConnection As String ' Holds connection information
Private isInitialized As Boolean ' State flag of the unmanaged
object

Public Sub New(ByVal ConnectionString As String)

myConnection = ConnectionString

'myLegacy is not init'ed yet
isInitialized = False

End Sub

Public Sub Connect()

If Not isInitialized Then
myLegacy = CreateObject("SomeContainer.SomeObject")
myLegacy.Connection = myConnection
myLegacy.Connect()
isInitialized = True
End If

End Sub

Public Sub Disconnect()

If isInitialized AndAlso Not myLegacy Is Nothing Then
'Free up pointers
myLegacy.Disconnect()
End If

myLegacy = Nothing
isInitialized = False

End Sub

#Region " IDisposable Support "

' This code added by Visual Basic to correctly implement the disposable
pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal disposing
As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub

' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: free unmanaged resources when explicitly called
Me.Disconnect()
End If
' TODO: free shared unmanaged resources
End If
Me.disposedValue = True
End Sub

Protected Overrides Sub Finalize()
Dispose(False)
MyBase.Finalize()
End Sub

#End Region

End Class
"Brano" <bp****@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Hi, It doesnt actually...

But I can access this class and reprogram it.

I have tried to add the microsoft code to do that which is:

Public MustInherit Class CUniDataBase
Implements IDisposable

' NB - ALWAYS CLOSE CONNECTION ON EXIT

' Pointer to an external unmanaged resource.
Private handle As IntPtr
' Other managed resource this class uses.
Private component As Component
' Track whether Dispose has been called.
Private disposed As Boolean = False

' The class constructor.
Public Sub New(ByVal handle As IntPtr)
Me.handle = handle
End Sub

' Implement IDisposable.
' Do not make this method virtual.
' A derived class should not be able to override this method.
Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
' This object will be cleaned up by the Dispose method.
' Therefore, you should call GC.SupressFinalize to
' take this object off the finalization queue
' and prevent finalization code for this object
' from executing a second time.
GC.SuppressFinalize(Me)
End Sub

' Dispose(bool disposing) executes in two distinct scenarios.
' If disposing equals true, the method has been called directly
' or indirectly by a user's code. Managed and unmanaged
resources
' can be disposed.
' If disposing equals false, the method has been called by the
' runtime from inside the finalizer and you should not
reference
' other objects. Only unmanaged resources can be disposed.
Private Overloads Sub Dispose(ByVal disposing As Boolean)
' Check to see if Dispose has already been called.
If Not Me.disposed Then
' If disposing equals true, dispose all managed
' and unmanaged resources.
If disposing Then
' Dispose managed resources.
component.Dispose()
End If

' Call the appropriate methods to clean up
' unmanaged resources here.
' If disposing is false,
' only the following code is executed.
CloseHandle(handle)
handle = IntPtr.Zero
End If
disposed = True
End Sub

' Use interop to call the method necessary
' to clean up the unmanaged resource.
<System.Runtime.InteropServices.DllImport("Kernel3 2")> _
Private Shared Function CloseHandle(ByVal handle As IntPtr) As
[Boolean]
End Function

' This finalizer will run only if the Dispose method
' does not get called.
' It gives your base class the opportunity to finalize.
' Do not provide finalize methods in types derived from this
class.
Protected Overrides Sub Finalize()
' Do not re-create Dispose clean-up code here.
' Calling Dispose(false) is optimal in terms of
' readability and maintainability.
Dispose(False)
MyBase.Finalize()
End Sub
But if I cann the dispose now I get Object reference not set to an
instance of an object error...

;o(

Feb 21 '06 #4
HI I think i have found the problem it is not my class because I have
rewritten the code from it to the main program and executed it and it
is creating the memory leek too.

The problem is I have a 3rd party class that doesnt have a dispose
method. It has however a OpenConnection method and a CloseConnection
method but I call both of these and the memory leak is still there. I
even set the object to Nothing afterwards but still the memory leek
appears. Here is snippet of my code :
Dim mobjUDConn As connection '# this object is declared in 3rd party
DLL that is referenced
Dim mobjUDDB As connDatabase '# this object is declared in 3rd
party DLL that is referenced

Const mcMultiDelim As String = "ý"
Const mcColumnDelim As String = "þ"

'# XXX means REMOVED FOR SECURITY PURPOUSES
Dim sHost As String = "XXX.x.XXX.xx"
Dim sPort As Int32 = XXXX
Dim sUser As String = String.Empty = "admin"
Dim sPassword As String = String.Empty = "XXXXXXX"
Dim sDatabase As String = String.Empty = "XXX-XXXXXXXX"

'#SUB CONNECT
Dim ErrMsg As String
Try
If mobjUDConn Is Nothing Then
mobjUDConn = New connection
End If
If mobjUDConn.isConnected = False Then
With mobjUDConn
.Host = sHost
.Port = sPort
.User = sUser
.Password = sPassword
.Database = sDatabase
mobjUDConn.open()
End With
End If
If mobjUDDB Is Nothing Then
mobjUDDB = New connDatabase
End If
If mobjUDDB.isConnected = False Then
mobjUDDB = mobjUDConn.getDatabase
End If
ErrMsg = ""
Catch ex As Exception
ErrMsg = ex.Message
End Try

'# Thats it connection opened
'# Do reads / writes here

'##### Close Connection ####
mobjUDDB.close()
'# ##### PROBLEM HERE #######
'# Problem is here mobjUDDB doesnt have a dispose method
mobjUDConn.close()
mobjUDConn.Dispose()

mobjUDDB = Nothing
mobjUDConn = Nothing

Feb 22 '06 #5
You might want to consider wrapping the 3rd party class that is leaking with
the USING functionality (in 2005). Another option to try is possibly wrap
the call and set up a second app domain which can be sandboxed. You then
create and destroy the app domain, thereby isolating the leak and disposing
of it. Granted there is a performance penalty with the second app domain
and the second domain must be stateless, but it might be a possibility.

Jim Wooley
HI I think i have found the problem it is not my class because I have
rewritten the code from it to the main program and executed it and it
is creating the memory leek too.

The problem is I have a 3rd party class that doesnt have a dispose
method. It has however a OpenConnection method and a CloseConnection
method but I call both of these and the memory leak is still there. I
even set the object to Nothing afterwards but still the memory leek
appears. Here is snippet of my code :

Dim mobjUDConn As connection '# this object is declared in 3rd
party
DLL that is referenced
Dim mobjUDDB As connDatabase '# this object is declared in 3rd
party DLL that is referenced
Const mcMultiDelim As String = "ý"
Const mcColumnDelim As String = "þ"
'# XXX means REMOVED FOR SECURITY PURPOUSES
Dim sHost As String = "XXX.x.XXX.xx"
Dim sPort As Int32 = XXXX
Dim sUser As String = String.Empty = "admin"
Dim sPassword As String = String.Empty = "XXXXXXX"
Dim sDatabase As String = String.Empty = "XXX-XXXXXXXX"
'#SUB CONNECT
Dim ErrMsg As String
Try
If mobjUDConn Is Nothing Then
mobjUDConn = New connection
End If
If mobjUDConn.isConnected = False Then
With mobjUDConn
.Host = sHost
.Port = sPort
.User = sUser
.Password = sPassword
.Database = sDatabase
mobjUDConn.open()
End With
End If
If mobjUDDB Is Nothing Then
mobjUDDB = New connDatabase
End If
If mobjUDDB.isConnected = False Then
mobjUDDB = mobjUDConn.getDatabase
End If
ErrMsg = ""
Catch ex As Exception
ErrMsg = ex.Message
End Try
'# Thats it connection opened
'# Do reads / writes here
'##### Close Connection ####
mobjUDDB.close()
'# ##### PROBLEM HERE #######
'# Problem is here mobjUDDB doesnt have a dispose method
mobjUDConn.close()
mobjUDConn.Dispose()
mobjUDDB = Nothing
mobjUDConn = Nothing

Feb 22 '06 #6
Hi sounds good but I have never done anything like it. An example or
link would be great...

thanks
Jim Wooley wrote:
You might want to consider wrapping the 3rd party class that is leaking with
the USING functionality (in 2005). Another option to try is possibly wrap
the call and set up a second app domain which can be sandboxed. You then
create and destroy the app domain, thereby isolating the leak and disposing
of it. Granted there is a performance penalty with the second app domain
and the second domain must be stateless, but it might be a possibility.

Jim Wooley
HI I think i have found the problem it is not my class because I have
rewritten the code from it to the main program and executed it and it
is creating the memory leek too.

The problem is I have a 3rd party class that doesnt have a dispose
method. It has however a OpenConnection method and a CloseConnection
method but I call both of these and the memory leak is still there. I
even set the object to Nothing afterwards but still the memory leek
appears. Here is snippet of my code :

Dim mobjUDConn As connection '# this object is declared in 3rd
party
DLL that is referenced
Dim mobjUDDB As connDatabase '# this object is declared in 3rd
party DLL that is referenced
Const mcMultiDelim As String = "ý"
Const mcColumnDelim As String = "þ"
'# XXX means REMOVED FOR SECURITY PURPOUSES
Dim sHost As String = "XXX.x.XXX.xx"
Dim sPort As Int32 = XXXX
Dim sUser As String = String.Empty = "admin"
Dim sPassword As String = String.Empty = "XXXXXXX"
Dim sDatabase As String = String.Empty = "XXX-XXXXXXXX"
'#SUB CONNECT
Dim ErrMsg As String
Try
If mobjUDConn Is Nothing Then
mobjUDConn = New connection
End If
If mobjUDConn.isConnected = False Then
With mobjUDConn
.Host = sHost
.Port = sPort
.User = sUser
.Password = sPassword
.Database = sDatabase
mobjUDConn.open()
End With
End If
If mobjUDDB Is Nothing Then
mobjUDDB = New connDatabase
End If
If mobjUDDB.isConnected = False Then
mobjUDDB = mobjUDConn.getDatabase
End If
ErrMsg = ""
Catch ex As Exception
ErrMsg = ex.Message
End Try
'# Thats it connection opened
'# Do reads / writes here
'##### Close Connection ####
mobjUDDB.close()
'# ##### PROBLEM HERE #######
'# Problem is here mobjUDDB doesnt have a dispose method
mobjUDConn.close()
mobjUDConn.Dispose()
mobjUDDB = Nothing
mobjUDConn = Nothing


Feb 24 '06 #7
http://msdn2.microsoft.com/en-us/lib...appdomain.aspx
Hi sounds good but I have never done anything like it. An example or
link would be great...

thanks

Jim Wooley wrote:
You might want to consider wrapping the 3rd party class that is
leaking with
the USING functionality (in 2005). Another option to try is possibly
wrap the call and set up a second app domain which can be sandboxed.
You then create and destroy the app domain, thereby isolating the
leak and disposing
of it. Granted there is a performance penalty with the second app
domain and the second domain must be stateless, but it might be a
possibility.

Jim Wooley

Feb 24 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Debian User | last post by:
Hi, I'm trying to discover a memory leak on a program of mine. I've taken several approaches, but the leak still resists to appear. First of all, I've tried to use the garbage collector to...
1
by: Ola Natvig | last post by:
Hi all I'm working with a long running, threaded server which serves HTTP requests with content which are passed through a XSLT processor. The XSLT processor I'm using is the Pyana processor. ...
18
by: Ramasubbu Ramasubramanian XR (AS/EAB) | last post by:
What is memory leakage, could any one explain with sample code
1
by: Rudy Meijer | last post by:
Hello, I made a class which read a key from the registry. This class is called every second in a timer event. Everything works fine but the class leaks memory. about 4Kb/s taskmanager. ...
0
by: Frank Lopez | last post by:
My program structure is: 1. 2. 3. => manually does the crt-init and crt-terminate calls 4. -- this is accessed by the unmanaged C++ classes in (3) using LoadLibrary and FreeLibrary
2
by: Dips | last post by:
Hello All, Does any of you know of any tool in the market to identify which Process/Software is leaking memory. The PDA is Samsung 730 , Pocket PC 2003 OS. The Device does not leak any memory if...
7
by: Ragnar Agustsson | last post by:
Hi all I have been wandering about the best way to sandbox memory leaks in 3rd party libraries when using them from the .Net framework. I have a 3rd party library, written in C++, that leaks a...
3
by: rupert.thurner | last post by:
the edgewall trac release 0.11 is blocked now since more than one month for a memory leak nobody is able to find, see http://groups.google.com/group/trac-dev/browse_thread/thread/116e519da54f16b...
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
6
by: ashjas | last post by:
Hello all.. I am experiencing memory leakes in on of my applications that i am developing. the % of memory used for the application is increasing as shown in the top command on linux. The...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.