YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix: cap MLX GPU buffer cache to 2GB — reduces memory from 12GB to 3GB
Louis Beaumontc63270ced4e0
diff --git a/Cargo.lock b/Cargo.lockindex 562ba2177..d9d463fdf 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -6737,7 +6737,7 @@ dependencies = [[[package]]name = "screenpipe-audio"-version = "0.3.257"+version = "0.3.258"dependencies = ["anyhow","audiopipe",@@ -6797,7 +6797,7 @@ dependencies = [[[package]]name = "screenpipe-config"-version = "0.3.257"+version = "0.3.258"dependencies = ["dirs 6.0.0","serde",@@ -6810,7 +6810,7 @@ dependencies = [[[package]]name = "screenpipe-connect"-version = "0.3.257"+version = "0.3.258"dependencies = ["anyhow","async-trait",@@ -6839,7 +6839,7 @@ dependencies = [[[package]]name = "screenpipe-core"-version = "0.3.257"+version = "0.3.258"dependencies = ["accessibility","accessibility-sys",@@ -6879,12 +6879,13 @@ dependencies = ["uiautomation","uuid","which 6.0.3",+ "windows-sys 0.59.0","zeroize",][[package]]name = "screenpipe-db"-version = "0.3.257"+version = "0.3.258"dependencies = ["anyhow","chrono",@@ -6910,7 +6911,7 @@ dependencies = [[[package]]name = "screenpipe-engine"-version = "0.3.257"+version = "0.3.258"dependencies = ["anyhow","async-trait",@@ -6980,7 +6981,7 @@ dependencies = [[[package]]name = "screenpipe-events"-version = "0.3.257"+version = "0.3.258"dependencies = ["anyhow","chrono",@@ -6998,7 +6999,7 @@ dependencies = [[[package]]name = "screenpipe-screen"-version = "0.3.257"+version = "0.3.258"dependencies = ["accessibility-sys","anyhow",@@ -7038,7 +7039,7 @@ dependencies = [[[package]]name = "screenpipe-vault"-version = "0.3.257"+version = "0.3.258"dependencies = ["argon2","chacha20poly1305",diff --git a/Cargo.toml b/Cargo.tomlindex f8c81f728..11627b11b 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -6,7 +6,7 @@ exclude = ["apps/screenpipe-app-tauri/src-tauri", "crates/screenpipe-integrationresolver = "2"[workspace.package]-version = "0.3.258"+version = "0.3.259"authors = ["louis030195 <[email protected]>"]description = ""repository = "https://github.com/screenpipe/screenpipe"diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml b/apps/screenpipe-app-tauri/src-tauri/Cargo.tomlindex 245672130..1580df522 100644--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.toml@@ -1,6 +1,6 @@[package]name = "screenpipe-app"-version = "2.3.21"+version = "2.3.22"description = "24/7 memory for your desktop. 100% local."authors = ["you"]license = ""diff --git a/crates/screenpipe-audio/src/transcription/engine.rs b/crates/screenpipe-audio/src/transcription/engine.rsindex d05811a21..d09964ecb 100644--- a/crates/screenpipe-audio/src/transcription/engine.rs+++ b/crates/screenpipe-audio/src/transcription/engine.rs@@ -19,6 +19,51 @@ use std::sync::Mutex as StdMutex;use tracing::{error, info, warn};use whisper_rs::{WhisperContext, WhisperState};+/// MLX Metal memory management — cap the GPU buffer cache to prevent unbounded growth.+/// MLX's caching allocator keeps freed GPU buffers for reuse; without a limit the+/// cache grows to 10+ GB over hours of transcription on a 0.6B model.+#[cfg(feature = "parakeet-mlx")]+mod mlx_memory {+ #[allow(dead_code)]+ extern "C" {+ pub fn mlx_set_cache_limit(res: *mut usize, limit: usize) -> std::ffi::c_int;+ pub fn mlx_get_active_memory(res: *mut usize) -> std::ffi::c_int;+ pub fn mlx_get_cache_memory(res: *mut usize) -> std::ffi::c_int;+ pub fn mlx_get_peak_memory(res: *mut usize) -> std::ffi::c_int;+ pub fn mlx_clear_cache() -> std::ffi::c_int;+ }++ /// Set MLX buffer cache limit. Returns the previous limit.+ pub fn set_cache_limit(limit_bytes: usize) -> usize {+ let mut prev: usize = 0;+ unsafe { mlx_set_cache_limit(&mut prev, limit_bytes) };+ prev+ }++ /// Clear all cached (unused) MLX Metal buffers.+ #[allow(dead_code)]+ pub fn clear_cache() {+ unsafe { mlx_clear_cache() };+ }++ /// Log current MLX memory stats.+ pub fn log_memory_stats(label: &str) {+ let (mut active, mut cache, mut peak) = (0usize, 0usize, 0usize);+ unsafe {+ mlx_get_active_memory(&mut active);+ mlx_get_cache_memory(&mut cache);+ mlx_get_peak_memory(&mut peak);+ }+ tracing::info!(+ "mlx memory [{}]: active={:.1}MB, cache={:.1}MB, peak={:.1}MB",+ label,+ active as f64 / 1048576.0,+ cache as f64 / 1048576.0,+ peak as f64 / 1048576.0,+ );+ }+}+/// Unified transcription engine that owns the runtime state for whatever backend is configured./// Only the selected model is loaded — no dummy Whisper downloads for non-Whisper engines.#[derive(Clone)]@@ -146,7 +191,17 @@ impl TranscriptionEngine {.map_err(|e| anyhow!("parakeet-mlx model loading task panicked: {}", e))?;match load_result {Ok(model) => {- info!("parakeet-tdt-0.6b-v3-mlx (GPU) model loaded successfully");+ // Cap MLX buffer cache to 2 GB — prevents the caching allocator+ // from accumulating 10+ GB of GPU memory over time.+ // Model weights (~1.2 GB) are active memory, not cache.+ const MLX_CACHE_LIMIT: usize = 2 * 1024 * 1024 * 1024;+ let prev = mlx_memory::set_cache_limit(MLX_CACHE_LIMIT);+ info!(+ "parakeet-tdt-0.6b-v3-mlx (GPU) model loaded successfully, \+ mlx cache limit set to 2GB (was {}MB)",+ prev / 1048576+ );+ mlx_memory::log_memory_stats("after model load");Ok(Self::ParakeetMlx {model: Arc::new(StdMutex::new(model)),vocabulary,@@ -207,7 +262,14 @@ impl TranscriptionEngine {.map_err(|e| anyhow!("parakeet-mlx model loading task panicked: {}", e))?;match load_result {Ok(model) => {- info!("parakeet-tdt-0.6b-v3-mlx (GPU) model loaded successfully");+ const MLX_CACHE_LIMIT: usize = 2 * 1024 * 1024 * 1024;+ let prev = mlx_memory::set_cache_limit(MLX_CACHE_LIMIT);+ info!(+ "parakeet-tdt-0.6b-v3-mlx (GPU) model loaded successfully, \+ mlx cache limit set to 2GB (was {}MB)",+ prev / 1048576+ );+ mlx_memory::log_memory_stats("after model load");Ok(Self::ParakeetMlx {model: Arc::new(StdMutex::new(model)),vocabulary,