VOOZH about

URL: https://thenewstack.io/meta-solves-pythons-problem-of-the-not-so-immutable-objects/

⇱ Meta Solves Python's Problem of the Not-so-Immutable Objects - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2023-09-27 08:20:27
Meta Solves Python's Problem of the Not-so-Immutable Objects
Python

Meta Solves Python’s Problem of the Not-so-Immutable Objects

By developing Immortal Objects, Meta’s engineers increased shared memory usage and decreased private memory usage though reducing copy-on-writes.
Sep 27th, 2023 8:20am by Jessica Wachtel
👁 Featued image for: Meta Solves Python’s Problem of the Not-so-Immutable Objects

Cue “The more you know” graphic: Python objects are not immutable. In fact, all Python objects contain a small amount of mutable state. And this can turn out to be bad news for memory usage.

This problem is at the core of how the Python runtime works and one of the reasons why the Global Interpreter Lock (GIL) is important. This problem has a large negative impact on CPU and memory performance, especially for approaches to Python’s scalability, because it impacts how objects scale in memory.

A recent blog post written by Eddie Elizondo details how and why social networking conglomerate Meta created Immortal Objects, PEP-683 for Python.

Meta’s engineers wanted to improve the memory and CPU efficiency of handling their requests. The frontend server was Python (Django). They achieved parallelism with a multiprocess architecture paired with asynchio for per-process concurrency. To further mitigate memory inefficiencies, Meta engineers prefer shared memory over private memory whenever possible.

To prioritize shared memory, developers lean on a pre-fork web server architecture to cache as many objects as possible. Separate processes use these caches as read-only, structured through shared memory. Resource metrics show this method was helpful, but private memory usage still grew over time while shared memory usage decreased.

👁 Image

Uh-oh!

The Problem with Not-so-Immutable Objects

Python and other programming languages use reference counting as a memory management approach. The reference count is a piece of state on the object that’s mutable by the runtime. The reference count is incremented whenever the object is referenced and decremented when dereferenced. When the reference count is 0, the object’s memory is deallocated (garbage collection).

Meta’s engineers analyzed the Python heap and found that most of their Python objects lived throughout the entire runtime, but the runtime still modified the reference counts and garbage collection (GC) operations on every read and GC cycle. This triggered a copy-on-write in the server process and up, up, up goes the private memory.

Immortal Objects for Python

Immortal Objects are objects with unchanging core object state. A special value in the object’s reference count field lets the runtime know when it can and can’t mutate both the referenced count fields and GC header. Immortal objects bypass reference count checks and live throughout the entire runtime execution and remove the need for GIL safety.

👁 Image

Standard objects versus Immortal Objects.

Implementing and releasing Immortal Objects within Instagram was a relatively straightforward process but this was not the case with the general release.

Challenges with Immortal Objects

Implementing Immortal Objects brought a few obstacles such as performance degradation, backward compatibility, and platform compatibility. Immortal objects cause a performance degradation. The core implementation relies on adding explicit checks in the reference count increment and decrement routines. This led to a performance degradation in the service. The smart use of register allocations managed to keep this to around a 2% regression.

Another large implementation hurdle was making sure applications didn’t crash after changing reference count implementations. Meta also made sure Immutable Objects works across all the different platforms, compilers, architectures, and hardware types.

Immortal Objects Reduced Private Memory Use

By developing Immortal Objects, Meta’s engineers increased shared memory usage and decreased private memory usage though reducing copy-on-writes.

👁 Image

But wait, there’s more! Now there’s true immutability of objects on the heap.

Before immutable objects, both the GC and reference count mechanism had unrestricted access to both fields. The contribution of Immortal Objects to Python means these objects can be shared across threads without needing the safety provided by the GIL. This is another step toward a multicore Python run time.

TRENDING STORIES
Jessica Wachtel is a developer marketing writer at InfluxData where she creates content that helps make the world of time series data more understandable and accessible. Jessica has a background in software development and technical journalism.
Read more from Jessica Wachtel
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.