Skip to content
Latchkey

Python "MemoryError" During Tests in CI

A test run exhausted the runner’s memory and Python raised MemoryError (or the process was OOM-killed). Either a single test allocates too much, or memory accumulates across the suite.

What this error means

Tests fail with MemoryError, or the run is killed abruptly with no traceback (the OOM killer). It often appears only in CI, where the runner has less RAM than a developer machine.

pytest output
tests/test_pipeline.py::test_large_join
    result = pd.concat(frames)  # accumulates every frame
MemoryError

Common causes

A single test allocates too much

Loading a large dataset, building a huge structure, or concatenating many frames in one test exceeds the runner’s memory.

Memory accumulates across tests

A module-scoped fixture or a global cache that grows per test leaks memory through the suite until it runs out.

How to fix it

Shrink fixtures and free memory

Use small representative fixtures and release large objects between tests.

Python
@pytest.fixture
def frame():
    df = make_small_frame()   # not the full production dataset
    yield df
    del df                    # release before the next test

Isolate or right-size the run

  1. Run heavy tests in a separate, higher-memory job if the size is genuine.
  2. Use function-scoped fixtures so large objects are not retained.
  3. Reduce -n (xdist workers) - each worker holds its own memory.

How to prevent it

  • Keep test fixtures small and representative, not production-scale.
  • Free large objects between tests; prefer function-scoped fixtures.
  • Right-size the runner (or worker count) for genuinely heavy suites.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →