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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:23:59+00:00 2026-05-15T10:23:59+00:00

So I’m using a StreamReader that uses a MemoryStream to write to a StreamWriter

  • 0

So I’m using a StreamReader that uses a MemoryStream to write to a StreamWriter and inside of this application but the memory usage increases by 300mb (From one of the larger inputs) and does not deallocate after Im done using it:

StreamWriter log = new StreamWriter("tempFile.txt");
log.Write(reader.ReadToEnd());
log.Close();

reader.DiscardBufferedData();
reader.Close();
reader.Dispose();
memoryStream.Dispose();
log.Dispose();
GC.Collect();

Before this and right after I get the RAM usage and before it is 300 mb less than after but I don’t know why. I have done everything I can think of to release that memory considering that the only thing going on here is the data from the reader is being placed in the text file I don’t see why any large amount of memory would even need to be used temporarily. Is there something I am missing?… Thanks.

  • 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-15T10:24:00+00:00Added an answer on May 15, 2026 at 10:24 am

    Are you looking at the RAM used by the process itself? I wouldn’t expect that to go down. The process will hang on to that memory and reuse it for further allocations. It doesn’t hand it back to the operating system. That’s just the way .NET tends to work.

    You can potentially get it to throw away the memory with some CLR API call – but usually I wouldn’t.

    This doesn’t mean the memory has really leaked – it’s still usable by the same process. For example, if you performed the same action again it probably wouldn’t need to increase the size of the heap – you’d see the memory usage stay flat at the process level. Use the CLR perfmon graphs to see the memory used within the managed heap to see if there’s a genuine leak.

    (There are also various different measure of how much memory an application is actually using. Which one is interesting depends on what you’re trying to do.)

    EDIT: If your code snippet is genuinely indicative of the code you’re using, then there’s a much simpler alternative: stream the data.

    using (TextWriter writer = File.CreateText("tempFile.txt"))
    {
        CopyText(reader, writer);
    }
    
    static void CopyText(TextReader reader, TextWriter writer)
    {
        char[] buffer = new char[8192];
        int charsRead;
        while ((charsRead = reader.Read(buffer, 0, buffer.Length)) > 0)
        {
            writer.Write(buffer, 0, charsRead);
        }
    }
    

    Note that unless you’re actually changing the encoding, you could do this without using a TextWriter/TextReader pair to start with.

    That way you won’t need to have the whole string in memory as well as its binary representation (in the MemoryStream). You’ll still have the binary representation of course – do you have to write everything into the MemoryStream to start with?

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

Sidebar

Ask A Question

Stats

  • Questions 533k
  • Answers 533k
  • 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 Hey you should remove that has 1, :user line from… May 17, 2026 at 12:31 am
  • Editorial Team
    Editorial Team added an answer The initWith... methods are instance methods, not class methods. Change… May 17, 2026 at 12:31 am
  • Editorial Team
    Editorial Team added an answer Looks to me like you are joining on the wrong… May 17, 2026 at 12:31 am

Trending Tags

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

Top Members

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
In order to apply a triggered animation to all ToolTip s in my app,
I want use html5's new tag to play a wav file (currently only supported
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.