Memory leak
littlej-x opened this issue · 4 comments
littlej-x commented
Describe the bug
Bellow is an example of usage of this library that causes a memory leak. Running this code will quickly use up the system's memory. With use of a memory profiler, I traced the source of the leak to line 323 of classbuilder.py if isinstance(val, ProtocolBase):
. At this point val
is "city1"
and self
is city1 <address_<anonymous> attributes: city>
from bellow example.
Example Schema and code
import python_jsonschema_objects as pjo
schema = {
"title": "person",
"type": "object",
"properties": {
"address": {
"type": "object",
"properties": {
"city": {"type": "string"},
},
"required": ["city"]
}
}
}
def person_from_data():
classes = pjo.ObjectBuilder(schema)
Person = classes.get_class('person')
Address = classes.get_class('person/address_<anonymous>')
return Person(address=Address(city="city1"))
def main():
while True:
person_from_data()
main()
Expected behavior
No memory leak should result from use of this library.
cwacek commented
This is the same as issue #210. You’re seeing this because you’re calling build_classes inside the loop. Move that out and you won’t see the memory increase each time.
… On Nov 17, 2022, at 9:39 PM, littlej-x ***@***.***> wrote:
Describe the bug
Bellow is an example of usage of this library that causes a memory leak. Running this code will quickly use up the system's memory. With use of a memory profiler, I traced the source of the leak to line 323 of classbuilder.py if isinstance(val, ProtocolBase):. At this point val is "city1" and self is city1 <address_<anonymous> attributes: city> from bellow example.
Example Schema and code
import python_jsonschema_objects as pjo
schema = {
"title": "person",
"type": "object",
"properties": {
"address": {
"type": "object",
"properties": {
"city": {"type": "string"},
},
"required": ["city"]
}
}
}
def person_from_data():
classes = pjo.ObjectBuilder(schema)
Person = classes.get_class('person')
Address = classes.get_class('person/address_<anonymous>')
return Person(address=Address(city="city1"))
def main():
while True:
person_from_data()
main()
Expected behavior
No memory leak should result from use of this library.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.
littlej-x commented
Ah ok, thank-you.
littlej-x commented
For my understanding, why then does this example not cause memory usage to increase.
import python_jsonschema_objects as pjo
schema = {
"title": "person",
"type": "object",
"properties": {
"address": {
"type": "object",
"properties": {
"city": {"type": "string"},
}
}
}
}
def person_from_data():
classes = pjo.ObjectBuilder(schema)
Person = classes.get_class('person')
Address = classes.get_class('person/address_<anonymous>')
person = Person(address=Address())
person.address.city = "city1"
return person
def main():
while True:
person_from_data()
main()
Where the line isinstance(val, ProtocolBase):
is never reached
?
cwacek commented
That’s a good question. I’ll need to look at that in more detail.
… On Nov 17, 2022, at 11:02 PM, littlej-x ***@***.***> wrote:
Why then does this example not cause memory usage to increase.
import python_jsonschema_objects as pjo
schema = {
"title": "person",
"type": "object",
"properties": {
"address": {
"type": "object",
"properties": {
"city": {"type": "string"},
}
}
}
}
def person_from_data():
classes = pjo.ObjectBuilder(schema)
Person = classes.get_class('person')
Address = classes.get_class('person/address_<anonymous>')
person = Person(address=Address())
person.address.city = "city1"
return person
def main():
while True:
person_from_data()
main()
Where the line isinstance(val, ProtocolBase): is never reached
?
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.