kubernetes-client / csharp

Officially supported dotnet Kubernetes Client library
Apache License 2.0
1.09k stars 294 forks source link

Add missing Dispose() calls #1552

Closed brianpursley closed 5 months ago

brianpursley commented 5 months ago

While not recommended, if you repeatedly create a Kubernetes client inside of a loop, there appears to be a memory leak, even if you dispose it.

This PR adds the following to fix this:

Possible fix for #1539 (I suspect that issue is not related only to aot).

Test Results

I tested this using the following console program and running in Rider using memory profiling (sampled allocations):

using k8s;

var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();

for (var i = 0; i < 1000; i++)
{
    using var client = new Kubernetes(config);
    var list = client.CoreV1.ListNamespacedPod("kube-system");
    Console.WriteLine(string.Join(", ", list.Items.Select(x => x.Metadata.Name)));
}

Before the fix

Memory increases slowly, eventually reaching ~163MB by the end of the program: BeforeFix1

After the fix

Memory holds stead around ~123MB: AfterFix1

k8s-ci-robot commented 5 months ago

Welcome @brianpursley!

It looks like this is your first PR to kubernetes-client/csharp 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-client/csharp has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. :smiley:

brianpursley commented 5 months ago

Hmm, i think I can't do this:

using var httpRequest = new HttpRequestMessage

... because this is not awaited ...

return SendRequestRaw("", httpRequest, cancellationToken);

So the using disposes httpRequest before SendRequestRaw has executed.

SendRequest should probably use async/await, but I think that is a different problem. I will update this PR to remove that change.

brianpursley commented 5 months ago

OK, removed the using change. I ran the profile again and it was unaffected, so maybe that change doesn't really make a difference anyway.

tg123 commented 5 months ago

Good catch

/LGTM

codecov-commenter commented 5 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

:exclamation: No coverage uploaded for pull request base (master@3dae1cf). Click here to learn what that means.

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #1552 +/- ## ========================================= Coverage ? 61.50% ========================================= Files ? 103 Lines ? 3068 Branches ? 639 ========================================= Hits ? 1887 Misses ? 1181 Partials ? 0 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

k8s-ci-robot commented 5 months ago

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brianpursley, tg123

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files: - ~~[OWNERS](https://github.com/kubernetes-client/csharp/blob/master/OWNERS)~~ [tg123] Approvers can indicate their approval by writing `/approve` in a comment Approvers can cancel approval by writing `/approve cancel` in a comment