C#文件内存不足

C#文件内存不足,c#,.net,C#,.net,在运行下面的代码时,我遇到了一个OutOfMemory异常,它发生在File.ReadLines行上,它可以处理大多数文件,直到遇到更大的文件 但在整个过程中,它一直在使用大量的内存和cpu 它崩溃的文件只有15600KB,也就是156mb static void Main(string[] args) { Console.CursorVisible = false; Console.ForegroundColor = ConsoleColor.Green; Conso

在运行下面的代码时,我遇到了一个OutOfMemory异常,它发生在File.ReadLines行上,它可以处理大多数文件,直到遇到更大的文件

但在整个过程中,它一直在使用大量的内存和cpu

它崩溃的文件只有15600KB,也就是156mb

static void Main(string[] args)
{
    Console.CursorVisible = false;

    Console.ForegroundColor = ConsoleColor.Green;
    Console.WriteLine();
    Console.WriteLine("  [" + DateTime.Now.ToShortTimeString() + "]" + " Connected to the Cassandra Database");
    Console.WriteLine();
    Console.ForegroundColor = ConsoleColor.White;

    string filepath = @"C:\Users\admin\Desktop\wecrack lists";
    DirectoryInfo directory = new DirectoryInfo(filepath);

    int fileCount = 0;

    var client = new MongoClient("mongodb://localhost:27017");
    var database = client.GetDatabase("cracking");
    var collection = database.GetCollection<Password>("passwords");

    foreach (var file in directory.GetFiles("*"))
    {
        fileCount++;

        Console.WriteLine("  [" + DateTime.Now.ToShortTimeString() + "]" + " Working through file: {" + file + "} {" + fileCount + "/" + directory.GetFiles("*").Count() + "}");

        List<Password> entitys = new List<Password>();

        foreach (string line in File.ReadLines(filepath + @"\" + file.ToString()))
        {
            entitys.Add(new Password { password = line });
        }

        collection.InsertManyAsync(entitys);
    }

    Console.WriteLine();
    Console.WriteLine("  [" + DateTime.Now.ToShortTimeString() + "]" + " Finished inserting records, press any key to get the count.");
    Console.ReadKey(true);

    while (true)
    {
        Console.ReadKey(true);
    }
}
static void Main(字符串[]args)
{
Console.CursorVisible=false;
Console.ForegroundColor=ConsoleColor.Green;
Console.WriteLine();
Console.WriteLine(“[”+DateTime.Now.ToSortTimeString()+“]”+“连接到Cassandra数据库”);
Console.WriteLine();
Console.ForegroundColor=ConsoleColor.White;
字符串文件路径=@“C:\Users\admin\Desktop\wecrack list”;
DirectoryInfo目录=新的DirectoryInfo(文件路径);
int fileCount=0;
var客户端=新的MongoClient(“mongodb://localhost:27017");
var database=client.GetDatabase(“破解”);
var collection=database.GetCollection(“密码”);
foreach(目录.GetFiles(“*”)中的var文件)
{
fileCount++;
Console.WriteLine(“[”+DateTime.Now.ToSortTimeString()+”]“+”处理文件:{“+file+”}{“+fileCount+”/“+directory.GetFiles(“*”.Count()+”}”);
List entitys=新列表();
foreach(File.ReadLines(filepath+@“\”+File.ToString())中的字符串行)
{
添加(新密码{Password=line});
}
collection.InsertManyAsync(entitys);
}
Console.WriteLine();
Console.WriteLine(“[”+DateTime.Now.ToSortTimeString()+“]”+“插入记录后,按任意键获取计数。”);
Console.ReadKey(true);
while(true)
{
Console.ReadKey(true);
}
}

尝试批量更新。这样,您就不会同时在内存中存储所有数据。它还可以帮助您不完全锁定数据库

...

foreach (var file in directory.GetFiles("*"))
{
    fileCount++;

    Console.WriteLine("  [" + DateTime.Now.ToShortTimeString() + "]" + " Working through file: {" + file + "} {" + fileCount + "/" + directory.GetFiles("*").Count() + "}");

    System.IO.StreamReader file = new System.IO.StreamReader(filepath + @"\" + file.ToString());

    while(!file.EndOfStream)
    {
        int passwordBatchCount = 0;
        List<Password> entitysBatch = new List<Password>();

        while ((string line = file.ReadLine()) != null && passwordBatchCount < BATCH_SIZE)
        {
            entitysBatch.Add(new Password { password = line });
            passwordBatchCount++;
        }

        collection.InsertManyAsync(entitysBatch);
    }

    file.Close();

    }
}

...
。。。
foreach(目录.GetFiles(“*”)中的var文件)
{
fileCount++;
Console.WriteLine(“[”+DateTime.Now.ToSortTimeString()+”]“+”处理文件:{“+file+”}{“+fileCount+”/“+directory.GetFiles(“*”.Count()+”}”);
System.IO.StreamReader file=new System.IO.StreamReader(filepath+@“\”+file.ToString());
而(!file.EndOfStream)
{
int passwordBatchCount=0;
List entitysBatch=新列表();
while((string line=file.ReadLine())!=null&&passwordBatchCount
您是在32位系统中运行还是在64位系统中运行?使用64位编译将有所帮助。。但是你不应该把所有的东西都保存在内存中,因为内存总是有限的,所以这永远不会起作用。。尝试在c#中使用weakreference,我不熟悉InsertManyAsync。如果集合设法将实体中的对象保留在内存中,那么很明显,您“正确”到达了内存的末尾。哪一行引发了异常?