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

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Nothing. Since the property type is Type, the XAML parser… May 11, 2026 at 11:31 pm
  • Editorial Team
    Editorial Team added an answer I was thinking if you can create two sockets. One… May 11, 2026 at 11:31 pm
  • Editorial Team
    Editorial Team added an answer Yes, RadioButtonList renders horrible HTML. Anyway, it's still easy to… May 11, 2026 at 11:31 pm

Related Questions

I am a C++ programmer and recently joined a new company that uses a
Does anyone have, or know of, a binary patch generation algorithm implementation in C#?
For making a scalable multiuser server in C#, which of these two types of
From time to time have I come across manually implemented sort and/or search algorithms,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.