question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Following program causes garbage collection

See original GitHub issue

using ZeroLog;
using ZeroLog.Appenders;
using ZeroLog.Configuration;

LogManager.Initialize(new ZeroLogConfiguration()
{
    LogMessagePoolSize = 10240,
    RootLogger =
    {
        Appenders =
        {
            new ConsoleAppender()
        }
    }
});

var logger = LogManager.GetLogger("Main");

var rand = new Random();
while (true)
{
    var r = rand.Next();
    Thread.Sleep(100);
    logger.Info($"rand: {r}");
}

when running the above program using 2.0.1 nuget package of ZeroLog, I can see the system is doing garbage collection.

Maybe I’m misunderstanding what is going on, but I thought that the logging was meant to be zero allocation and thus no garbage should be created.

Issue Analytics

  • State:closed
  • Created 8 months ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ltrzesniewskicommented, Feb 6, 2023

Well, oops. Sorry about that. 😬

Turns out, UTF8Encoding has an optimization that OSEncoding does not: the base Encoding class allocates by default, and UTF8Encoding overrides that part. 😞 Maybe that’s something I should report as an issue with OSEncoding

Just FYI, you have some easier solutions that avoid allocations than copying the ConsoleAppender code:

  • An appender that inherits ConsoleAppender

    class MyAppender : ConsoleAppender
    {
        public MyAppender()
        {
            Encoding = Encoding.Default;
        }
    }
    
  • Using new TextWriterAppender(Console.Out) with ZeroLog v2.1.0-pre2 (but that removes color)

  • Setting Console.OutputEncoding = Encoding.UTF8; before the ConsoleAppender is instantiated should also do the trick (if you can do that for your app).

In any case, that will be fixed in ZeroLog v2.1.0 final. 🙂

1reaction
petdrcommented, Feb 6, 2023

Thanks for the prompt response and can confirm that in my real app I’m no longer seeing allocations.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is garbage collection? Automated memory ...
The biggest issues are the overhead of the garbage collector; unpredictable stalls when the garbage collector decides to run; and increased lag ...
Read more >
What is garbage collection (GC) in programming?
Garbage collection is an ongoing process that requires central processing unit resources, which can affect an application's general performance or even disrupt ...
Read more >
Garbage Collection in Java
Whenever JVM runs the Garbage Collector program, then only the object will be destroyed. But when JVM runs Garbage Collector, we can not...
Read more >
Garbage Collection and Performance
Read about issues related to garbage collection and memory usage. Learn to minimize the effect of garbage collection on your applications.
Read more >
Garbage Collection in Java – What is GC and How it Works ...
Garbage collection makes Java memory efficient because it removes the unreferenced objects from heap memory and makes free space for new objects ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found