Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7523287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:45:25+00:00 2026-05-30T02:45:25+00:00

Given this code: readFile = new FileStream(FilePath, FileMode.Open); streamReader = new StreamReader(readFile); dsSheet =

  • 0

Given this code:

readFile = new FileStream(FilePath, FileMode.Open);
streamReader = new StreamReader(readFile);

dsSheet = new DataSet();
dsSheet.Tables.Add(sValidNumbersLibrary);
dsSheet.Tables[sValidNumbersLibrary].Columns.Add("Numbers");
dt50Records = dsSheet.Tables[sValidNumbersLibrary].Clone();

String sLine = string.Empty;
sLine = streamReader.ReadToEnd();
// The next line fails with System.OutOfMemoryException:
sLine = (sLine.Contains(",")) ? sLine.Replace(",", "\r\n") : sLine;
sLine = sLine.Replace("\r\n", ",");

How should I handle this System.OutOfMemoryException?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:45:27+00:00Added an answer on May 30, 2026 at 2:45 am

    What are you planning to do with the altered string once you’re done with your manipulation? If you are just going to write it to a file, then perhaps you should read one line at a time, perform your manipulation, then write the altered line to the file. StreamReader’s readline will return everything until it encounters \r\n. Since you are planning on replacing \r\n with “,”, you can just append a comma to the string returned by readline.

    using (StreamWriter sw = new StreamWriter(Path.GetTempFileName()))
    {
        using (StreamReader sr = new StreamReader(@"YourFile"))
        {
            while (!sr.EndOfStream)
            {
                string line = sr.ReadLine();
                if (line != null)
                {
                    sw.Write(line + ",");
                }
            }
        }
    }
    

    If you are planning on using your manipulated text throughout the rest of your code, then you can build the string with StringBuilder (replace sw.Write with sb.Append); however, using StringBuilder will not guarantee that you won’t run into an OutOfMemoryException. A large string is a large string. If there’s no contiguous block of memory for your string, then you will get that OutOfMemoryException. Also, as @SteveCzetty pointed out, your calls to string.Replace() returns a copy of your string. The larger the string, the more likely you’re going to encounter the exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this code: using (var conn = new SqlConnection(...)) { conn.Open(); using (var cmd
Given this code: var arrayStrings = new string[1000]; Parallel.ForEach<string>(arrayStrings, someString => { DoSomething(someString); });
given this code: class Foo def bar return Bar.new end end class Bar ...
Given this code in Java: FileOutputStream os = new FileOutputStream(/tmp/test.dat); os.write(0x14); os.write(0xfe); os.write(0xae); os.write(String.valueOf((char)
Given this code (C#, .NET 3.5 SP1): var doc = new XmlDocument(); doc.LoadXml(<?xml version=\1.0\?><root>
Given this code: List<string> things = new List<string>(); foreach (string thing in things) {
Given this code in Java: int i,j; String[] names; names[0] = new String(mary); names[1]
I was given this code a while back. I finally got around to testing
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
I was given this Python code that would calculate an MD5 value for any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.