#903·automerge

Memory leak with `initSyncState`

Author: jnthnCreated Apr 19, 2024Updated Aug 28, 2026
Labelsperformance

First of all, thanks for Automerge! I recently implemented use of SyncState in order to sync over a WebSocket. Shortly after deploying that, I started observing growing memory usage, and eventual SIGKILL due to out of memory on the tab containing the web application in question.

Investigating a bit, I observed that a load balancer was closing the WebSocket after around 30s, which we handled by opening a new connection producing a fresh SyncState for all documents we were syncing. I figured I'd managed to leak the socket or handlers or some such, but best I can follow the heap shapshot, that isn't the case, and it looks like the memory is tied up in WASM memory arrays.

To take the browser and all of our application out of the equation, I tried this:

import * as Automerge from "@automerge/automerge";

interface Something {
    test: string
}

let doc = Automerge.init<Something>();
doc = Automerge.change(doc, d => d.test = 'ing');

while (true) {
    Automerge.initSyncState();
}

And it does indeed slowly leak. I'd say too slowly to explain all of the leak we're seeing the full application, but the Node.js process grows at a rate of around 1MB/s.

I observed the leak in version 2.1.10, and just tried 2.1.13 and the 2.2.0 RC and they also exhibit it.