使用C#代码步骤内存占满了

使用问题 · 299 次浏览
之乎者也吧 创建于 2024-03-26 16:38

功能是获取文件夹里所有文件并按照像素点的色值分开存放,功能上来讲一切正常,但是当全部功能都通过C#来实现时就出问题了,内存直接占满卡到爆。

只有当获取文件和路径这个步骤通过quicker实现,把文件路径通过变量传入到C#里执行剩下的判断和复制操作才可以流畅运行,占用也不高。

每次只处理一个路径,处理完进入下一次循环,因为文件太多了想要实现多线程,但是多线程时勾上为线程创建上下文与否都会失败,所以想要通过C#实现异步的(还没异步,只是单线程测试的时候就卡爆了),如果可以给出其他实现异步的具体思路的话也不用纠结这个了。

下面是用quicker+C#实现的步骤

//.cs  文件类型,便于外部编辑时使用
// 引用必要的命名空间
using System;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
 public static void Exec(Quicker.Public.IStepContext context)
{
    string blueDir = @"E:\文件\分类文件\蓝色";
    string redDir = @"E:\文件\分类文件\红色";
    string greenDir = @"E:\文件\分类文件\绿色";
    string unKnown = @"E:\文件\分类文件\不知道算什么";
    string picPath = context.GetVarValue("filePath").ToString();
    string fileName = Path.GetFileNameWithoutExtension(picPath);
    string newPath = "";
    
    Bitmap image = new Bitmap(picPath);
    Color color = image.GetPixel(10, 10);
    int red = color.R;
    //context.SetVarValue("Color", red);
    if (red == 81||red==46)
    {
    	newPath = Path.Combine(blueDir, fileName+".jpg");
    	while(File.Exists(newPath))
    	{	
    		fileName = fileName + "重复";
    		newPath = Path.Combine(blueDir, fileName+".jpg");
    	}
    	File.Copy(picPath,newPath);
    }
    else if(red==229||red==208)
    {
    	newPath = Path.Combine(redDir, fileName+".jpg");
    	while(File.Exists(newPath))
    	{	
    		fileName = fileName + "重复";
    		newPath = Path.Combine(redDir, fileName+".jpg");
    	}
    	File.Copy(picPath,newPath);
    }
    else if(red==70||red==95)
    {
    	newPath = Path.Combine(greenDir, fileName+".jpg");
    	while(File.Exists(newPath))
    	{	
    		fileName = fileName + "重复";
    		newPath = Path.Combine(greenDir, fileName+".jpg");
    	}
    	File.Copy(picPath,newPath);
    }
    else
    {
    	newPath = Path.Combine(unKnown, fileName+".jpg");
    	while(File.Exists(newPath))
    	{	
    		fileName = fileName + "重复";
    		newPath = Path.Combine(unKnown, fileName+".jpg");
    	}
    	File.Copy(picPath,newPath);
    }
    	
}

下面的是全部用C#实现的代码

//.cs  文件类型,便于外部编辑时使用
// 引用必要的命名空间
using System;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
public static void Exec(Quicker.Public.IStepContext context)
{
	string blueDir = @"E:\文件\分类文件\蓝色";
	string redDir = @"E:\文件\分类文件\红色";
	string greenDir = @"E:\文件\分类文件\绿色";
	string unKnown = @"E:\文件\分类文件\不知道算什么";
	string newPath = "";
	string sourceDirectory = context.GetVarValue("文件夹路径").ToString();

	try
	{
		var Files = Directory.EnumerateFiles(sourceDirectory, "*.jpg", SearchOption.AllDirectories);

		foreach (string currentFile in Files)
		{
			string fileName = Path.GetFileNameWithoutExtension(currentFile);
			Bitmap image = new Bitmap(currentFile);
			Color color = image.GetPixel(10, 10);
			int red = color.R;
			if (red == 81||red==46)
			{
				newPath = Path.Combine(blueDir, fileName+".jpg");
				while(File.Exists(newPath))
				{
					fileName = fileName + "重复";
					newPath = Path.Combine(blueDir, fileName+".jpg");
				}
				File.Copy(currentFile,newPath);
			}
			else if(red==229||red==208)
			{
				newPath = Path.Combine(redDir, fileName+".jpg");
				while(File.Exists(newPath))
				{
					fileName = fileName + "重复";
					newPath = Path.Combine(redDir, fileName+".jpg");
				}
				File.Copy(currentFile,newPath);
			}
			else if(red==70||red==95)
			{
				newPath = Path.Combine(greenDir, fileName+".jpg");
				while(File.Exists(newPath))
				{
					fileName = fileName + "重复";
					newPath = Path.Combine(greenDir, fileName+".jpg");
				}
				File.Copy(currentFile,newPath);
			}
			else
			{
				newPath = Path.Combine(unKnown, fileName+".jpg");
				File.Copy(currentFile,newPath);
			}
		}
		
	}
	catch (Exception e)
	{
		MessageBox.Show(e.Message);
	}
	
}

回复内容
CL 2024-03-26 16:42
#1

占满是占了多少?

具体原因,可以用resharper之类的工具分析一下。

之乎者也吧 回复 CL 2024-03-26 17:30 :

共8G 运行前已经占了4G 运行后逐渐的就占满了,增长的速度也不慢,差不多一秒100到200mb,主要是他在不断叠加,怀疑16G也扛不住,是代码本身有问题吗调用的不对还是什么情况0-0,遍历和复制粘贴操作应该不会这么占内存才对

CL 回复 之乎者也吧 2024-03-26 17:31 :

这种用工具看比较方便,猜起来比较困难。也许并不是这里的原因。

另外8g太小了,建议16-32g。

CL 最后更新于 2024-03-26 17:32
之乎者也吧 回复 CL 2024-03-26 17:32 :

好的多谢老大,我去研究研究工具

回复主贴