-
-
Notifications
You must be signed in to change notification settings - Fork 114
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
memory leak of GenConverter with detailed_validation = True #290
Comments
Interesting, the hooks should be generated only once in any case. Would be curious if you can get a small repro? |
I tried getting a repro: from attrs import define
from cattrs import GenConverter
c = GenConverter()
@define
class Test:
a: int
b: int
c: int
while True:
c.structure({"a": 1, "b": 2, "c": 3}, Test) but it doesn't leak. |
This issue is found in one of my test server which has little memory, in a scheduled task loop which cause memory usage too high, had to reboot after running 2 days. I have made my converter usage as singleton. I can give a simple test as below:
|
Ah, you seem to be creating a converter in a loop. I strongly advise not doing that, not only with cattrs, but also with libraries like attrs and dataclasses. All of these libraries have heavy initialization steps (including code generation) to make them faster afterwards; if you're constantly creating new converters you're paying the price and getting no benefit. This is probably a leak somewhere in linecache, yeah. The hooks are generated once per converter. I can take a look when I have spare cycles, but due to what I said in the first paragraph I think this is lower prio. |
yes,don't create instance repeatedly if not necessary,agree that. |
This should actually already be fixed by #464 |
while True:
# creating GenConverter instance with all default params.
# registering some simple structure hook,
# loading some data,
the memory usage of process keep increasing.
If set detailed_validation = False, there is no memory leak. I'm not sure if it cost by line cache.
The text was updated successfully, but these errors were encountered: