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 85475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:05:43+00:00 2026-05-10T22:05:43+00:00

In C# which is more memory efficient: Option #1 or Option #2? public void

  • 0

In C# which is more memory efficient: Option #1 or Option #2?

public void TestStringBuilder() {     //potentially a collection with several hundred items:     string[] outputStrings = new string[] { 'test1', 'test2', 'test3' };      //Option #1     StringBuilder formattedOutput = new StringBuilder();     foreach (string outputString in outputStrings)     {         formattedOutput.Append('prefix ');         formattedOutput.Append(outputString);         formattedOutput.Append(' postfix');          string output = formattedOutput.ToString();         ExistingOutputMethodThatOnlyTakesAString(output);          //Clear existing string to make ready for next iteration:         formattedOutput.Remove(0, output.Length);     }      //Option #2     foreach (string outputString in outputStrings)     {         StringBuilder formattedOutputInsideALoop = new StringBuilder();          formattedOutputInsideALoop.Append('prefix ');         formattedOutputInsideALoop.Append(outputString);         formattedOutputInsideALoop.Append(' postfix');          ExistingOutputMethodThatOnlyTakesAString(            formattedOutputInsideALoop.ToString());     } }  private void ExistingOutputMethodThatOnlyTakesAString(string output) {     //This method actually writes out to a file.     System.Console.WriteLine(output); } 
  • 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. 2026-05-10T22:05:43+00:00Added an answer on May 10, 2026 at 10:05 pm

    Several of the answers gently suggested that I get off my duff and figure out it myself so below are my results. I think that sentiment generally goes against the grain of this site but if you want something done right, you might as well do…. 🙂

    I modified option #1 to take advantage of @Ty suggestion to use StringBuilder.Length = 0 instead of the Remove method. This made the code of the two options more similar. The two differences are now whether the constructor for the StringBuilder is in or out of the loop and option #1 now uses the the Length method to clear the StringBuilder. Both options were set to run over an outputStrings array with 100,000 elements to make the garbage collector do some work.

    A couple answers offered hints to look at the various PerfMon counters & such and use the results to pick an option. I did some research and ended up using the built-in Performance Explorer of the Visual Studio Team Systems Developer edition that I have at work. I found the second blog entry of a multipart series that explained how to set it up here. Basically, you wire up a unit test to point at the code you want to profile; go through a wizard & some configurations; and launch the unit test profiling. I enabled the .NET object allocation & lifetime metrics. The results of the profiling where difficult to format for this answer so I placed them at the end. If you copy and paste the text into Excel and massage them a bit, they’ll be readable.

    Option #1 is the most memory efficiency because it makes the garbage collector do a little less work and it allocates half the memory and instances to the StringBuilder object than Option #2. For everyday coding, picking option #2 is perfectly fine.

    If you’re still reading, I asked this question because Option #2 will make the memory leak detectors of an experience C/C++ developer go ballistic. A huge memory leak will occur if the StringBuilder instance is not released before being reassigned. Of course, we C# developers don’t worry about such things (until they jump up and bite us). Thanks to all!!


    ClassName   Instances   TotalBytesAllocated Gen0_InstancesCollected Gen0BytesCollected  Gen1InstancesCollected  Gen1BytesCollected =======Option #1                     System.Text.StringBuilder   100,001 2,000,020   100,016 2,000,320   2   40 System.String   301,020 32,587,168  201,147 11,165,268  3   246 System.Char[]   200,000 8,977,780   200,022 8,979,678   2   90 System.String[] 1   400,016 26  1,512   0   0 System.Int32    100,000 1,200,000   100,061 1,200,732   2   24 System.Object[] 100,000 2,000,000   100,070 2,004,092   2   40 ======Option #2                  System.Text.StringBuilder   200,000 4,000,000   200,011 4,000,220   4   80 System.String   401,018 37,587,036  301,127 16,164,318  3   214 System.Char[]   200,000 9,377,780   200,024 9,379,768   0   0 System.String[] 1   400,016 20  1,208   0   0 System.Int32    100,000 1,200,000   100,051 1,200,612   1   12 System.Object[] 100,000 2,000,000   100,058 2,003,004   1   20 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Which is more efficient for the compiler and the best practice for checking whether
Which is more efficient? SELECT theField FROM theTable GROUP BY theField or SELECT DISTINCT
Question of the century? I basically want to know which would be more efficient
Which of these two different models would be more efficient (consider thrashing, utilization of
I'm new to Java. Which is more efficient for Vectors -- clear() or removeAllElements().
Which of the two queries is more efficient? SELECT *FROM Emp WHERE DeptNo<>20 SELECT
I'm looking for a more efficient way to reprioritize items in a priority queue.
I have a number of tracks recorded by a GPS, which more formally can
Yahoo has separate map for India ( which has more details than the regular
I have an excel file which has more than 65536 rows. However, I can

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.