Memory leak in MediaPlayer

Can someone explain why there is not enough memory in the next program?

class Program { private static void ThreadRoutine() { System.Windows.Media.MediaPlayer player = new System.Windows.Media.MediaPlayer(); } static void Main(string[] args) { Thread aThread; int iteration = 1; while (true) { aThread = new Thread(ThreadRoutine); aThread.Start(); aThread.Join(); Console.WriteLine("Iteration: " + iteration++); } } } 

To be fair, I get a specific System.ComponentModel.Win32Exception exception: "Out of memory to process this command." An exception occurs when you try to create a new MediaPlayer.

MediaPlayer does not implement the IDisposable interface, so I'm not sure if another cleanup is needed. I definitely did not find in the MediaPlayer documentation.

+8
garbage-collection multithreading c # threadpool
source share
2 answers

I assume that the created Thread objects are not collected on time.

As you state in your comment that you were unlucky with finding a solution that is not related to a memory leak, I am sending this alternative as an answer, even if it does not try to answer your original question.

If you use ThreadPool instead, it (a) works much faster and (b) does not crash.

 class Program { private static void ThreadRoutine(object state) { var player = new MediaPlayer(); var iteration = (int)state; if (iteration % 1000 == 0) { Console.WriteLine("Executed: " + state); } } static void Main(string[] args) { for (int i = 0; i < 10000000; i++) { if (i % 1000 == 0) { Console.WriteLine("Queued: " + i); } ThreadPool.QueueUserWorkItem(ThreadRoutine, i); } } } 

On my machine, I have no problem creating 10 million thread streams in a few seconds.

 Queued: 9988000 Queued: 9989000 Queued: 9990000 Queued: 9991000 Executed: 9989000 Executed: 9990000 Executed: 9991000 Executed: 9988000 Queued: 9992000 Executed: 9992000 Queued: 9993000 Queued: 9994000 Queued: 9995000 Executed: 9994000 Executed: 9993000 Queued: 9996000 Executed: 9996000 Executed: 9995000 Queued: 9997000 Executed: 9997000 Queued: 9998000 Executed: 9998000 Queued: 9999000 Executed: 9999000 Press any key to continue . . . 
0
source share

I had an application for a long time that displayed data for an image and placed it on top of the PictureBox as a result, for example, the pipeline line of the graphics engine, ...

The dishware common with you was a lack of memory, it was so ...

  • 66%
  • 67%
  • 68%
  • 69%
  • 70%
  • 71%
  • 72%
  • 73%
  • 74%
  • 70% β†’ Auto garbage collection
  • 71%
  • 72%
  • 73%
  • 74%
  • 75%
  • 76%
  • 77%
  • 78%
  • 79%
  • 80%
  • 81%
  • 77% β†’ Automatic garbage collection
  • 78%
  • 79%
  • .
  • .
  • .

And a lot of Auto Garbage Collecting in high memory is about 90% to 97%, ... but it seems that this was not enough, and at some point about 97% and 98% of the system (application) crashed with a memory problem ...

so I came up with this idea to call the garbage collector every few frames, so I did this: GC.Collect ();

GC.Collect it self is to heavy, also when there are too many objects inside the memory ... but when you do not leave too many objects, it works smoothly, ...

There are also many things about completing objects, but I'm not sure if they work correctly, as they naturally end in β†’ x = null

which means that we have broken the connection with this object, but this does not mean that we do not have this object somewhere around the galaxy (for example, you can fill the object’s destructor with a message and see after it left this object, t destroy it immediately , until you close the application or call GC.Collect / Lack on memory, or maybe random automatic collection), ... until it is destroyed, ... for example as the using directive, he calls the Dispose method himself, and we populate our dispose with obj = null, ... then I'm not sure what I should I have to tell you to write the finalization method manually, but there is one more thing ... there is a marshal object that I don’t know where it came from, and if it works on the whole object, I see that some programmer uses it to release objects, ..., I do not know how this works, but it is possible that it really frees an object from our memory, ...

If you did not find anything useful in this, my first option would be to call GC.Collect ...

You can also set its parameters, so it collects fewer objects than usual.

https://msdn.microsoft.com/en-us/library/xe0c2357%28v=vs.110%29.aspx

Free up resources in .Net C #

I also did not read this completely, but it seems to have problems with the thread and memory: Freeing up resources when the thread is inactive

+1
source share

All Articles