---
title: "Introducing Immortal Objects for Python"
description: "Instagram's pre-fork Django servers were losing shared memory to copy-on-write because CPython mutates reference counts and GC headers even on effectively read-only objects."
url: "https://swe.quest/articles/introducing-immortal-objects-for-python-dcfddb697b3a74fe"
canonical: "https://engineering.fb.com/2023/08/15/developer-tools/immortal-objects-for-python-instagram-meta/"
source: "Meta Engineering"
published: "2023-08-15"
difficulty: "medium"
kind: "Case study"
reading_minutes: 4
tags: ["python","memory-management","runtimes","concurrency"]
---

# Introducing Immortal Objects for Python

A post on [Meta Engineering](https://swe.quest/sources/meta.md), published 2023-08-15, 4 min read, Case study. Original: [https://engineering.fb.com/2023/08/15/developer-tools/immortal-objects-for-python-instagram-meta/](https://engineering.fb.com/2023/08/15/developer-tools/immortal-objects-for-python-instagram-meta/)

## Summary

Instagram's pre-fork Django servers were losing shared memory to copy-on-write because CPython mutates reference counts and GC headers even on effectively read-only objects. PEP-683 Immortal Objects marks a special refcount value so the runtime never modifies an object's refcount or GC header, which keeps those pages shared and lets objects be shared across threads without the GIL. Upstreaming required preserving backward compatibility across platforms, compilers, word sizes and endianness, and careful register allocation held the refcount-path check to about a 2% regression. The change is a building block for PEP-684 (per-interpreter GIL) and PEP-703 (optional GIL).

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Python](https://swe.quest/tags/python.md) (Languages)
- [Memory management](https://swe.quest/tags/memory-management.md) (Compilers & runtimes)
- [Runtimes & VMs](https://swe.quest/tags/runtimes.md) (Compilers & runtimes)
- [Concurrency](https://swe.quest/tags/concurrency.md) (Compilers & runtimes)

## Prerequisites

- reference counting
- copy-on-write and fork
- Python GIL ([easy posts on Python](https://swe.quest/tags/python.md?difficulty=easy))

## Keywords

PEP-683, Immortal Objects, CPython, Instagram, Django, PEP-703, PEP-684, GIL

## Related posts

- [Meta contributes new features to Python 3.12](https://swe.quest/articles/meta-contributes-new-features-to-python-3-12-9b216e044692b29d.md): Meta Engineering · 2023-10-05 · Medium · 6 min read · Python, Runtimes & VMs, Performance engineering, Compilers, Open source
  Meta upstreamed several features into CPython 3.12, many derived from its Cinder runtime: immortal objects (PEP 683) that skip reference counting to cut copy-on-write memory in forked Instagram workers, the @typing.override decorator (PEP 698), and inlined comprehensions (PEP 709) running up to 2x faster.
- [Using OxCaml to implement type-safe reference counting between OCaml and Python](https://swe.quest/articles/using-oxcaml-to-implement-type-safe-reference-counting-between-ocaml-and-python-6289aa53698f2fb6.md): Jane Street Tech Blog · 2026-06-15 · Hard · 12 min read · Functional programming, Memory management, Type systems, Python
  Wrapping Python objects as OCaml values in PyOCaml ties each object's release to the OCaml GC finalizer, so large data frames or GPU buffers can stay alive long after Python code drops them.
- [Why we rewrote WAL-G for Postgres backups in Rust: Meet WAL-RUS](https://swe.quest/articles/why-we-rewrote-wal-g-for-postgres-backups-in-rust-meet-wal-rus-a99cc2a7ede61ae3.md): ClickHouse · 2026-06-25 · Medium · 4 min read · PostgreSQL, Rust, Memory management, Storage systems, Go
  WAL-RUS is an open-source Rust rewrite of WAL-G's Postgres backup and WAL archival tooling, built because Go's garbage-collected runtime made virtual memory hard to predict in resource-constrained managed Postgres environments where overcommit is disabled.
- [chDB - A Rocket Engine on a Bicycle](https://swe.quest/articles/chdb-a-rocket-engine-on-a-bicycle-951148e84c12126b.md): ClickHouse · 2023-09-29 · Hard · 9 min read · Analytics & OLAP, Memory management, C & C++, Python, Performance engineering
  chDB embeds the ClickHouse engine as an in-process Python module instead of shelling out to the 500MB clickhouse-local binary, using WriteBufferFromVector, Python memoryview and Pybind11 for zero-copy result retrieval.
- [Understanding Garbage Collection in JavaScriptCore From Scratch](https://swe.quest/articles/understanding-garbage-collection-in-javascriptcore-from-scratch-6bc19cc2aad39444.md): WebKit · 2022-07-29 · Hard · 52 min read · Memory management, Runtimes & VMs, JavaScript, Concurrency
  JavaScriptCore's garbage collector is non-compacting, generational and mostly concurrent, with heavy use of lock-free programming.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
