Skip to content

DotNetXmlSerializer leaks memory #1988

Closed
@Timo-Weike

Description

@Timo-Weike

DO NOT USE ISSUES FOR QUESTIONS

Describe the bug
The default xml-serializer DotNetXmlSerializer leaks memory in the form of dynamic assemblies.
As stated in the msdn for XmlSerializer, each instance of XmlSerializer should be cached for later use if it was not created by one of the mentioned self-caching constructors.

var serializer = new XmlSerializer(obj.GetType(), root);

To Reproduce
Run

var serializer = new DotNetXmlSerializer();

for (var i = 0; i < 1_000; i++)
{
    var res = serializer.Serialize(someObj);

    Console.WriteLine(AppDomain.CurrentDomain.GetAssemblies().Count());

    Thread.Sleep(100);
}

and start a memory profiler or take a memory dump after sometime.

Expected behavior
Reuse serializers.

Stack trace
Copy the full stack trace here if you get an exception.

Desktop (please complete the following information):

  • OS: Linux
  • .NET 6
  • Version 108.0.1

Additional context
Due to this memory-leak and a high amount of requestst we have a kubernetes hosted application that increases its memory consumtion by ~3GB in just 6 hours.
We will likly fix this issue our self by using a custom serilizer that includes caching.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions