Thanks to visit codestin.com
Credit goes to github.com

Skip to content

separate initial load to avoid retaining list items in memory #4107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

rafaelri
Copy link

@rafaelri rafaelri commented Jun 24, 2025

Local variables list and items will retain the list of objects returned in the initial list in the heap while the Thread that is running run is alive. Leading to a minor but still a leak since those objects could be collected if the local variable was made out of scope.
This PR separates the initial load in a separate method that returns the resourceVersion and shortens the scope of the variables mentioned to allow garbage collection.

 ApiListType list =
          listerWatcher.list(
              new CallGeneratorParams(Boolean.FALSE, getRelistResourceVersion(), null));

      V1ListMeta listMeta = list.getMetadata();
      String resourceVersion = listMeta.getResourceVersion();
      List<? extends KubernetesObject> items = list.getItems();

This PR also removes the watcher attribute and replaces the closing logic to release resources as soon as the events on a particular watcher have been processed.

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: rafaelri
Once this PR has been reviewed and has the lgtm label, please assign brendandburns for approval. For more information see the Code Review Process.

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

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link

linux-foundation-easycla bot commented Jun 24, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot
Copy link
Contributor

Welcome @rafaelri!

It looks like this is your first PR to kubernetes-client/java 🎉. 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/java 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. 😃

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Jun 24, 2025
@@ -12,6 +12,18 @@
*/
package io.kubernetes.client.informer.cache;

import java.io.IOException;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this change and maintain the orginal order of imports.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something made VSCode change imports upon save. I ended up fixing them using vi. :(

return;
}

while (isActive.get()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think that this logic is quite the same as the while(true) logic, since it will recreate the watch even if it isn't active.

@@ -165,7 +141,13 @@ public void run() {
this.exceptionHandler.accept(apiTypeClass, t);
return;
} finally {
closeWatch();
if (watch != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the value of this refactor, can you keep the original closeWatch code?

@@ -278,6 +269,11 @@ private void watchHandler(Watchable<ApiType> watch) {
log.debug("{}#Receiving resourceVersion {}", apiTypeClass, lastSyncResourceVersion);
}
}
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is adding this code needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the changes were aimed in making it easier for the Response related resources to be reclaimed by the GC (as recommended per OkHttp that is the underlying http client used).
We've taken some heap dumps and noticed an increased number of GSON and byte[] classes held in memory and our suspicion is that the lack of a proper close and the two references held removed are the main culprit.

@brendandburns
Copy link
Contributor

Thank you for the PR!

I think it would be better if the PR was smaller/more focused. Please update this PR to just separate out the list/watch logic for your memory management purposes, and if you want to send a second refactor PR that's great too.

fwiw, I believe that explicitly setting things to null will also achieve the same nudge to the garbage collector and may be less invasive that this code refactor.

@brendandburns
Copy link
Contributor

Are there benchmarks or other validation that we can add to prove the effectiveness of this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants