Engineering 12 min read
Twice the Audio, Four Times the Memory in On-Device ASR
Cutting memory for on-device speech recognition: peak memory on 30 seconds of audio, from 880 MB down to 329 MB
ByPeng Shen Updated
On this page
- Something odd
- Memory isn't one number, it's a timeline
- Fix 1: Kill the quadratic growth
- Where the square comes from
- The fix
- How did it go?
- Side story: fused, but slower?
- Fix 2: The square is gone, the line is still there
- We almost blamed the wrong thing
- So who was it?
- Results
- Fix 3: The moment of loading
- One model, four copies in memory
- Taking them apart one by one
- Give back as you go
- Fix 4: Transcription is done, but memory won't come down
- How we measured
- Still on the to-do list
- Wrapping up
We run offline speech recognition on phones.
When people talk about on-device AI, the first question is usually "how fast is it?" But the thing that actually gets you paged at 2 a.m. is more often memory: a background service that the OS quietly kills, a low-end phone that crashes the moment it loads the model, or a process whose memory never comes back down after one long recording.
This post walks through what we did about memory. The biggest fix took peak memory on 30 s of audio from 880 MB to 329 MB. To be honest, it wasn't a grand, carefully planned overhaul. It was more like whack-a-mole: knock one problem down, and the next one pops up.
Something odd
Here's what we measured on a phone with a Dimensity 8300. Same model, audio of different lengths, and we tracked how much memory the process used at its peak:

At 5 and 10 seconds everything looks fine. At 30 seconds we're heading straight for 900 MB. The model itself is only about 70 MB, so using close to 1 GB to transcribe half a minute of speech? That simply doesn't fly on a phone.
The worst part: everyday testing never catches it. The clips we test with during development are mostly a few seconds long, memory looks perfectly healthy, and the problem only shows up when a long recording comes along.
Now look at how it grows. Subtract what the model itself takes, and going from 10 to 20 seconds makes the extra memory roughly 4 times bigger; 20 to 30 seconds keeps the same trend. In other words, the extra memory grows with the square of the audio length. That's where the title comes from.
Memory isn't one number, it's a timeline
Before changing anything, we broke "memory" down. A single transcription actually goes through several hurdles, and each one has its own cause and its own fix:
The moment of loading: the model is read from disk and rearranged into the format inference needs, which creates a short-lived spike;
While decoding: this depends on audio length, and it's where the "square curve" above lives;
After decoding: once transcription is done, does memory return to where it was?
For a speech service that lives in the background, none of these can be ignored: the load spike decides whether a low-end phone can start at all, the decode peak decides how long a recording you can handle, and whatever is left after decoding directly affects who the OS kills first when memory gets tight.

Let's go through them, biggest problem first.
Fix 1: Kill the quadratic growth
Where the square comes from
Our recognition model is a Zipformer, and inside it is a computation called "attention." Put simply, every audio frame scores itself against every other frame to see how related they are. That produces a frames × frames table of scores. Double the audio and you double the frames, so the table's area becomes 4 times larger.
That table alone wouldn't eat 880 MB, though. The real problem is that once the model is exported as a compute graph, attention gets split into a long chain of small steps: compute the scores, build position indices, gather by index, apply a mask, normalize… and every step writes out an equally large intermediate table. We counted roughly 10 of these tables per attention block, and the model has 16 attention blocks.
Do the math and you can see where the memory went.

The fix
The idea is pretty simple: all these intermediate tables are just by-products, so don't let them exist on their own.
We added a graph optimization at model-conversion time that recognizes the whole attention computation and fuses it into a single operator. The new operator works through the data in small blocks, one block at a time, so it never has to store every step's full intermediate table. The original graph also had an entire chain dedicated to "building position indices," and we cut that too: inside the new operator, positions are just pointer offsets.
All 16 attention blocks matched, and the graph shrank from 11,103 operators to 10,174.
How did it go?
Same Dimensity 8300 phone. On 20 seconds of audio, peak memory dropped 45.9%; on 30 seconds, it dropped 62.6%:

If you fit peak memory as a function of audio length, the quadratic coefficient dropped by 96.9%. The parabola that used to curve upward has been pulled nearly straight.

And a bonus we didn't plan for: with fewer intermediate tables, there's less data being shuffled around, so decoding 30 seconds of audio also got 15.5% faster.
Side story: fused, but slower?
A little embarrassing to admit, but the first version of this optimization actually made things 26.8% slower.
The reason was that the new operator initially processed one row at a time, and that memory access pattern was very unfriendly to the CPU cache. Once we switched to working in small blocks, the speed came back, and it ended up faster than before.
Since then we've had a rule: a fused operator has to pass a speed check, not just a memory check. Saving memory while losing speed still doesn't get merged.
Fix 2: The square is gone, the line is still there
With the quadratic term handled, what remained was mostly a "linear term": every extra second of audio added a few more MB.
We almost blamed the wrong thing
At first, looking at the curve fit, we concluded that fusion had made the linear term worse: its coefficient went from 0.54 MB per second to 5.07 MB per second. It really looked like a side effect of the new operator.
Then we added detailed logging to the memory allocator, printing exactly when every block was allocated and freed, and compared them one by one. It turned out that conclusion was wrong. This part of memory was allocated exactly the same way before and after fusion.
So why did it "look" worse? Because in the old curve the quadratic term was so large that it hid the linear term, and the fit couldn't tell the two apart. Once the quadratic term went away, the linear term that had been there all along finally showed itself.
That was a good reminder: curve fits can lie; finding the real cause takes direct evidence. We didn't delete the original conclusion from our optimization log. It's still there, clearly marked "corrected."
So who was it?
The logs pointed the linear growth at two places, neither of which had anything to do with attention:
Several big buffers alive at once. The front of the model has a chain of element-wise operations, and every step creates a new buffer as large as its input. The catch is that the inference engine has no notion of "in-place" computation: even when the previous input is about to become useless, it's only freed after the new buffer has been allocated. So at one moment you have to fit several big buffers at the same time, and they turned out to account for this entire part of the memory peak.
Intermediate results between submodules held until the very end. Inside the engine, the model is split into dozens of submodules, and the results passed between them are kept until the whole inference finishes, even when the next module finished with them long ago.
So we did two things:
For a small set of operators we checked one by one that "each output element depends only on the input element at the same position," and for those we let the output reuse the input buffer that's about to be freed;
We work out ahead of time who uses each intermediate result last, and free it right after that.
Results
Both of these were measured on x86 by running the encoder on its own. Measured as "how much more memory each extra second of audio costs," the memory pool's growth rate fell from 6.17 MB per second to 3.65 MB per second, about 41% less. The peak on 30 seconds of audio went from 329.3 MB to 299.0 MB, then to 257.3 MB.
Speed changed by less than measurement noise in both cases, so we consider it unaffected.
Fix 3: The moment of loading
For the far more common short utterances, the highest memory peak isn't during decoding at all. It's the instant the model loads.
One model, four copies in memory
We looked at loading with a memory profiler and found that at the peak, four copies of the encoder model were sitting in memory at the same time. Several problems were stacked on top of each other:
The file was read without allocating the right size up front, growing as it went. A 70 MB file ended up occupying 128 MB;
To figure out "what kind of model is this," the program loaded the entire model once, and that copy was never freed. It was still around when the real load began;
After the model's raw data had been rearranged into the inference format, it was only freed when the whole loading process finished.
Taking them apart one by one
Get the file size first and read it in one go; free the "what kind of model" load right after use. The load peak went from 450 MB to 290 MB;
We gave the inference engine a "metadata-only read" so checking the model type no longer requires a full load, and we switched to memory-mapping the model file, which saves another full copy. The peak dropped further to 223 MB.
Together (Dimensity 8300): the load peak was cut in half, and load time went from 740 ms to 173 ms, 77% less. Honestly, faster loading wasn't part of the plan. It came for free with fewer copies.
Give back as you go
Later, on a Snapdragon 778G test phone, we went one step further. The model weights are rearranged one by one in order, so the original file pages for the part that's already done can go back to the system right away. There's no need to wait for loading to finish.
The load peak went from 200 MB to 137 MB, and the extra memory taken during loading dropped from 63.5 MB to 0.23 MB. It's basically gone.
That number may not look as impressive as the others, but it hits the most common case. Speech usually goes through VAD (voice activity detection) first and gets cut into short segments, mostly around 5 seconds and at most 20. For audio like that, the load moment is the highest point of the whole process. So this change alone cut the process peak on 5 seconds of audio by about 20%.

Fix 4: Transcription is done, but memory won't come down
The last problem hides after decoding finishes.
The memory pool the inference engine uses for intermediate results only grows, never shrinks: it expands to the biggest demand it has ever seen and never gives anything back. So once the process has handled one 30-second recording, memory stays parked at that high-water mark, even if afterwards it only transcribes a few seconds at a time or just sits idle.
For a background service this number really matters. When Android runs low on memory, it decides who to kill based on how much memory a process is using right now, not on its historical peak.
Our fix: after every decode, actively hand that pool back, and let the engine allocate it again on the next inference. Here's how it looks on a Snapdragon 778G:

Without releasing, resident memory climbs with the longest recording seen so far; with releasing, it barely moves.
Of course, there's no free lunch. The next inference has to get that memory back, which adds about 2.5% latency. We measured the cost to be proportional to the amount released, roughly 0.27 ms per MB. So the feature is off by default, and whoever deploys it decides based on the scenario: for a background service that's called only now and then, it's worth turning on; for continuous, high-frequency transcription, it isn't.
How we measured
Memory optimizations are easy to "measure into looking good," so the method deserves a few words of its own:
Change one variable at a time. For A/B comparisons we used the same program binary and swapped only the inference engine's library, ruling out any other build differences;
Alternate the runs. We ran in a "baseline, new, new, baseline" order and started a fresh process every time, to cancel out drift such as the device heating up or background tasks;
Track three kinds of memory separately: load peak, process peak, and resident memory after decoding. Each carries a different risk, and lumping them together into "memory went down by X" doesn't really mean anything;
The transcript must not change. Every optimization is checked against the recognized text. If the text changes, it's a failure, no matter how much memory it saves;
Be honest about what didn't improve. Every entry in our optimization log spells out which metrics stayed the same. For example, the loading fixes did nothing for everyday resident memory, and attention fusion did nothing for the loading phase. That way nobody mistakes a local gain for an overall one.
We hit a few traps along the way. On one test phone, every new process randomly landed in one of two CPU scheduling modes, fast or slow, and the two differed by 1.3×. Yet within a single process, 20 consecutive runs were rock-steady, so you'd never notice from one run. Without alternating runs and launching many processes, we could easily have mistaken the scheduling difference for an optimization win.
Still on the to-do list
There are a few known gaps left on the memory front:
The fused attention operator doesn't support fp16 yet and doesn't use SIMD instructions yet;
If the model is loaded from Android's packaged resources (APK assets), memory mapping isn't available, so most of the loading gains don't apply;
Releasing the pool after decoding currently happens every single time. Smarter triggers, based on idle time or a memory high-water mark, haven't been evaluated yet.
Wrapping up
Looking back, the fixes land on three points in time:
While decoding: killed the quadratic growth, taking the peak on 30 seconds of audio from 880 MB to 329 MB; then squeezed the linear growth, cutting the per-second increase by about 41%;
While loading: removed duplicate copies and gave memory back as we went, halving the load peak and cutting load time by 77%;
After decoding: released the memory pool, cutting resident memory after a 30-second recording by nearly 38%.
If you take away just one thing, make it this: don't test memory with short clips only. Quadratic growth is almost invisible on short audio, and yet it's exactly the thing most likely to take down an on-device service.
Related product
Related articles
Engineering
Non-Real-Time, Low-Power Transcription on MediaTek Phones
Low-power transcription on MediaTek phones: SCP capture and VAD plus batched recognition let the CPU sleep. In a 30-hour test, battery fell 15 points vs 31.
Engineering
One Earbud Each. When Does the Translation Start?
VoxLingo started playback in about 1.8 seconds in a controlled English–Chinese test. See where the time went and how each reply reaches the right ear.
Engineering
From "AI Writes Code" to "AI Delivers"
Coding agents write code; delivery still needs humans. My open-source Orca workflow: 75% fewer feedback rounds, half the serious defects, ~5× the tokens.