---
title: "Using OxCaml to implement type-safe reference counting between OCaml and Python"
description: "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."
url: "https://swe.quest/articles/using-oxcaml-to-implement-type-safe-reference-counting-between-ocaml-and-python-6289aa53698f2fb6"
canonical: "https://blog.janestreet.com/oxcaml-typesafe-reference-counting-python/"
source: "Jane Street Tech Blog"
published: "2026-06-15"
difficulty: "hard"
kind: "Deep dive"
reading_minutes: 12
tags: ["functional-programming","memory-management","type-systems","python"]
---

# Using OxCaml to implement type-safe reference counting between OCaml and Python

A post on [Jane Street Tech Blog](https://swe.quest/sources/jane-street.md), published 2026-06-15, 12 min read, Deep dive. Original: [https://blog.janestreet.com/oxcaml-typesafe-reference-counting-python/](https://blog.janestreet.com/oxcaml-typesafe-reference-counting-python/)

## Summary

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. OxCaml's modes fix this: \`@ local\` arguments let C glue borrow Python references without an incref, \`@ unique\` returns let it steal the reference back without one, and \`borrow\_\` keeps a value unique across local calls. For manual control, \`Py.newref\`, a uniqueness-consuming \`Py.decref\` and \`ppx\_release\` provide type-safe explicit reference counting that frees objects promptly even when exceptions are raised.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Functional programming](https://swe.quest/tags/functional-programming.md) (Languages)
- [Memory management](https://swe.quest/tags/memory-management.md) (Compilers & runtimes)
- [Type systems](https://swe.quest/tags/type-systems.md) (Compilers & runtimes)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- OCaml basics ([easy posts on Functional programming](https://swe.quest/tags/functional-programming.md?difficulty=easy))
- Python C extension API ([easy posts on Python](https://swe.quest/tags/python.md?difficulty=easy))
- reference counting
- garbage collection ([easy posts on Memory management](https://swe.quest/tags/memory-management.md?difficulty=easy))

## Keywords

OxCaml, OCaml, PyOCaml, CPython, ppx\_release, Jane Street

## Related posts

- [Oxidizing OCaml: Rust-Style Ownership](https://swe.quest/articles/oxidizing-ocaml-rust-style-ownership-5670c1001a0b9cc2.md): Jane Street Tech Blog · 2023-06-21 · Hard · 14 min read · Functional programming, Type systems, Language design, Memory management, Rust
  Jane Street's OCaml extensions add Rust-style ownership through mode axes.
- [Oxidizing OCaml: Locality](https://swe.quest/articles/oxidizing-ocaml-locality-b1f7bf98d1488c18.md): Jane Street Tech Blog · 2023-05-26 · Hard · 13 min read · Functional programming, Type systems, Memory management, Language design, Compilers
  Jane Street is extending OCaml with modes, starting with a locality mode that marks values as local (cannot escape their region) or global, so the compiler can stack-allocate short-lived values while keeping garbage collection everywhere else.
- [What the interns have wrought, 2025 edition](https://swe.quest/articles/what-the-interns-have-wrought-2025-edition-eda7f47e78fb3ca5.md): Jane Street Tech Blog · 2025-08-27 · Hard · 12 min read · Functional programming, Memory management, Query engines, Type systems, Performance engineering
  Jane Street's 2025 intern projects include an index-aware evaluator for JSQL, the company's internal SQL dialect, which over-approximates key ranges from WHERE clauses and specializes execution to Map, Hashtbl or multi-index containers, giving ~700x and ~30x speedups on 3.8M-row benchmarks.
- [Introducing Immortal Objects for Python](https://swe.quest/articles/introducing-immortal-objects-for-python-dcfddb697b3a74fe.md): Meta Engineering · 2023-08-15 · Medium · 4 min read · Python, Memory management, Runtimes & VMs, Concurrency
  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.
- [What the interns have wrought, 2022 edition](https://swe.quest/articles/what-the-interns-have-wrought-2022-edition-0f4dcf7e53f3e8d4.md): Jane Street Tech Blog · 2022-08-25 · Medium · 8 min read · Functional programming, Testing, Performance engineering, Developer tools, Engineering culture
  Three Jane Street intern projects from 2022 show substantial tooling work in OCaml. magic-trace gained a sampling mode that can cover 20 seconds to 20 minutes on Intel or AMD hardware, plus multi-process tracing, CPU-frequency transition reporting and a perf dlfilter backend that speeds trace decoding by 10–50%.

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)
