Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory issues #27

Open
lwoldt opened this issue Jun 19, 2023 · 13 comments
Open

Memory issues #27

lwoldt opened this issue Jun 19, 2023 · 13 comments

Comments

@lwoldt
Copy link

lwoldt commented Jun 19, 2023

Hello, when using an external video capture card, you may notice that the memory usage continues to increase, ultimately causing the program to crash. Is there a button for real-time release

@secile
Copy link
Owner

secile commented Jun 19, 2023

Hello.
I think my library do not have problem of leaking memory.

  1. library include 2 sample project, 'UsbCameraForms' and 'UsbCameraWpf'. Use these sample projects and let me know crash happens or not.
  2. if possible, show me your code.
  3. tell me product name of external video capture card.

@SankarGaneshKumar
Copy link

Im aslo facing the same issue. GC is not collecting. Memory keeps on increasing when it reaches 3.9 GB it ends in Out of memory exception. My camera is See3CAM_CU55 by eCam and its details are 5 MP (2592 x 1944) 24 fps and 12 fps Uncompressed
UYVY and Compressed MJPEG 60 fps .
Screenshot (33)
Screenshot (32)

@yangjieshao
Copy link

Im aslo facing the same issue. GC is not collecting. Memory keeps on increasing when it reaches 3.9 GB it ends in Out of memory exception. My camera is See3CAM_CU55 by eCam and its details are 5 MP (2592 x 1944) 24 fps and 12 fps Uncompressed UYVY and Compressed MJPEG 60 fps . Screenshot (33) Screenshot (32)

change

var timer = new System.Timers.Timer(1000 / 30) { SynchronizingObject = this };
timer.Elapsed += (s, ev) => pictureBox1.Image = camera.GetBitmap();
timer.start();

to

var timer = new System.Timers.Timer(1000 / 30) { SynchronizingObject = this };
timer.Elapsed += (s, ev) =>
{
	var oldImage = pictureBox1.Image;
	pictureBox1.Image = camera.GetBitmap();
	if(oldImage!=null)
	{
		oldImage.Dispose();
	}
};
timer.start();

@lwoldt
Copy link
Author

lwoldt commented Oct 12, 2023 via email

@yangjieshao
Copy link

改用其他库,该库存在明显的内存回收问题,起因是外接的usb设备无法管理

并没有问题哦 我有项目跑了2年了 前台登记的客户端 一点内存都没漏
只是例子里 没释放pictureBox1.Image旧图片

你用啥库 只要是把图片clone一份到pictureBox1.Image 都得自己释放

@lwoldt
Copy link
Author

lwoldt commented Oct 12, 2023 via email

@yangjieshao
Copy link

您的库在系统自带的摄像头下无任何问题,一旦有其他的外接采集卡存在,内存即使自己回收,也还是会有问题

---Original--- From: @.> Date: Thu, Oct 12, 2023 19:47 PM To: @.>; Cc: @.@.>; Subject: Re: [secile/UsbCamera] Memory issues (Issue #27) 改用其他库,该库存在明显的内存回收问题,起因是外接的usb设备无法管理 并没有问题哦 我有项目跑了2年了 前台登记的客户端 一点内存都没漏 只是例子里 没释放pictureBox1.Image旧图片 你用啥库 只要是把图片clone一份到pictureBox1.Image 都得自己释放 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

你得翻译成英文啊 作者看不懂中文
然后 USB摄像头我跑了很久没问题
采集卡的话 我没有 不知道什么情况

现在的项目都要考虑信创 不用这个库了

@SankarGaneshKumar
Copy link

Tried your change still it leads to out of memory error. Only one time GC is calling atr 3.5 GB. then it leads to out of memory exception. When I changed this function

public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
{
if (Buffer == null || Buffer.Length != BufferLen)
{
Buffer = new byte[BufferLen];
}
// replace lock statement to Monitor.TryEnter. (issue #14)
var locked = false;
try
{
System.Threading.Monitor.TryEnter(BufferLock, 0, ref locked);
if (locked)
{
Marshal.Copy(pBuffer, Buffer, 0, BufferLen);

                 }
             }
             finally
             {
                 if (locked) System.Threading.Monitor.Exit(BufferLock);
                Buffer = null;
            }
             // notify buffered to worker thread. (issue #16)
             if (Buffered != null) BufferedEvent.Set();
            return 0;
        }
        
        To this
        
        
         public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen)
        {
            
            byte[] tempBuffer = new byte[BufferLen];
            
            var locked = false;
            try
            {
                System.Threading.Monitor.TryEnter(BufferLock, 0, ref locked);
                if (locked)
                {

                    Marshal.Copy(pBuffer, tempBuffer, 0, BufferLen);
                    //Console.WriteLine("BufferLen " + BufferLen);
                    Buffer = tempBuffer;
                    tempBuffer = null;
                }
            }
            finally
            {
                if (locked) System.Threading.Monitor.Exit(BufferLock);
            }
            // notify buffered to worker thread. (issue #16)
            if (Buffered != null) BufferedEvent.Set();
            return 0;
        }
        
        GC is calling very frequently. But it is not leading to Out of memory exception. Any idea what is happening ?

Screenshot (2)

@yangjieshao
Copy link

yangjieshao commented Oct 12, 2023

@SankarGaneshKumar maybe you can try FlashCap

It can run on Linux and Windows
if you want run on mac you can try kekyo/FlashCap#65

@secile
Copy link
Owner

secile commented Oct 12, 2023

hello, SankarGaneshKumar.

sorry, I do not have time, so in short.

I have ever experienced this problem once.
At that time, when I changed target framework .NET Framework version, the problem is disapeared.
Could you try?

@lwoldt
Copy link
Author

lwoldt commented Oct 12, 2023 via email

@secile
Copy link
Owner

secile commented Oct 12, 2023

If it doesn't work, there's one more thing you can try.
Recently, I added 'USBCAMERA_BYTEARRAY' symbol.
if 'USBCAMERA_BYTEARRAY' symbol defined, GetBitmap() returns image data as byte array.

Define 'USBCAMERA_BYTEARRAY' symbol and try the code below.

// 2. use Timer and GetBitmap().
var timer = new System.Timers.Timer(1000 / 30) { SynchronizingObject = this };
timer.Elapsed += (s, ev) =>
{
    var buf = (byte[])camera.GetBitmap();
    pictureBox1.Image = BufferToBitmap(buf, camera.Size.Width, camera.Size.Height);
};
timer.Start();

private static Bitmap BufferToBitmap(byte[] buffer, int width, int height)
{
    var result = new Bitmap(width, height);
    var bmpData = result.LockBits(new Rectangle(Point.Empty, result.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    System.Runtime.InteropServices.Marshal.Copy(buffer, 0, bmpData.Scan0, buffer.Length);
    result.UnlockBits(bmpData);
    return result;
}

@secile
Copy link
Owner

secile commented Feb 27, 2024

I would like to summarize what we currently know about this issue.

  • If you use a timer (or subscribe PreviewCaptured) instead of SetPreviewControl, an error may occur.
    • in my environment, when VideoFormat image size is greater that 1920×1080, error happens.
    • when VideoFormat image size is smaller than 1280×1024, error not happen.
  • It is thought that the reason why error happen is out of memory due to Bitmap not being disposed.
  • I previously said 'changed target framework .NET Framework version, the problem is disapeared', but maybe this is not correct.
  • I previously said ''define USBCAMERA_BYTEARRAY' symbol', this has an effect.
    • the reason may that creating new Bitmap in local function make bitmap GC generation to 0, therefor, bitmap is disposed aggressively.

Basically, bitmap object is disposed automatically by GC, so it is not occur out of memory error in normal situation.
But for various reasons, for example, selecting large image size, bitmap is not disposed collectly and error occur.
If you are in the situation, you have to dispose bitmap expressly.
Please see also issue34.

I'll remain this issue open for a while.
If the same problem is not reported after a while, I will close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants