Get the latest tech news

Loading Pydantic models from JSON without running out of memory


Pydantic’s JSON loading uses a huge amount of memory; here’s how to reduce it.

Essentially, slots are a more efficient in-memory representation for Python objects, where the list of possible attributes is fixed. This saves memory at the cost of disallowing adding extra attributes to an object, which in practice isn’t that common so it’s often a good tradeoff. ImplementationPeak memory usage (MB) Model.model_validate_json() 2000 ijson 1200 ijson+@dataclass(slots=True) 450This particular use case, of loading a large number of objects, may not be something Pydantic developers care about, or have the time to prioritize.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Memory

Memory

Photo of json

json

Related news:

News photo

OWC's New Sale Includes Big Discounts on Mac Docks, Memory, Accessories, and More

News photo

How (memory) safe is Zig? (2021)

News photo

In-Memory Ferroelectric Differentiator