Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

VB.NET Memory leak when using "DimAs"?

Former Member
0 Kudos
150

Hi everyone,

Ive been fiddeling now for a couple of days with my material interface for SAP and came across a memory leak.

The material interface transfers material data into SAP and runs 24/7. The interface itself works fine, but we have to restart it every 12 hours, because the interfaces then uses arround 600MB Memory and will continue to grow until we restart it.

To determine the memory leak Ive sepereated the different code parts and found out, that it has something to do with either "ReleaseComObject" or the "DimAs" Routine. Here are the problematic lines:

Private Sub InitSAPBAPI(ByRef objSAPBAPI As Object)

	Dim SAPBAPIInitError As New Exception("SAPBAPIInitError")

	Try

	    objStandardMaterial = objSAPBAPI.GetSAPObject("StandardMaterial")
	    objHeadData = objSAPBAPI.DimAs(objStandardMaterial, "SaveData", "HeadData")

	Catch ex As Exception
		Throw SAPBAPIInitError
	End Try

End Sub
Private Sub ClearSAPObjects()

	ReleaseObject_and_GC(objHeadData)

	ReleaseObject_and_GC(objStandardMaterial)

End Sub
Private Sub ReleaseObject_and_GC(ByRef objObject As Object)
	Try
		Do
			System.Runtime.InteropServices.Marshal.ReleaseComObject(objObject)
		Loop While System.Runtime.InteropServices.Marshal.ReleaseComObject(objObject) >= 0
	Catch ex As Exception
	End Try
		
	objObject = Nothing
	GC.Collect()
	GC.WaitForPendingFinalizers()
End Sub

The Start() Routine loops a thousand times and makes a login, gets the BAPI definition, destroys it again and disconnects. This is basically how the interface works the whole day.

Now, if I comment out the line containing "objHeadData" the interface works just fine. But as soon as I uncomment the line, the memory keeps to grow.

So, what am I doing wrong?

Regards,

Michael Weinstabl

Edited by: Michael Weinstabl on Jun 1, 2010 6:42 PM

1 REPLY 1

Former Member
0 Kudos
79

bump