-
Notifications
You must be signed in to change notification settings - Fork 2k
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
base: master
Are you sure you want to change the base?
Conversation
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: rafaelri 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 |
Welcome @rafaelri! |
@@ -12,6 +12,18 @@ | |||
*/ | |||
package io.kubernetes.client.informer.cache; | |||
|
|||
import java.io.IOException; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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 |
Are there benchmarks or other validation that we can add to prove the effectiveness of this code? |
Local variables
list
anditems
will retain the list of objects returned in the initial list in the heap while theThread
that is runningrun
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.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.