runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Reproducible memory leak with enabled memory cgroups in .NET 5

Open gerlachch opened this issue 5 years ago • 5 comments

Description

Since upgrading from .NET 3.1 to .NET 5, we have been observing a memory leak in our C# application. After further investigation, we think the issue is caused by a bug in the CGroup part of the dotnet runtime (see below).

The issue can be reproduced as follows:

  • Create a solution with the example application below and target either .NET 5 oder .NET 6 Preview
  • Build the application with targeting Linux
  • Run the application on a Linux system with memory cgroups enabled

Observed behavior:

  • The memory usage of the application increases linearly over time

Expected behavior:

  • The average memory usage of the application is constant over time

Example application:

using System;
using System.Collections.Generic;
using System.Threading;

namespace memtester
{
    static class Program
    {
        static void Main(string[] args)
        {
            var random = new Random();
            var counter = 0;
            var list = new List<string>();

            while (true)
            {
                for (int i = 0; i < 100; i++)
                {
                    list.Add($"Foobar: {++counter}");
                }

                if (random.Next(100) % 2 == 0)
                {
                    list = new List<string>();
                }

                Thread.Sleep(TimeSpan.FromMilliseconds(1));
            }
        }
    }
}

Configuration

The issue only occurs when targeting .NET 5 or .NET 6 Preview on various platforms running Linux when memory cgroups are enabled. The issue does not occur when targeting .NET 3.1.

We originally observed the problem on an i.MX6 system running a Yocto Linux, but were also able to reproduce it on a Raspberry Pi 3 running Raspbian after manually enabling memory cgroups.

Even though we only evaluated ARM platforms, the leak is very likely to also occur on x86.

Regression?

The issue occurs when targeting .NET 5 or .NET 6 Preview and is not present when targeting .NET 3.1.

Other information

The leak is likely to be caused by a bug in the CGroup part of the dotnet runtime.

The CGroup::GetCGroupMemoryUsage functions in both src\coreclr\gc\unix\cgroup.cpp and src\coreclr\pal\src\misc\cgroup.cpp seem to be missing a corresponding free for the memory allocated to hold the memory usage path string.

Here is the current implementation of the UNIX version for reference:

static bool GetCGroupMemoryUsage(size_t *val, const char *filename)
{
    if (s_memory_cgroup_path == nullptr)
        return false;

    char* mem_usage_filename = nullptr;
    if (asprintf(&mem_usage_filename, "%s%s", s_memory_cgroup_path, filename) < 0)
        return false;

    uint64_t temp = 0;
    bool result = ReadMemoryValueFromFile(mem_usage_filename, &temp);
    if (result)
    {
        if (temp > std::numeric_limits<size_t>::max())
        {
            *val = std::numeric_limits<size_t>::max();
        }
        else
        {
            *val = (size_t)temp;
        }
    }

    return result;
}

The code shows that the memory allocated at mem_usage_filename by the call to asprintf is not freed and lost after the GetCGroupMemoryUsage returns. Additionally, dumping the supposedly leaking sections of the application showed them to repeatedly contain exactly the path string created in this function.

gerlachch avatar Feb 26 '21 15:02 gerlachch

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ghost avatar Feb 26 '21 15:02 ghost

Tagging subscribers to this area: @dotnet/gc See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Since upgrading from .NET 3.1 to .NET 5, we have been observing a memory leak in our C# application. After further investigation, we think the issue is caused by a bug in the CGroup part of the dotnet runtime (see below).

The issue can be reproduced as follows:

  • Create a solution with the example application below and target either .NET 5 oder .NET 6 Preview
  • Build the application with targeting Linux
  • Run the application on a Linux system with memory cgroups enabled

Observed behavior:

  • The memory usage of the application increases linearly over time

Expected behavior:

  • The average memory usage of the application is constant over time

Example application:

using System;
using System.Collections.Generic;
using System.Threading;

namespace memtester
{
    static class Program
    {
        static void Main(string[] args)
        {
            var random = new Random();
            var counter = 0;
            var list = new List<string>();

            while (true)
            {
                for (int i = 0; i < 100; i++)
                {
                    list.Add($"Foobar: {++counter}");
                }

                if (random.Next(100) % 2 == 0)
                {
                    list = new List<string>();
                }

                Thread.Sleep(TimeSpan.FromMilliseconds(1));
            }
        }
    }
}

Configuration

The issue only occurs when targeting .NET 5 or .NET 6 Preview on various platforms running Linux when memory cgroups are enabled. The issue does not occur when targeting .NET 3.1.

We originally observed the problem on an i.MX6 system running a Yocto Linux, but were also able to reproduce it on a Raspberry Pi 3 running Raspbian after manually enabling memory cgroups.

Even though we only evaluated ARM platforms, the leak is very likely to also occur on x86.

Regression?

The issue occurs when targeting .NET 5 or .NET 6 Preview and is not present when targeting .NET 3.1.

Other information

The leak is likely to be caused by a bug in the CGroup part of the dotnet runtime.

The CGroup::GetCGroupMemoryUsage functions in both src\coreclr\gc\unix\cgroup.cpp and src\coreclr\pal\src\misc\cgroup.cpp seem to be missing a corresponding free for the memory allocated to hold the memory usage path string.

Here is the current implementation of the UNIX version for reference:

static bool GetCGroupMemoryUsage(size_t *val, const char *filename)
{
    if (s_memory_cgroup_path == nullptr)
        return false;

    char* mem_usage_filename = nullptr;
    if (asprintf(&mem_usage_filename, "%s%s", s_memory_cgroup_path, filename) < 0)
        return false;

    uint64_t temp = 0;
    bool result = ReadMemoryValueFromFile(mem_usage_filename, &temp);
    if (result)
    {
        if (temp > std::numeric_limits<size_t>::max())
        {
            *val = std::numeric_limits<size_t>::max();
        }
        else
        {
            *val = (size_t)temp;
        }
    }

    return result;
}

The code shows that the memory allocated at mem_usage_filename by the call to asprintf is not freed and lost after the GetCGroupMemoryUsage returns. Additionally, dumping the supposedly leaking sections of the application showed them to repeatedly contain exactly the path string created in this function.

Author: gerlachch
Assignees: -
Labels:

area-GC-coreclr, untriaged

Milestone: -

ghost avatar Feb 26 '21 15:02 ghost

@gerlachch you're right, thanks for tracking down the leak! free(mem_usage_filename) is missing. Do you want to make a PR to add it?

cc @janvorli @omajid

tmds avatar Mar 01 '21 13:03 tmds

@tmds I've worked with @gerlachch on this, he is on vaction today but we discussed this (PR) briefly. We'd welcome if you add the missing line and make the PR. Will this find its way into the next 5.0 service releases or only in 6.0?

hlippke avatar Mar 01 '21 14:03 hlippke

@tmds I wouldn't mind contributing the fix but as @hlippke said, I am not available today. I could get familiar with the guidelines and create a PR tomorrow. But either way would be fine.

gerlachch avatar Mar 01 '21 15:03 gerlachch