question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Memory growth fast when schema is defined in a function

See original GitHub issue

When I use the following code, I observe that memory growth is very fast, but when I define the schema outside the function, there is no problem.

def test_schema():
    class FooSchema(Schema):
        foo = fields.String()
    # not do anything
    return "xxx"
if __name__ == '__main__':
    while True:
        print test_schema()```

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jtillmancommented, Jul 21, 2017

Hey @justanr, thanks for the reasoning and details explanation here which I appreciate. I too have hit this issue which causes high memory issues in my production environment. Now that I am aware of it I would like to know the suggested solution, in what I see as a valid use case here.

I typically use inheritance to generate classes to give standard packaging.

class ResponseObject(Schema):
    @classmethod
    def load_paged_result(cls, data):
        class ResponsePageObject(Schema):
            page_number = fields.Integer()
            total = fields.Integer()
            items = fields.List(fields.Nested(cls))
        return ResponsePageObject.loads(data)

class UserResponse(ResponseObject):
    name = fields.String()

This way I can easily standardize my response formats across my schemas.

UserResponse.load_paged_result(response.content)

Is there a suggested solution here. I was thinking about manually checking things in spots, but I fear its hard to train others users to follow suit and avoid the memory leaks.

1reaction
sloriacommented, Jul 21, 2017

@jtillman You can prevent marshmallow from storing your generated classes in its internal registry of schemas by generating the classes without a name.

class ResponseObject(Schema):
    @classmethod
    def load_paged_result(cls, data):
        fields = {
            'page_number': fields.Integer(),
            'total': fields.Integer(),
            'items': fields.List(fields.Nested(cls)),
        }
        attrs = dict(fields, Meta=Meta)
        # Create a nameless class to prevent storing the class in
        # marshmallow's in-memory registry
        ResponsePageObject = type(str(''), (ma.Schema,), attrs)
        return ResponsePageObject(strict=True).loads(data)

class UserResponse(ResponseObject):
    name = fields.String()

I’ve also proposed #660 , which would expose a more straightforward way to bypass the registry. In the meantime, the above workaround should get you most of the way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Schemas and Memory - Psychologist World
Schemas and Memory. Schemas (or schemata) refer to a type of cognitive heuristic which facilitates our understanding of our environment.
Read more >
Memory Schema - an overview | ScienceDirect Topics
Here, schema memory processing refers to the processes of integrating newly ... emphasis on functions of inner psychic structures and subjective meaning ......
Read more >
Schema in Psychology: Definition, Types, Examples
In Piaget's theory, a schema is both the category of knowledge as well as the process of acquiring that knowledge.
Read more >
The Role of a Schema in Psychology
A schema is a knowledge structure that allows organisms to interpret and understand the world around them. Schemata are a method of organizing ......
Read more >
What Is a Schema in Psychology? Definition and Examples
A schema is a mental structure that helps organize knowledge into categories and understand and interpret new information.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found