系统内存不足异常将大型txt文件转换为数组

时间:2016-05-04 21:54:11

标签: c# arrays

以下代码适用于小型txt文件,但是如果我们有大型txt文件,则会在 string [] array = File.ReadAllLines(&#34; hash.txt&#34;)中给出outofmemory异常; < /强>

hash.txt文件是500 mb

我尝试了一些来自互联网的建议,但我没有做到这一点。

&#13;
&#13;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace Hash_Parser
{
    internal class Program
    {
        private static List<string> users = new List<string>();
        private static Dictionary<string, int> hash_original = new Dictionary<string, int>();
        private static List<string> hash_found = new List<string>();
        private static List<string> pass = new List<string>();
        private static string hash_path = "split.txt";
        private static void split()
        {
            Console.WriteLine("Splitting...");
            StreamWriter streamWriter = new StreamWriter("user.txt");
            StreamWriter streamWriter2 = new StreamWriter("hash.txt");
            string[] array = File.ReadAllLines(Program.hash_path);
            for (int i = 0; i < array.Length; i++)
            {
                string text = array[i];
                string[] array2 = text.Split(new char[]
               {
                   ':'
               }, 2);
                if (array2.Count<string>() >= 2)
                {
                    streamWriter.WriteLine(array2[0]);
                    streamWriter2.WriteLine(array2[1]);
                }
            }
            streamWriter.Close();
            streamWriter2.Close();
            Console.WriteLine("Saved as user.txt and hash.txt");
        }
        private static void populate()
        {
            Console.WriteLine("Populating lists...");
            Program.users.AddRange(File.ReadAllLines("user.txt"));
            Program.pass.AddRange(File.ReadAllLines("pass.txt"));
            Program.hash_found.AddRange(File.ReadAllLines("found.txt"));
            int num = 0;
            string[] array = File.ReadAllLines("hash.txt");
            for (int i = 0; i < array.Length; i++)
            {
                string key = array[i];
                Program.hash_original.Add(key, num);
                num++;
            }
        }
        private static void seek()
        {
            StreamWriter streamWriter = new StreamWriter("userpass.txt");
            int num = 0;
            int num2 = 100;
            foreach (string current in Program.hash_found)
            {
                if (Program.hash_original.ContainsKey(current))
                {
                    streamWriter.WriteLine(Program.users[Program.hash_original[current]] + ":" + Program.pass[num]);
                }
                num++;
                if (num >= num2)
                {
                    Console.Title = string.Concat(new object[]
                   {
                       "Processed: ",
                       num,
                       " : ",
                       Program.hash_found.Count
                   });
                    num2 += 1000;
                }
            }
            Console.Title = string.Concat(new object[]
           {
               "Processed: ",
               num,
               " : ",
               Program.hash_found.Count
           });
            streamWriter.Close();
        }
        private static void Main(string[] args)
        {
            Console.WriteLine("Split hash /split");
            Console.WriteLine("'split.txt'\n");
            Console.WriteLine("Parse hashes /parse");
            Console.WriteLine("'user.txt' | 'found.txt' | 'hash.txt' | 'pass.txt'");
            string a = Console.ReadLine();
            if (a == "/split")
            {
                Program.split();
            }
            else
            {
                if (a == "/parse")
                {
                    Program.populate();
                    Console.WriteLine("Processing...");
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    Program.seek();
                    stopwatch.Stop();
                    Console.WriteLine("Saved as userpass.txt");
                    Console.WriteLine("Time elapsed: " + stopwatch.Elapsed);
                    Console.ReadKey();
                }
            }
        }
    }
}
&#13;
&#13;
&#13; 为你们提供帮助。

2 个答案:

答案 0 :(得分:4)

试试这段代码:

foreach (var line in File.ReadLines(_filePath))
{
    //Don't put "line" into a list or collection.
    //Just make your processing on it.
}

带引号的文本:只需使用File.ReadLines,它返回一个IEnumerable并且不会立即将所有行加载到内存中。

引用链接:https://stackoverflow.com/a/13416225/3041974

我希望它有所帮助。

答案 1 :(得分:0)

请注意.NET中的进程限制

http://www.codeproject.com/Articles/483475/Memory-Limits-in-a-NET-Process

  

例如,32位系统的物理内存不能超过4 GB。毋庸置疑,2 ^ 32将为您提供具有4.294.967.296个不同条目的虚拟地址空间,这正是4GB限制的来源。但即使系统上有4GB可用,您的应用程序实际上也只能看到2GB。为什么?

     

因为在32位系统上,Windows会拆分虚拟地址空间   两个相等的部分:一个用于用户模式应用程序,另一个用于用户模式   用于内核(系统应用程序)。可以覆盖此行为   使用&#34; / 3gb&#34; Windows boot.ini配置文件中的标志。如果我们这样做   因此,系统将为用户应用程序保留3GB,并保留1 GB   对于内核。

任务管理器中的MEM使用过程是什么?