Skip to Content
avatar image
Former Member

Memory Leak in DI Object

I am using following Code

static void Main(string[] args)
{
string xml = System.IO.File.ReadAllText("file.txt") ;
string err_msg, Key;
int err;
Integration.Integrator init = new Integration.Integrator();
Console.WriteLine(init.company.CompanyName);

var xdoc = XDocument.Parse(xml);
xdoc.Save("file.xml");

Console.WriteLine("Initial Memory={0}", System.Diagnostics.Process.GetCurrentProcess().PrivateMemorySize64);
Console.ReadKey();
object obj=null ;
while (true)
{
var company = init.company;

try
{

obj = init.company.GetBusinessObjectFromXML("file.xml", 0);
try { ((dynamic)obj).UserFields.Fields.Item("U_InStatus").Value = "I"; }
catch { }
err = ((dynamic)obj).Add();
Console.WriteLine("reachedAdd");
}
catch
{
}
finally
{
if (obj == null)
obj = init.company.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoBridge);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
obj = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

}

Console.ReadKey();
}

Console.ReadKey();
}

when I execute code at SAP Server it increases memory constantly. SAP B1 Version at client side is 9.1

I believe Garbage created by following line of code is not being collected by releaseComObject.

err = ((dynamic)obj).Add();

Please help

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Nov 25, 2016 at 12:43 PM

    Hi Muhammad,

    I think you are confusing the ReleaseComObject with the garbage collector.

    ReleaseComObject simply decreases the Com Reference count (https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.releasecomobject(v=vs.110).aspx).

    The garbage collector is responsible for reclaiming memory when objects are no longer used. However, if an object is used it can't collect that memory. It is possible in code to be unable to access an object, yet still be referencing it elsewhere (e.g.: event listeners);

    My initial suggestion (in your duplicate post) still stands, use a memory profiler to investigate what objects are remaining in memory, and take the necessary steps to make sure they get collected.

    Pedro Magueija

    LinkedIn | Twitter | Blog

    Add comment
    10|10000 characters needed characters exceeded