垃圾收集器不能做的工作。内存消耗= 1.5GB和放大器;内存不足异常放大器、能做、消耗、异常

2023-09-06 14:20:16 作者:安洛熙

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用的System.Threading;
使用System.IO;
使用System.Drawing中;
使用System.Drawing.Imaging;

命名空间TrainSVM
{
    类节目
    {
        静态无效的主要(字串[] args)
        {
            的FileStream FS =新的FileStream(dg.train,FileMode.OpenOrCreate,FileAccess.Write);
            StreamWriter的SW =新的StreamWriter(FS);
            的String [] filePathArr = Directory.GetFiles(E:\\ \\图像);

            的foreach(在filePathArr字符串文件路径)
            {
                如果(filePath.Contains(HBP))
                {
                    sw.Write(1);
                    Console.Write(1);
                }
                其他
                {
                    sw.Write(1);
                    Console.Write(1);
                }

                使用(位图originalBMP =新位图(文件路径))
                {


                    / *********************** /
                    位图imageBody;
                    ImageBody.ImageBody IM =新ImageBody.ImageBody(originalBMP);
                    使用(imageBody = im.GetImageBody(-1))
                    {

                        / *白大衣* /
                        位图whiteCoatBitmap = Rgb2Hsi.Rgb2Hsi.GetHuePlane(imageBody);
                        浮WhiteCoatPixelPercentage = Rgb2Hsi.Rgb2Hsi.GetWhiteCoatPixelPercentage(whiteCoatBitmap);
                        //Console.Write("whiteDone\t);
                        sw.Write(1:+ WhiteCoatPixelPercentage +);
                        Console.Write(1:+ WhiteCoatPixelPercentage +);

                        / ****************** /
                        Quaternion.Quaternion QTR =新Quaternion.Quaternion(-15);
                        位图yellowCoatBMP = qtr.processImage(imageBody);
                        //yellowCoatBMP.Save("yellowCoat.bmp);
                        浮yellowCoatPixelPercentage = qtr.GetYellowCoatPixelPercentage(yellowCoatBMP);
                        //Console.Write("yellowCoatDone\t);
                        sw.Write(2:+ yellowCoatPixelPercentage +);
                        Console.Write(2:+ yellowCoatPixelPercentage +);

                        / ********************** /
                        位图balckPatchBitmap = BlackPatchDetection.BlackPatchDetector.MarkBlackPatches(imageBody);
                        浮BlackPatchPixelPercentage = BlackPatchDetection.BlackPatchDetector.BlackPatchPercentage;
                        //Console.Write("balckPatchDone\n);
                        sw.Write(3+ BlackPatchPixelPercentage +\ N);
                        Console.Write(3+ BlackPatchPixelPercentage +\ N);
                    }
                }


                sw.Flush();

            }


            sw.Dispose();
            fs.Dispose();

        }

    }
}
 

解决方案

有一些位图情况下,有你没有处理。你真的应该尝试在使用使用块而不是手动处理,以阻止这些事情漏网的习惯。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace TrainSVM
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream fs = new FileStream("dg.train",FileMode.OpenOrCreate,FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            String[] filePathArr = Directory.GetFiles("E:\\images\\");

            foreach (string filePath in filePathArr)
            {
                if (filePath.Contains("HBP"))
                {
                    sw.Write("1 ");
                    Console.Write("1 ");
                }
                else
                {
                    sw.Write("1 ");
                    Console.Write("1 ");
                }

                using (Bitmap originalBMP = new Bitmap(filePath))
                {


                    /***********************/
                    Bitmap imageBody;
                    ImageBody.ImageBody im = new ImageBody.ImageBody(originalBMP);
                    using (imageBody = im.GetImageBody(-1))
                    {

                        /* white coat */
                        Bitmap whiteCoatBitmap = Rgb2Hsi.Rgb2Hsi.GetHuePlane(imageBody);
                        float WhiteCoatPixelPercentage = Rgb2Hsi.Rgb2Hsi.GetWhiteCoatPixelPercentage(whiteCoatBitmap);
                        //Console.Write("whiteDone\t");
                        sw.Write("1:" + WhiteCoatPixelPercentage + " ");
                        Console.Write("1:" + WhiteCoatPixelPercentage + " ");

                        /******************/
                        Quaternion.Quaternion qtr = new Quaternion.Quaternion(-15);
                        Bitmap yellowCoatBMP = qtr.processImage(imageBody);
                        //yellowCoatBMP.Save("yellowCoat.bmp");
                        float yellowCoatPixelPercentage = qtr.GetYellowCoatPixelPercentage(yellowCoatBMP);
                        //Console.Write("yellowCoatDone\t");
                        sw.Write("2:" + yellowCoatPixelPercentage + " ");
                        Console.Write("2:" + yellowCoatPixelPercentage + " ");

                        /**********************/
                        Bitmap balckPatchBitmap = BlackPatchDetection.BlackPatchDetector.MarkBlackPatches(imageBody);
                        float BlackPatchPixelPercentage = BlackPatchDetection.BlackPatchDetector.BlackPatchPercentage;
                        //Console.Write("balckPatchDone\n");
                        sw.Write("3:" + BlackPatchPixelPercentage + "\n");
                        Console.Write("3:" + BlackPatchPixelPercentage + "\n");
                    }
                }


                sw.Flush();

            }


            sw.Dispose();
            fs.Dispose();

        }

    }
}
深入理解Java虚拟机之垃圾收集器与内存分配

解决方案

There are some Bitmap instances there that you aren't disposing. You should really try to get in the habit of using a using block rather than disposing manually, to stop these things slipping through the net.

 
精彩推荐
图片推荐