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.

Server-side memory leaks

See original GitHub issue

What version of gRPC and what language are you using?

  • dotnet version 6

  • Grpc packages:

    Google.Protobuf ==> Version=“3.23.1”
    Grpc.AspNetCore.Server.ClientFactory ==> Version=“2.55.0”
    Grpc.Net.Client ==> Version=“2.55.0”
    Grpc.Tools ==> Version=“2.55.1”

What operating system (Linux, Windows,…) and version?

Windows 10 [version 10.0.19045.3208]

Scenario

Following the example from the official website, a simple demo was written. I found that grpc-service memory kept going up and not down, even when I closed the calling client, the memory still did not go down

server code

    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        builder.WebHost.ConfigureKestrel(options =>
        {
            options.ListenAnyIP(4999, listenOptions => listenOptions.Protocols = HttpProtocols.Http2);
        });

        builder.Services.AddGrpc();

        var app = builder.Build();

        app.MapGrpcService<GreeterService>();
        
        app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. ");

        app.Run();
    }

    public class GreeterService : Greeter.GreeterBase
    {
        public GreeterService()
        {
        }

        public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
        {
            Console.WriteLine($"Starting call. Request: {request}");
            return Task.FromResult(new HelloReply
            {
                Message = "Hello " + request.Name
            });
        }
    }

client code

   static void Main(string[] args)
   {
       using var channel = GrpcChannel.ForAddress("http://127.0.0.1:4999");
       var client = new Greeter.GreeterClient(channel);

       //Press Enter and call grpc service 100 times
       while (string.IsNullOrWhiteSpace(Console.ReadLine()))
       {
           for(int i = 0; i < 100; i++)
           {
               var reponse = client.SayHello(new HelloRequest() { Name =$"zeke-{i}" });
           }
       }

       Console.WriteLine("bye!");
       Console.ReadLine();
   }

protos

   syntax = "proto3";

   option csharp_namespace = "GrpcService1";

   package greet;

   // The greeting service definition.
   service Greeter {
   // Sends a greeting
   rpc SayHello (HelloRequest) returns (HelloReply);
   }

   // The request message containing the user's name.
   message HelloRequest {
   string name = 1;
   }

   // The response message containing the greetings.
   message HelloReply {
   string message = 1;
   }

  • After the service is started, the memory is monitored as follows

    name memory
    GrpcService1.exe 44,568 k
  • The client calls 500 times for the first time, the memory check is as follows

    name memory
    GrpcService1.exe 49,188 k
  • The client calls 500 times for the second time, the memory is checked as follows

    name memory
    GrpcService1.exe 49,316 k
  • After repeating the previous step N times, close the client and check for memory as follows

    name memory
    GrpcService1.exe 55,840 k

As you can see, the memory keeps growing and is not reclaimed

I want to be consulted. Is there a problem with my usage? or Is there a memory leak in grpc?.
Look forward to your reply,Thanks for helping!

Issue Analytics

  • State:closed
  • Created 2 months ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
zeke202207commented, Jul 25, 2023

Thank you so much @gfoidl

0reactions
gfoidlcommented, Jul 24, 2023

This is the normal behavior of the .NET GC.

In the client do something like

DateTime start = DateTime.Now;

while (DateTime.Now <= start.AddSeconds(20))
{
    for (int i = 0; i < 100; i++)
    {
        HelloReply reponse = client.SayHello(new HelloRequest() { Name = $"zeke-{i}" });
        Console.WriteLine(reponse.Message);
    }
}

then – when the GC determines to run – the memory usage can drop…see link above.

Under a profiler (the one from VS) it looks on my machine like grafik

The red vertical bars show when the GC kicks in to collect objects. Can also be seen in the “Live Objects” graph above.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Server-Side Memory Leaks ; : r/nextjs
I've found plenty of articles talking about how to avoid memory leaks by react on the client side, from cleaning up useEffect subscriptions ......
Read more >
Hunting memory leaks in a server side rendered React ...
How to find the memory leak? · 1. Simulating the request load. As a start, I collected some network traffic with tShark (https://www.wireshark....
Read more >
Fixing memory leaks in web applications | Read the Tea Leaves
But it's extremely unlikely to leak memory on the client side, since the browser will clear the memory every time you navigate between...
Read more >
Memory leak on server side · Issue #12384 · nuxt/nuxt
Describe the bug ... In "Memory" tab take heap snapshot. ... Then take another snapshot. Repeat steps 5, 6 few times. You will...
Read more >
Debugging Memory Leaks in Node.js Applications
Memory leaks in long running Node.js applications are like ticking time bombs that, if left unchecked in production environments, can result in devastating ......
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