Skip to content
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

reduce memory usage of Transport #1470

Merged
merged 5 commits into from
Dec 23, 2021
Merged

Conversation

pawl
Copy link
Contributor

@pawl pawl commented Dec 21, 2021

While investigating a redis broker memory leak, I noticed the same issue as py-amqp's #377:

Top 10 lines
#1: /celery_app/kombu/kombu/transport/virtual/base.py:911: 782.4 KiB
    ARRAY_TYPE_H, range(self.channel_max, 0, -1),
<truncated>

The array that's used to store available connection ids is taking up an unnecessary amount of memory (more than anything else while running celery), and it's making memory leaks worse.

This PR reduces the memory usage of the Transport by reversing that logic and turning _avail_channel_ids into _used_channel_ids. This will only store the channel ids that are currently used, which is almost always going to be a much smaller array.

self.channel_id = self.connection._avail_channel_ids.pop()
except IndexError:
# Cast to a set for fast lookups, and keep stored as an array for lower memory usage.
used_channel_ids = set(self.connection._used_channel_ids)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe we should put this _used_channel_ids stuff into its own method like it is in py-amqp?: https://github.com/celery/py-amqp/blob/6433be13199938f47bbc2826e6585e746baf4f9b/amqp/connection.py#L484-L492

Copy link
Member

Choose a reason for hiding this comment

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

if that improves the codebase you could give it a try

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I went ahead and made this change: a439ada

@auvipy
Copy link
Member

auvipy commented Dec 22, 2021

also can you and your team please check the two open PR in pyamqp with this two PR in kombu & celery that nothing create any regression? I like your efforts very much, just being too cautious as it's holiday season coming.

@auvipy
Copy link
Member

auvipy commented Dec 22, 2021

@michael-lazar

@auvipy auvipy added this to the 5.2.x milestone Dec 22, 2021
@auvipy auvipy merged commit 507b306 into celery:master Dec 23, 2021
keithgg pushed a commit to open-craft/kombu that referenced this pull request Aug 11, 2022
* reduce memory usage of Transport

* fix flake8 errors

* move channel_id login into _get_free_channel_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants