skip to Main Content

I am having a strange issue with .NET Core 3.0 while using loggers.

The app works ok on Windows, but when I start it on Linux (Debian 10) as a daemon, it just keeps taking more and more memory.
The issue was first manifested when I was using NLog, then I switched to Serilog, but the issue is still here. The problem is not there when I remove NLog/Serilog.

Using memory snapshots and Jetbrains dotMemory all I get is a bunch of sbyte arrays that are created (probably) by NLog/Serilog.

When I disable logging to a file and leave just console logging – the problem disappears!

I tried adding manual calls to Garbage collector but that didn’t help.

We managed to create a workaround by adding MemoryMax parameter in ther .service file. It seems that garbage collector starts cleaning up when it is near the Max limit. (ie the limit is 150 MB and now the app is holding at 145 MB).

Does anyone have any idea what might be wrong? Or should I just address this issue with NLog and Serilog developers.

2

Answers


  1. I have also noticed high memory consumption. There have been a few different things to try including:

    • using .Destructure.ToMaximumCollectionCount(10) // Default is int.MaxValue
    • removing .Enrich.WithExceptionDetails to see if the memory issue magically disappears
    • Checking the Swap file memory usage of the Linux container and hence setting the Swapiness value (although take care of unintended consequences)

    To be certain about where the memory leak is happening though you will need to analyse the memory heap to look at what objects are using up the memory.

    SeriLog Memory Leak using EF

    Login or Signup to reply.
  2. NLog opens/closes the file for each file-write operation by default. This ensures the highest level of compatibility but it has a cost.

    Try configuring NLog like this:

    <nlog>
       <targets>
          <target type="file" name="logfile" fileName="log.txt" keepFileOpen="true" concurrentWrites="false" />
       </targets>
       <rules>
          <logger name="*" minLevel="Debug" writeTo="logfile" />
       </rules>
    </nlog>
    

    Do not use <targets async="true"> if memory allocation is an issue.

    NLog ver. 4.7 fixes a performance issue when using concurrentWrites="true" together with NLog archiving-options on non-Windows-platforms. Until NLog ver. 4.7 is released then one should consider configuring concurrentWrites="false" if possible.

    See also: https://github.com/NLog/NLog/wiki/performance

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search