Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Out of Memory Exception while reading Text File

Out of Memory Exception while reading Text File

Scheduled Pinned Locked Moved C#
databaseperformancehelpquestionannouncement
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Revathij
    wrote on last edited by
    #1

    Hi, My requirement is to read the file one by one and update the content in database. I'm getting Out of memory Exception while reading 32Mb text file. I ve used streamReader to read the File. StreamReader strReader = new StreamReader(filePath); return strReader.ReadToEnd(); I have tried with ReadLine() also. Same error comes. Is there any solutions for this? Thanks in advance

    M K L D 4 Replies Last reply
    0
    • R Revathij

      Hi, My requirement is to read the file one by one and update the content in database. I'm getting Out of memory Exception while reading 32Mb text file. I ve used streamReader to read the File. StreamReader strReader = new StreamReader(filePath); return strReader.ReadToEnd(); I have tried with ReadLine() also. Same error comes. Is there any solutions for this? Thanks in advance

      M Offline
      M Offline
      Manas Bhardwaj
      wrote on last edited by
      #2

      String _result = String.Empty;
      using(StreamReader strReader = new StreamReader(filePath))
      {
      _result = strReader.ReadToEnd();
      }
      return _result;

      Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

      1 Reply Last reply
      0
      • R Revathij

        Hi, My requirement is to read the file one by one and update the content in database. I'm getting Out of memory Exception while reading 32Mb text file. I ve used streamReader to read the File. StreamReader strReader = new StreamReader(filePath); return strReader.ReadToEnd(); I have tried with ReadLine() also. Same error comes. Is there any solutions for this? Thanks in advance

        K Offline
        K Offline
        Keith Barrow
        wrote on last edited by
        #3

        The problem is because your machine doesn't have enough memory to read the string, probably because the buffer required can't be created.See http://msdn.microsoft.com/en-us/library/system.io.streamreader.readtoend(VS.80).aspx[[^](http://msdn.microsoft.com/en-us/library/system.io.streamreader.readtoend
        (VS.80).aspx "New Window")] You'll have more success reading each files in chunks, see http://msdn.microsoft.com/en-us/library/9kstw824(VS.80).aspx[^] Hope this helps!

        1 Reply Last reply
        0
        • R Revathij

          Hi, My requirement is to read the file one by one and update the content in database. I'm getting Out of memory Exception while reading 32Mb text file. I ve used streamReader to read the File. StreamReader strReader = new StreamReader(filePath); return strReader.ReadToEnd(); I have tried with ReadLine() also. Same error comes. Is there any solutions for this? Thanks in advance

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, how many files (and how many megabytes of accumulated file data) does it take to get the OOM Exception? I suspect you are getting large-heap fragmentation. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


          1 Reply Last reply
          0
          • R Revathij

            Hi, My requirement is to read the file one by one and update the content in database. I'm getting Out of memory Exception while reading 32Mb text file. I ve used streamReader to read the File. StreamReader strReader = new StreamReader(filePath); return strReader.ReadToEnd(); I have tried with ReadLine() also. Same error comes. Is there any solutions for this? Thanks in advance

            D Offline
            D Offline
            Dan Neely
            wrote on last edited by
            #5

            Luc is probably right about heap fragmentation being the problem. Instead of creating new strings with each read and flogging the heap to death like this:

            string fileText ="";

            while (strReader.EndOfFile())
            {
            fileText += strReader.ReadLine();
            }

            return fileText;

            Use a stringBuilder initialized to the size of the file so that you only have a single memory allocation.

            stringBuilder fileText = new stringBuilder(sizeOfFile);

            while (strReader.EndOfFile())
            {
            fileText.AppendLine(strReader.ReadLine());
            }

            return fileText;

            It is a truth universally acknowledged that a zombie in possession of brains must be in want of more brains. -- Pride and Prejudice and Zombies

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups