none
Memory Management issue while calling COM method from .Net component RRS feed

  • Question

  • Hi all,

     The Com Component Method say(GetChildresponse) requires Byte pointer as input. From .Net Component, while calling GetChildResponse  I am creating IntPtr and passing it to Component. 

    I am only creating the pointer in my .Net Component but allocation of bytes(CoTaskMemAlloc) happens at COM Method. After populating the pointer buffer, I am transferring to memory buffer in .Net method and Free the memorry of the pointer using "Marshal.FreeCoTaskMem" method.  Still I am not sure, whether it is deallocating the memory since the Garbarge collector wont happen immediately. 

    We are hitting on performance issue while doing load testing and we suspect this might be due to memory allocation that is not getting deallocated properly.

    Any pointers would be helpful.

    Thanks,

    Nithya

    Sample Code:

    unsafe

    {

     

    // Get the physical memory address

    ocpHeaderBufferPointer =

    new IntPtr((void*)&ocpHeaderBufferPointer);

    temporaryResponseBufferPointer =

    new IntPtr((void*)&temporaryResponseBufferPointer);

    }

     

    try

    {

     

    try

    {

     

    // Call the GetResponse of the pass through ocpChannel

     

    new OCPChannelDSClass().GetResponse((uint)transactionId, ocpHeaderBufferPointer, out ocpHeaderBufferSize,

    temporaryResponseBufferPointer,

    out temporaryResponseBufferSize, (uint)timeout);

    }

     

    catch (Exception ex)

    {

     

     

    // Populate the local arrays with the received data

     

    if (ocpHeaderBufferSize > 0)

    {

    ocpHeaderBuffer =

    new byte[ocpHeaderBufferSize];

     

    Marshal.Copy(ocpHeaderBufferPointer, ocpHeaderBuffer, 0, ocpHeaderBuffer.Length);

    }

     

    if (temporaryResponseBufferSize > 0)

    {

    temporaryResponseBuffer =

    new byte[temporaryResponseBufferSize];

     

    Marshal.Copy(temporaryResponseBufferPointer, temporaryResponseBuffer, 0, temporaryResponseBuffer.Length);

    }

    }

     

    catch (Exception ex)

    {

     

    }

     

    finally

    {

     

    // free the memory

     

    Marshal.FreeHGlobal(ocpHeaderBufferPointer);

     

    Marshal.FreeHGlobal(temporaryResponseBufferPointer);

    }

     

    Thursday, September 2, 2010 12:53 PM

Answers

  • The allocator used with COM is usually Marshal.AllocCoTaskMem / Marshal.FreeCoTaskMem.  The code you show uses the HGlobal ones.  I can't say for sure what to do, you haven't showed enough code to tell.

    > We are hitting on performance issue while doing load testing

    Is the memory usage of your process getting very large and your system paging heavily to disk?  This would signify a leak that is decimating performance.

     

    • Marked as answer by SamAgain Thursday, September 9, 2010 9:34 AM
    Thursday, September 2, 2010 11:22 PM