2013-04-18 44 views
0

后,我想读一个文件,该代码,但经过682次我得到这个错误:出的存储器中读取读取682 txt文件

Exception of type 'System.OutOfMemoryException' was thrown.

System.IO.StreamReader file = new System.IO.StreamReader(scanpad[lusteller]); 

scanpad与文件路径和lusteller是一个数组反它。 Scanpad感染11207个文件。

我也看过恩没有对地方682文件

任何一个解决方案吗?

这里完整版我的代码

 using (StreamReader file = new System.IO.StreamReader(scanpad[lusteller])) 
     { 
      //do stuff 

      lengte = bestandsnaam.Length; 
      lengte = lengte - 13; 
      controller = bestandsnaam.Remove(lengte, 13);   // controllertype uit naam halen 
      do 
      { 
       rij = file.ReadLine(); 
       if (rij != null)    //error op volgende lijn vermijden 
       { 
        if (rij.StartsWith("0") || rij.StartsWith("1") || rij.StartsWith("2") || rij.StartsWith("3")) 
        { 
         rijcheck = true; 
        } 

        teller = teller + 1; 
        if (teller > 10) 
        { 
         if (rijcheck == true)   // rij is datumlijn 
         { 
          string[] split = rij.Split(' '); 
          foutinformatie[0, index] = type;      //type ophalen 
          foutinformatie[3, index] = controller;    //controllernaam ophalen 
          foutinformatie[1, index] = split[0];    //datum ophalen 
          foutinformatie[2, index] = split[1];    //tijd ophalen 
          foutinformatie[4, index] = split[2];    //foutcode ophalen 
          foutinformatie[5, index] = split[5];    //foutteller ophalen 
          if (controller.StartsWith("MPU") == true) 
          { 
           lusarraygraad = 5; 
           while (lusarraygraad < 360) 
           { 

            if (graadmpu[0, lusarraygraad] == split[2]) 
            { 
             try 
             { 
              graad = graadmpu[1, lusarraygraad]; 
              foutinformatie[7, index] = graad; 
             } 
             catch 
             { 
             } 
             lusarraygraad = lusarraygraad + 499; 
             graadgevonden = true; 
            } 
            lusarraygraad = lusarraygraad + 1; 
           } 
           foutinformatie[7, index] = graad; 
           if (graadgevonden == false) 
           { 
            foutinformatie[7, index] = ""; 
           } 
           graadgevonden = false; 
          } 

          ////////////////////////////////////////////////////////////////////////// 
          if (controller.StartsWith("AAUX") == true) 
          { 
           lusarraygraad = 4; 
           while (lusarraygraad < 30) 
           { 

            if (graadaaux[0, lusarraygraad] == split[2]) 
            { 
             try 
             { 
              graad = graadaaux[1, lusarraygraad].ToString(); 
              foutinformatie[7, index] = graad; 
             } 
             catch 
             { 
             } 
             lusarraygraad = lusarraygraad + 499; 
             graadgevonden = true; 
            } 
            lusarraygraad = lusarraygraad + 1; 
           } 
           foutinformatie[7, index] = graad; 
           if (graadgevonden == false) 
           { 
            foutinformatie[7, index] = ""; 
           } 
           graadgevonden = false; 
          } 
          if (controller.StartsWith("ACTRL") == true) 
          { 
           lusarraygraad = 6; 
           while (lusarraygraad < 85) 
           { 

            if (graadactrl[0, lusarraygraad] == split[2]) 
            { 
             try 
             { 
              graad = graadactrl[1, lusarraygraad].ToString(); 
              foutinformatie[7, index] = graad; 
             } 
             catch 
             { 
             } 
             lusarraygraad = lusarraygraad + 499; 
             graadgevonden = true; 
            } 
            lusarraygraad = lusarraygraad + 1; 
           } 
           foutinformatie[7, index] = graad; 
           if (graadgevonden == false) 
           { 
            foutinformatie[7, index] = ""; 
           } 
           graadgevonden = false; 
          } 

          try 
          { 
           telleromschrijving = 6; 
           informatie = ""; 
           while (split[telleromschrijving] != " ") 
           { 
            informatie = informatie + " " + split[telleromschrijving]; 
            telleromschrijving = telleromschrijving + 1; 
           } 
          } 
          catch 
          { 
           foutinformatie[6, index] = informatie;    //foutteller ophalen 
          } 
          rijcheck = false; 
          rij = file.ReadLine(); 
          while (varcheck < 40) 
          { 
           // rij met eerste variable 

           if (rij != "") 
           { 
            variable[indexlokaal, varteller] = rij; 
            foutinformatie[varteller + 8, index] = variable[indexlokaal, varteller] = rij; 
            varteller = varteller + 1; 
            rij = file.ReadLine(); 
           } 
           else 
           { 
            foutinformatie[varteller + 8, index] = " "; 

            varteller = varteller + 1; 
           } 
           varcheck = varcheck + 1; 
          } 
          varcheck = 0; 
          varteller = 0; 
          indexlokaal = indexlokaal + 1; 
          index = index + 1; 
         } 
        } 
       } 
      } 
      while (rij != null); 
      file.Close(); 
      file.Dispose(); 
     } 
+2

听起来像你不使用'使用' – Sayse 2013-04-18 07:03:59

+0

你是否同时工作的流? – 2013-04-18 07:04:12

+1

你能发布更多你正在使用的代码,例如你正在使用的循环吗? – 2013-04-18 07:05:08

回答

0

尝试使用Using:(读后配置内存)

using(System.IO.StreamReader file = new System.IO.StreamReader(scanpad[lusteller])) 
     { 

     } 
3

我想象682仅仅是一个数字,你碰巧被耗尽内存,而不是一个特殊的数字。你应该封装你的StreamReader到

using(StreamReader file = new System.IO.StreamReader(scanpad[lusteller])) 
{ 
//do stuff 
} 

这样,你的StreamReader正在使用后被丢弃,清除内存

EDIT FOR UPDATE

里面你的StreamReader你也有

     rij = file.ReadLine(); 
         while (varcheck < 40) 
         { 
          // rij met eerste variable 

          if (rij != "") 
          { 
           variable[indexlokaal, varteller] = rij; 
           foutinformatie[varteller + 8, index] = variable[indexlokaal, varteller] = rij; 
           varteller = varteller + 1; 
           rij = file.ReadLine(); 
          } 

但您不检查ReadLine是否再次返回空值,查找处的G http://msdn.microsoft.com/en-GB/library/system.io.streamreader.readline.aspx

这也可能会导致内存溢出的异常,因为您的流已经达到零。可是你还继续起

+0

OK谢谢,我会尝试 – user2282583 2013-04-18 07:06:09

+0

这给了我同样的问题 – user2282583 2013-04-18 07:28:01

+0

编辑你的问题,显示的更新...(或许更多有关如何使用这条线了!) – Sayse 2013-04-18 07:29:06

1

这一切都取决于你使用的StreamReader做什么。

首先你需要处理它,但我不认为这是GC在内存耗尽之前收集它的问题(除非你也持有对它的引用)。

如果您使用类似于“ReadToEnd”的内容并且文件内容很大,您可以使用创建的字符串(如果通过创建子字符串来操纵它们可能会变得更糟)来打大对象堆。 过了一段时间堆会变得碎片化,因此无法为您的请求找到足够大的内存块。

那么,这是我的猜测,你的代码很少有信息,祝你好运。

+0

+ 1ed为读取结束,我不相信这是问题,因为我无法想象任何人想读11207不同大小的文件,所以我认为他们都是个别但同样大小的文件 – Sayse 2013-04-18 07:26:18

+0

也许他是扫描数据文件插入数据到数据库,或类似的文件搜索..有很多情况下,坦率地扫描完全相同大小的11K文件似乎不太可能:) – Arthur 2013-04-18 07:30:05

+0

我正在扫描文件,把它们放在一个SQL数据库。但我必须有txt文件的每一行 – user2282583 2013-04-18 07:40:57