Out of Memory exception when using Windows MediaElement Class C#

Rengarajan, Narayanan 0 Reputation points
2024-07-05T12:41:48.65+00:00

Hello,

We are trying to run multiple h264 videos in a loop using MediaElement class in C# WPF windows application. We ran into out of memory exception after running it for a while.

We observed unmanaged memory shooting up more than a GB.

Please find the environment details:

.Net framework version: 4.8

Windows OS: Win 10

GPU: Intel Graphics HD 520 (internal)

Any idea on how to resolve this?

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
11,724 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,022 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Wesley Li 10,240 Reputation points
    2024-07-06T00:11:25.84+00:00

    Hello

    It sounds like you’re encountering a memory leak issue with the MediaElement class in your WPF application. Here are a few suggestions to help mitigate this problem:

     

    1.Unregister Event Handlers: Ensure that you unregister any event handlers when they are no longer needed. For example, if you have an event handler for MediaEnded, make sure to unregister it when the media element is no longer in use:

     

    myMediaElement.MediaEnded -= myMediaElement_MediaEnded;

     

    2.Dispose of MediaElement Properly: Explicitly set the Source property of the MediaElement to null and call the Close method when you are done with a video:

     

    myMediaElement.Stop();

    myMediaElement.Source = null;

    myMediaElement.Close();

     

    3.Turn Off Hardware Acceleration: Sometimes, hardware acceleration can cause issues. You can turn it off by setting the render mode to software only:

     

    protected override void OnSourceInitialized(EventArgs e)

    {

        HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;

        HwndTarget hwndTarget = hwndSource.CompositionTarget;

        hwndTarget.RenderMode = RenderMode.SoftwareOnly;

        base.OnSourceInitialized(e);

    }

     

    4.Use a Different Media Player: If the issue persists, consider using a different media player library like VLC or FFmpeg which might handle memory management more efficiently.

     

    5.Monitor and Profile Memory Usage: Use tools like the Visual Studio Diagnostic Tools or dotMemory to monitor and profile your application’s memory usage. This can help you identify specific areas where memory is not being released properly.

     

    Implementing these suggestions should help reduce the memory consumption and prevent the out-of-memory exceptions you’re experiencing.

    0 comments No comments

  2. Wesley Li 10,240 Reputation points
    2024-08-06T08:45:16.3133333+00:00

    Hello

    Is your problem solved?

    If the above reply is helpful to you, please mark your reply as an answer, thank you very much!

    If you have any further questions, please do not hesitate to contact us.

    Thanks

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.