GithubHelp home page GithubHelp logo

Memory issues about usbcamera HOT 13 OPEN

lwoldt avatar lwoldt commented on August 20, 2024
Memory issues

from usbcamera.

Comments (13)

secile avatar secile commented on August 20, 2024

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.

from usbcamera.

SankarGaneshKumar avatar SankarGaneshKumar commented on August 20, 2024

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)

from usbcamera.

yangjieshao avatar yangjieshao commented on August 20, 2024

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();

from usbcamera.

lwoldt avatar lwoldt commented on August 20, 2024

from usbcamera.

yangjieshao avatar yangjieshao commented on August 20, 2024

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

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

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

from usbcamera.

lwoldt avatar lwoldt commented on August 20, 2024

from usbcamera.

yangjieshao avatar yangjieshao commented on August 20, 2024

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

---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摄像头我跑了很久没问题
采集卡的话 我没有 不知道什么情况

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

from usbcamera.

SankarGaneshKumar avatar SankarGaneshKumar commented on August 20, 2024

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)

from usbcamera.

yangjieshao avatar yangjieshao commented on August 20, 2024

@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

from usbcamera.

secile avatar secile commented on August 20, 2024

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?

from usbcamera.

lwoldt avatar lwoldt commented on August 20, 2024

from usbcamera.

secile avatar secile commented on August 20, 2024

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;
}

from usbcamera.

secile avatar secile commented on August 20, 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.

from usbcamera.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.