C# Out of Memory

  • Problem

    Out of memory

    The program is stalled after a few minutes due to out of memory.

    The system has 16GB of memory, but as soon as the program starts, it consumes over 15GB of memory.
    The memory usage fluctuates around 15GB, and after a few minutes, it runs out of memory and stalls.

    Could you please give me some advice on possible countermeasures?

  • System Configuration

CPU: 12th Gen Intel Core i7-12700H 2.30 GHz
RAM: 16.0 GB
OS: Windows 11 Pro 23 H2
GPU: NVIDIA GeForce RTX 3050 Laptop GPU
Cuda Toolkit: cuda_12.8.0_571.96_windows
ZED SDK: ZED_SDK_Windows_cuda12.8_tensorrt10.9_v5.0.6
Programming Language: C# .NET 9.0 Visual Studio 2022 Version 17.14.13 (August 2025)

  • Source Code modified for inquiry

      public static void ZEDInitialize()
      {
          // Create a ZED camera object
          if (zed == null)
          {
              zed = new(0);
          }
    
          // Set configuration parameters                                                                                               
          InitParameters init_params = new();
    
          init_params.resolution = RESOLUTION.HD1080;
          init_params.cameraFPS = 30;
          init_params.depthMode = DEPTH_MODE.NONE;
    
          // Open the camera
          try
          {
              return_state = zed.Open(ref init_params);
          }
          catch (Exception ex)
          {
              Environment.Exit(-1);
          }
    
          if (return_state != ERROR_CODE.SUCCESS)
          {
              Environment.Exit(-1);
          }
    
          zed.SetCameraSettings(VIDEO_SETTINGS.LED_STATUS, 0);  // Front LED Disabled
    
          Start();
      }
    
    
      public static void Start()
      {
          uint mWidth = (uint)zed.ImageWidth;
          uint mHeight = (uint)zed.ImageHeight;
    
          // Initialize the Mat that will contain the image
          sl.Mat zedImage = new();
          zedImage.Create(mWidth * 2, mHeight, MAT_TYPE.MAT_8U_C4, MEM.CPU); // Mat needs to be created before use.
    
          OpenCvSharp.Mat cvImage = OpenCvSharp.Mat.FromPixelData(mHeight, mWidth, MatType.CV_8UC4, zedImage.GetPtr());
    
          RuntimeParameters runtimeParameters = new();
    
          var jpegParams = new int[] { (int)ImwriteFlags.JpegQuality, 75 };
    
          int FrameCount = 1;
    
          while (true)
          {
              if (zed.Grab(ref runtimeParameters) == ERROR_CODE.SUCCESS)
              {
                  if (zed.RetrieveImage(zedImage, VIEW.SIDE_BY_SIDE, MEM.CPU) != ERROR_CODE.SUCCESS)
                  {
                      continue;
                  }
    
                  System.Drawing.Image tmpImage = BitmapConverter.ToBitmap(cvImage);
                  if (tmpImage == null)
                  {
                      continue;
                  }
    
                  PictureBoxZED.Image = tmpImage;
                  if (PictureBoxZED.Image == null)
                  {
                      continue;
                  }
    
                  if (RecordingFlag) // 録画中
                  {
                      string OutputFile = FrameCount.ToString("D6") + ".jpg";
    
                      try
                      {
                          cvImage.ImWrite(OutputFile, jpegParams);
                      }
                      catch (Exception ex)
                      {
                          
                      }
                          
                      FrameCount++;
                  }
              }
              else
              {
                  continue;
              }
          }
    
          zed.Close();
          zed = null;
       }
    

Hi,

Do you experience the same issue when running one of our C # samples?

Dear Benjamin-san,

Thank you for your quick response.

Which sample do I need to run?

Best regards,
Yasuyuki Nasu

Any of the csharp samples, like this one: zed-sdk/camera control/csharp at master · stereolabs/zed-sdk · GitHub