Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory leak in .NET Core 3.0 app when using NLog or Serilog

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.

like image 488
Marino F Avatar asked Jan 09 '20 08:01

Marino F


1 Answers

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

like image 198
sarin Avatar answered Sep 18 '22 20:09

sarin