cwacek/python-jsonschema-objects

Memory leak

littlej-x opened this issue · 4 comments

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.

Ah ok, thank-you.

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
?