fix(local-asr): 切走/删模型/手动释放时真正归还 RAM#270
Merged
H-Chris233 merged 1 commit intomainfrom May 5, 2026
Merged
Conversation
三个问题合修: 1. 用户点"释放"按钮后活动监视器看 RSS 不下降。原因:encoder f32 weights 是 malloc 出来的几百 MB,free 后 macOS libmalloc 不会立即把页归还内核。 修:drop QwenAsrEngine 后调 malloc_zone_pressure_relief(NULL, 0),主动 归还。decoder bf16 走 mmap 不依赖这个。 2. 切回云端 ASR 后本地引擎仍驻留。set_active_asr_provider 切到非 local 时未释放,1.2GB+ RAM 一直留到进程退出。 3. 删除已下载的模型时未释放内存里加载的同 id 引擎,留下 mmap 指向已 unlink 文件 + RAM 不归还的双重问题。 Windows 端本期不上线(cache.rs macOS cfg 块包住改动)。
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
Summary
修复"按了释放按钮内存没掉"以及一并查出的两个相关泄漏点。三处都是同一主题:本地 Qwen3-ASR engine 该走时却没真走。
1. macOS 主动归还内核(用户报告主因)
按"释放"按钮后
Arc<QwenAsrEngine>已经 drop、qwen_free也跑完了,但活动监视器看 RSS 几乎不动。原因:encoder weights 是 f32 malloc 出来的几百 MB,free()后 macOS libmalloc 把页留在 freelist 上不归还内核。decoder bf16 走 mmap,munmap 已立即生效,所以会"掉一部分但不掉大头"。修:在
LocalAsrCache::release_now/release_if_idle真 drop 完 engine 后调malloc_zone_pressure_relief(NULL, 0),让 libmalloc 主动归还。2. 切回云端时本地引擎不释放
set_active_asr_provider当前只在切到 local 时预加载,切回云端时没有镜像处理。结果:用户切走后 1.2GB+ RAM 一直留到进程退出,下次再走 dictation 是云端路径,根本不会触发schedule_local_asr_release。修:切到非
local-qwen3时coord.release_local_asr_engine()。3. 删模型时不释放内存里同 id 引擎
local_asr_delete_model直接delete_model(id)删盘,不看内存里有没有加载这个 id。删完后 mmap 还指着已 unlink 的文件,RAM 还在。修:
local_asr_delete_model注入coord,删之前若loaded_model_id == 要删的 id先 release。平台范围
Windows 端本地 ASR 本期不上线(issue #256),改动全部在
#[cfg(target_os = "macos")]块内或只影响 macOS 唯一持有引擎的代码路径。Test plan
release engine ... on demand+malloc_zone_pressure_relief freed ~N bytes,活动监视器 RSS 立即下降几百 MB(含 encoder malloc 部分)schedule_local_asr_release行为不变)cargo check --manifest-path src-tauri/Cargo.toml通过PR Type
Bug fix
Description
Manually release engine now reclaims memory on macOS
malloc_zone_pressure_reliefafter engine drop to return pages to kernelSwitching ASR provider frees local engine immediately
set_active_asr_providernow callsrelease_local_asr_enginewhen selecting cloud providersDeleting model safely releases engine before file removal
local_asr_delete_modelreceives coordinator, releases engine if loaded model matches the one being deletedDiagram Walkthrough
File Walkthrough
cache.rs
macOS memory pressure relief after ASR engine dropopenless-all/app/src-tauri/src/asr/local/cache.rs
pressure_relief_macos()to force macOS malloc to return freedpages to the kernel
concurrency
commands.rs
Release engine on ASR provider switch and model deletionopenless-all/app/src-tauri/src/commands.rs
set_active_asr_provider: release local engine when switching to anon-local provider
local_asr_delete_model: accept coordinator, release engine if loadedmodel is targeted for deletion