Skip to content

grpc-js: Allow garbage collection of IDLE channels #2896

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

Conversation

murgatroid99
Copy link
Member

This addresses #2893 to the greatest extent reasonable.

This PR includes 3 changes to allow garbage collection of IDLE channels:

  • Modify the subchannel wrapper used by the channel so that it does not register itself until it has a ref, to handle the fact that pick_first may never ref some subchannels.
  • Don't run the callRefTimer until a call is started, and stop it when the channel goes IDLE.
  • Move the channelz info into a separate object, so that the reference from channelz only keeps that object alive instead of the whole channel.

A caveat here is that repeatedly creating and discarding channels without closing them will still leak some memory with channelz, because the channel is never unregistered with channelz, so channelz needs to retain the associated data. This does not happen if channelz is disabled for a channel.

@murgatroid99 murgatroid99 merged commit 6bd791d into grpc:@grpc/[email protected] Feb 4, 2025
4 of 5 checks passed
@igorescobar-moonpay
Copy link

Do you think this issue could be related somehow? googleapis/nodejs-pubsub#2016

@karliky
Copy link

karliky commented Feb 27, 2025

@igorescobar-moonpay are you properly closing the clients?

@murgatroid99
Copy link
Member Author

@igorescobar-moonpay The purpose of this change was to minimize the memory impact of continuously creating and discarding client objects. However, the best strategy is to not do that at all. For any given service on a particular server, you should only create one client object, and use it for the lifetime of the process. If for whatever reason your only option is to keep creating new clients, you should always explicitly close them when you discard them.

The heap dump screenshots in the linked issue show that a large amount of retained memory is in the Client class, which implies that you are in fact creating many client objects.

@igorescobar-moonpay
Copy link

@igorescobar-moonpay The purpose of this change was to minimize the memory impact of continuously creating and discarding client objects. However, the best strategy is to not do that at all. For any given service on a particular server, you should only create one client object, and use it for the lifetime of the process. If for whatever reason your only option is to keep creating new clients, you should always explicitly close them when you discard them.

The heap dump screenshots in the linked issue show that a large amount of retained memory is in the Client class, which implies that you are in fact creating many client objects.

I appreciate you taking the time to look into the screenshots and giving us some pointers! I will have a look!

@igorescobar-moonpay
Copy link

igorescobar-moonpay commented Feb 28, 2025

Thanks a lot for the pointer @murgatroid99! The issue was fixed, more here: googleapis/nodejs-pubsub#2016 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants