A Call-Recognition Service on PyTorch Hit the 8 GB Memory Limit 452 Times Without Crashing

A speech-recognition service built on PyTorch ran for a month without a single failure, but over two days it hit its 8 GB limit 452 times. The Linux kernel had to reclaim memory from the container each time, even though no OOM kill occurred. The CPU build of the same code uses 2 GB, so suspicion falls on the cuDNN plan cache, which stores an entry for every new length of a speech fragment. Choose ML container limits based on cgroup counters, not on the absence of OOM kills.

AI-processed from Хабр: Машинное обучение; edited by Hamidun News
A Call-Recognition Service on PyTorch Hit the 8 GB Memory Limit 452 Times Without Crashing
Source: Хабр: Машинное обучение. Collage: Hamidun News.
Listen to article
Article Fact Sheet & Key Figures
ParameterDetails
Primary sourceХабр: Машинное обучение ↗
Editor-in-Chief
MALLOC_ARENA_MAX=2zero limit hits, speed 29× versus 30×
Key metric #2The CPU build of the same code holds 2.05–2.08 GB under any settings
Key metric #3A cuDNN plan weighs about 1.9 MB instead of the 200 KiB stated in the PyTorch documentation

A speech-recognition service built on PyTorch ran for a whole month without a single failure, and then it turned out that it had been living on the very edge of its allocated memory all along. The container was given 8 GB, the process used 7.5–7.

8 GB, and over two days the container hit that ceiling 452 times. Each hit meant the Linux kernel had to urgently reclaim memory from the container. It never got as far as an OOM kill, meaning the system forcibly terminating the process, but there was no headroom left.

Let's look at how the team hunted down the cause and why cuDNN, the Nvidia library that GPUs use to compute the convolutional layers of a neural network, comes into it.

What the service does and how the problem was noticed

The team builds a system for analyzing the company's phone calls. It runs on the customer's server next to the PBX. Recognition is handled by a separate Docker container: PyTorch 2.9.1, the GigaAM-v3 model, and the silero-VAD speech detector, which cuts the recording into fragments containing speech. The model turns those fragments into text.

One installation runs on ordinary office hardware: an Intel i5-12400F, an RTX 3060 with 12 GB of VRAM, and 16 GB of RAM. It processes about 300 calls a day, roughly ten hours of conversation. The problem was found by accident while the team was sizing memory for servers of different sizes.

They looked inside the container's cgroup v2 files. Anonymous memory, meaning memory allocated directly by the process, came to 7.5–7.8 GB. File cache was about 0.1 GB and swap was 35 MB. The most important figure was the memory.events counter: it grows every time the container hits its limit. There was almost no file cache, so part of the anonymous memory was being pushed into swap, and further growth would have led to an OOM kill.

Two hypotheses that did not settle the question

They first suspected the model weights might be sitting in the filesystem cache. That did not hold up: the cache was only 0.1 GB, and almost all of the memory was allocated directly by the process.

The second hypothesis was glibc malloc arenas. The process had 48 threads and 51 arenas of 64 MB each. To keep threads from contending for a single lock during allocation, glibc gives them separate arenas, up to eight per CPU core. Freed memory is returned to the system reluctantly: a single live block in the middle of a heap is enough to keep the whole region with the process. Multithreaded services that allocate and free buffers of varying sizes all day grow in exactly this way.

This is remedied with the MALLOC_ARENA_MAX environment variable, with no code changes required. The team set it to 2 and let it run for a day. Limit hits dropped to zero, and recognition speed did not change: 29× real time versus 30× before, within the margin of noise. But memory kept climbing and reached 6.6 GB over the day. Growth slowed but did not stop, so the arenas were only part of the problem.

A hint from the CPU build and the cuDNN suspicion

The decisive clue came from a simple comparison. The service has two builds, one for the GPU and one for the CPU, with identical recognition code. The CPU build was run locally, and its memory held at 2.05–2.08 GB under any allocator settings. If memory only grows in the CUDA build, the search has to focus on what exists only there.

The cuDNN plan cache came under suspicion. For each new convolution configuration (tensor shapes, data type, layout), the library selects an execution plan. That selection is expensive, so PyTorch caches the plans. The cache size is controlled by the TORCH_CUDNN_V8_API_LRU_CACHE_LIMIT variable, which defaults to 10,000 plans.

Now consider what goes into it. The speech detector splits calls at pauses, so fragments come in any length: 1.3 seconds, 4.7, 11.2. For a convolution, each new length means a new tensor shape, and therefore a new plan. Thousands of fragments pass through in a day, and nearly every one adds an entry to the cache.

The PyTorch documentation estimates a plan at about 200 KiB, which would put 10,000 plans at roughly 2 GiB. But the authors' rough estimate was that a plan actually took about 1.9 MB, nearly an order of magnitude more. At that size the default limit does not constrain anything: the cache can comfortably fill the entire 8 GB container.

What to take away from this

First, when choosing a memory limit for an ML container, look not at the absence of OOM kills but at memory.events and the anon/file ratio in memory.stat. A growing count of limit hits is an early signal that the headroom is running out.

Second, if the service is multithreaded and allocates heavily, MALLOC_ARENA_MAX=2 is a cheap measure that requires no code changes. Here it removed the limit hits without costing speed.

Third, if input data has variable length, as speech fragments do after the detector, look closely at the cuDNN cache and its limit. A simple check is to compare memory usage of the CPU and CUDA builds running the same code.

An honest caveat: in the part of the material covered here, the cuDNN suspicion rests on estimates and indirect evidence, and the actual effect of limiting the cache on a production installation has to be measured independently. Rolling back such changes is easiest with a local docker compose override, so the settings can be reverted with a single command. After each step, the team waited between three hours and a full day of live calls.

Story

Key Questions Answered

What the service does and how the problem was noticed?

The team builds a system for analyzing the company's phone calls. It runs on the customer's server next to the PBX. Recognition is handled by a separate Docker container: PyTorch 2.9.1, the GigaAM-v3 model, and the silero-VAD speech detector, which cuts the recording into fragments containing speech. The model turns those fragments into text. One installation runs on ordinary office hardware: an Intel i5-12400F, an RTX 3060 with 12 GB of VRAM, and 16 GB of RAM.

What are the key details on Two hypotheses that did not settle the question?

They first suspected the model weights might be sitting in the filesystem cache. That did not hold up: the cache was only 0.1 GB, and almost all of the memory was allocated directly by the process. The second hypothesis was glibc malloc arenas. The process had 48 threads and 51 arenas of 64 MB each. To keep threads from contending for a single lock during allocation, glibc gives them separate arenas, up to eight per CPU core.

What are the key details on A hint from the CPU build and the cuDNN suspicion?

The decisive clue came from a simple comparison. The service has two builds, one for the GPU and one for the CPU, with identical recognition code. The CPU build was run locally, and its memory held at 2.05–2.08 GB under any allocator settings. If memory only grows in the CUDA build, the search has to focus on what exists only there. The cuDNN plan cache came under suspicion. For each new convolution configuration (tensor shapes, data type, layout), the library selects an execution plan.

ZK
Zhemal Khamidun — CPO at Alpina Digital, product manager of AlpinaGPT. Building AI products since 2019. Hamidun News is a pet project that grew into a production aggregator with 6,000+ readers.
✦ Unified AI Workspace · Nomad

Test Frontier LLMs & Autonomous Agents in One Workspace

Run Claude Opus 5.5, GPT-6, Gemini 3.1, and DeepSeek side-by-side in Nomad AI (nomadnet.ai) — unified model routing, deep research, and multimodal workflows under one subscription.

Try Nomad AI →Launch Nomad Agent
What do you think?
Loading comments…