0
     using var stream = new MemoryStream(filteredFrame);
     using var image = Image.FromStream(stream);
     using var resizedBitmap = new Bitmap(image, new Size(640, 320));
     using var resizedImageStream = new MemoryStream();
     resizedBitmap.Save(resizedImageStream, ImageFormat.Jpeg);
     FrameReceived?.Invoke(resizedImageStream.ToArray());

above code is part of method.

printed memory usage, because has guessed to make memory leak in using stream

     using var currentProcess = Process.GetCurrentProcess();
     Debug.WriteLine(currentProcess.PrivateMemorySize64.ToString());
     using var stream = new MemoryStream(filteredFrame);
     Debug.WriteLine(currentProcess.PrivateMemorySize64.ToString());
     using var image = Image.FromStream(stream);
     Debug.WriteLine(currentProcess.PrivateMemorySize64.ToString());
     using var resizedBitmap = new Bitmap(image, new Size(640, 320));
     Debug.WriteLine(currentProcess.PrivateMemorySize64.ToString());
     using var resizedImageStream = new MemoryStream();
     Debug.WriteLine(currentProcess.PrivateMemorySize64.ToString());
     resizedBitmap.Save(resizedImageStream, ImageFormat.Jpeg);
     Debug.WriteLine(currentProcess.PrivateMemorySize64.ToString());
     FrameReceived?.Invoke(resizedImageStream.ToArray());
     Debug.WriteLine(currentProcess.PrivateMemorySize64.ToString());

https://docs.google.com/document/d/1eygcxuGMGveHrHYn59CaPWL69ekKNeWwITUhcGgH1yU/edit?usp=sharing

above link is printing result

Each time the method is called, the memory increases.

I thinks that if use using, the memory should be freed. but memory was just increased

if above code remove, not showing memory leak

Is gc slower than memory allocation speed?

Method is called once every 0.5 seconds.

I used System.Drawing.Common, excution enviorment is arm64 linux .net standard 2.1

Please let me know if I'm using the using syntax incorrectly or if I've made a mistake. Or let me know if you have any other comments about the memory leak.

enter image description here

Above is a visualization of the memory usage log left when my server was forced to reboot twice. The unit is mb.

Garbage collector works, but eventually it builds up too much memory and kills the app.

using MemoryStream stream = new MemoryStream(filteredFrame), resizedImageStream = new MemoryStream();
using Bitmap resizedBitmap = new Bitmap(Image.FromStream(stream), new Size(640, 320));                
resizedBitmap.Save(resizedImageStream, ImageFormat.Jpeg);
FrameReceived?.Invoke(resizedImageStream.ToArray());

Above code is a bit more refactored code.

Sunday
  • 109
  • 1
  • 9
  • 2
    Are you aware that disposing objects ahs nothing directly to do with releasing memory? Disposing an object means that that objects managed and unmanaged resources are released now, rather than the GC having to finalise the object later in order to clean up its memory. Disposing means that memory can be cleaned up sooner, but it doesn't clean up that memory. The GC will clean up memory when it can. In the meantime, your app will continue to allocate new memory. What you're seeing is normal. – John Aug 01 '22 at 09:09
  • A problem can arise when you perform operations that allocate a lot of memory without the GC having time to clean it up in between. In such cases, you may have to invoke the GC manually from time to time. – John Aug 01 '22 at 09:10
  • If you want to find memory leaks, use a memory profiler. That will allow you to capture snapshots of allocated objects, and allows for GCs to be triggered at any time. – JonasH Aug 01 '22 at 09:27
  • 1
    _" because has guessed"_ - you should not guess, you should profile and check. The fact that memory is growing woes not prove that there is a memory leak - depended on application type and a lot of other factors (like GC setup, total available memory and so on) GC can be pretty lazy with freeing up the memory. On the other side "jagged" memory usage growth (when memory growth, drops a bit, but not to the "original" level, then repeats the pattern indefinitely, with "local minimums" continually growing) can be a huge sign for leak. – Guru Stron Aug 01 '22 at 10:27
  • Thank you Let's check some more details about the memory leak and add information. – Sunday Aug 01 '22 at 11:24
  • @GuruStron Is this the shape you're talking about? – Sunday Aug 02 '22 at 05:15
  • @Sunday yes, based on the image and descriptions looks similar to leak. But it is not necessarily caused by your manipulations with bitmaps. You need to determine what is stored in memory indefinitely and how it gets there. I would argue that `FrameReceived` can be a suspect too. – Guru Stron Aug 02 '22 at 11:14

0 Answers0