locked
[WP8.1] [C#] Why can't OutOfMemoryException be caught in WRL RRS feed

  • Question

  • Here is the C# code that catches OOM periodically:

    try
    {
         //pData is byte[]
         myCxClass.Foo(pData, out iSize);
    }
    catch (OutOfMemoryException)
    {
        //It is caught here periodocally
    }

    Here is the WRL C++ code:

    MyCxClass::Foo(WriteOnlyArray<byte>^ pData, int* pSize)
    {
    	try
    	{
    		//do some work
    	}
    	catch (Exception^ ex)
    	{
                 //No exception has ever been caught here
    	}
    	catch (...)
    	{
                 //No exception has ever been caught here
    	}
    }
    Does the OOM happen during data marshalling? WriteOnlyArray is used specifically for reducing memory footprint.   Could anyone shed some light on this?


    Hong



    Tuesday, July 28, 2015 9:57 PM

Answers

  • Hi Hong,

    >>The C++ code is WRL not C++/CX

    No, it's absolutely C++/CX, see Windows Runtime C++ Template Library (WRL)

    Back to your question, let's see the figure below:

    As we can see, the Marshal must allocate a contiguous unmanaged memory to allow the unmanaged code to fill (as I tested, the memory address of the .NET array and the data pointer the unmanaged code to fill are different).

    And after the filling, the .NET need to allocate the managed memory to fill the data back from the Marshal memory. Theoretically, the memory will be double, this is the cost of the raw data communication between the managed code and unmanaged code.

    It is possible to get the out of memory exception when the array is very big.

    Considering the memory utilization of the application, I don’t recommended exchanging a lot of raw data between managed code and unmanaged code.

    Usually, we can use buffer to reduce the unmanaged memory cost , but in this case, since it requires a contiguous unmanaged memory, so using buffer will not suit.

    For high memory utilization when processing  a log of raw data, I recommend using unmanaged code.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 7, 2015 2:12 AM

All replies

  • I have confirmed that the OOM happens after MyCxClass::Foo(WriteOnlyArray<byte>^ pData, int* pSize) has finished its work. Does this mean the OOM happens during marshalling?  WriteOnlyArray<byte>^ pData is provided by the caller for the WRL method to fill, so the marshalling should not take much memory.

    Hong


    Wednesday, July 29, 2015 12:41 AM
  • Hi Hong,

    I'm discussing with our experts for this question, the preliminary conclusion is there are some inappropriate usages at the Interop part.

    I will come back if have any updates.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, July 29, 2015 12:12 PM
  • Thanks a lot, Franklin.  

    Look forward to the elucidation.


    Hong

    Wednesday, July 29, 2015 7:07 PM
  • Hi Franklin,

    I just want to make a correction.  The C++ code is WRL not C++/CX.  I do not know if it makes much difference.


    Hong


    Wednesday, July 29, 2015 11:05 PM
  • Hi Hong,

    >>The C++ code is WRL not C++/CX

    No, it's absolutely C++/CX, see Windows Runtime C++ Template Library (WRL)

    Back to your question, let's see the figure below:

    As we can see, the Marshal must allocate a contiguous unmanaged memory to allow the unmanaged code to fill (as I tested, the memory address of the .NET array and the data pointer the unmanaged code to fill are different).

    And after the filling, the .NET need to allocate the managed memory to fill the data back from the Marshal memory. Theoretically, the memory will be double, this is the cost of the raw data communication between the managed code and unmanaged code.

    It is possible to get the out of memory exception when the array is very big.

    Considering the memory utilization of the application, I don’t recommended exchanging a lot of raw data between managed code and unmanaged code.

    Usually, we can use buffer to reduce the unmanaged memory cost , but in this case, since it requires a contiguous unmanaged memory, so using buffer will not suit.

    For high memory utilization when processing  a log of raw data, I recommend using unmanaged code.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 7, 2015 2:12 AM
  • Thank you very much , Franklin. 

    I assumed WriteOnlyArray was allocated once, and passed as a pointer.  Your investigation convincingly shows the assumption is wrong.


    Hong

    Friday, August 7, 2015 2:50 AM