Search code examples
c#memory-leaksobject-detectionml.net

C# Object detection ML.NET Model Builder consumes too much RAM


I writing an object detection using C# ML.NET Object Detection, every time I press the button to run an object scan from the image, the RAM it increases a bit (about 5-10mb each time). The RAM keeps increasing every time I press the scan object button from the image and it doesn't release after each run (First time run scan 800mb RAM, seconds time, 1GB+, 3rd time or more : about 5-10mb each time). Continue like this the software will consume a lot of GB of RAM if I run this object detection function many times. I have tried many ways: Image.Disposed, using{}. But it seems I need to adjust the ML.Model1.consumption.cs file. Does anyone know why?

    private void button1_Click(object sender, EventArgs e)
    {
        var image = (Bitmap)Image.FromFile(@"D:\Fraps\IMG\0.png");
        MLModel1.ModelInput sampleData = new MLModel1.ModelInput()
        {
            ImageSource = image,
        };
        // Make a single prediction on the sample data and print results.
        var predictionResult = MLModel1.Predict(sampleData);
        Console.WriteLine("\n\nPredicted Boxes:\n");
    }

Solution

  • There seems to be no problem. The software will always take up about 1GB of memory. It will add about 200-300MB if I run the object detection function loop 50 times. But it automatically drops to about ~1GB of RAM if I keep running it 50 more times. Also, use using{} for the image saves about 100MB of RAM.