Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# console logging: will it have memory issues?

So I'm working with a console app that will persist for days, weeks, or months at a time.

It logs useful information to a text file, but it also writes to the Console. Do I have to worry about the console memory not getting purged for some reason. It seems to be okay.

I wrote a little test to run over lunch. I added some randomly spaced letters so that my coworkers think I must be processing something large and can take a longer lunch.

for (Int64 i = 0; i < 1000000000000000000; i++)
            {
                string random = Path.GetRandomFileName();
                random = random.Replace(".", "");
                random = random.Replace("m", "               ");
                random = random.Replace("a", "                                  ");
                random = random.Replace("h", "                         ");


                Console.WriteLine("i " + i + "   " + random);

            }

So, memory looks stable without writing to a file. I just want some confirmation that if I do have a memory issue, it's not from the console...

Memory does climb ever so slightly if I log it to a text file as well.

like image 861
FlavorScape Avatar asked Mar 30 '26 19:03

FlavorScape


2 Answers

It logs useful information to a text file, but it also writes to the Console. Do I have to worry about the console memory not getting purged for some reason. It seems to be okay.

Not unless you have your console set up to have an enormous buffer, or it's redirected to an in-memory file system, or something like that. In normal situations, it should be fine.

like image 181
Jon Skeet Avatar answered Apr 01 '26 12:04

Jon Skeet


Not an issue, any text that scrolls off the console buffer as specified by Console.SetBufferSize() falls in the bit-bucket. The maximum buffer size is 64KB, a restriction that the Console class forgets to check btw.

like image 24
Hans Passant Avatar answered Apr 01 '26 12:04

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!