add clean memory

This commit is contained in:
Julian Freeman
2026-03-03 22:03:36 -04:00
parent 0c77149c9e
commit 92adfd7a2e
5 changed files with 501 additions and 16 deletions

View File

@@ -492,3 +492,77 @@ pub async fn run_browser_clean(browser: BrowserType, profile_paths: Vec<String>)
fail_count,
})
}
// --- 内存清理逻辑 ---
use sysinfo::{System, ProcessesToUpdate};
#[derive(Serialize, Clone)]
pub struct MemoryStats {
pub total: u64,
pub used: u64,
pub free: u64,
pub percent: f32,
}
/// 获取当前系统内存状态
pub fn get_memory_stats() -> MemoryStats {
let mut sys = System::new_all();
sys.refresh_memory();
let total = sys.total_memory();
let used = sys.used_memory();
let free = total.saturating_sub(used);
let percent = (used as f32 / total as f32) * 100.0;
MemoryStats { total, used, free, percent }
}
/// 执行内存压缩 (Empty Working Set)
pub async fn run_memory_clean() -> Result<u64, String> {
use windows_sys::Win32::System::ProcessStatus::EmptyWorkingSet;
use windows_sys::Win32::System::Threading::{OpenProcess, PROCESS_QUERY_INFORMATION, PROCESS_SET_QUOTA};
use windows_sys::Win32::Foundation::CloseHandle;
let before = get_memory_stats().used;
let mut sys = System::new_all();
sys.refresh_processes(ProcessesToUpdate::All, true);
for (pid, _) in sys.processes() {
let pid_u32 = pid.as_u32();
unsafe {
let handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_SET_QUOTA, 0, pid_u32);
if handle != std::ptr::null_mut() {
EmptyWorkingSet(handle);
CloseHandle(handle);
}
}
}
// 给系统一点点时间反应
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
let after = get_memory_stats().used;
let freed = before.saturating_sub(after);
Ok(freed)
}
/// 深度内存清理 (Standby List / System Cache)
pub async fn run_deep_memory_clean() -> Result<u64, String> {
use windows_sys::Win32::System::Memory::SetSystemFileCacheSize;
let before = get_memory_stats().used;
unsafe {
// -1 (usize::MAX) 表示清空系统文件缓存
SetSystemFileCacheSize(usize::MAX, usize::MAX, 0);
}
// 配合普通清理一起执行
let _ = run_memory_clean().await;
let after = get_memory_stats().used;
let freed = before.saturating_sub(after);
Ok(freed)
}

View File

@@ -60,6 +60,23 @@ async fn start_browser_clean(browser: String, profiles: Vec<String>) -> Result<c
cleaner::run_browser_clean(b_type, profiles).await
}
// --- 内存清理命令 ---
#[tauri::command]
async fn get_memory_stats() -> cleaner::MemoryStats {
cleaner::get_memory_stats()
}
#[tauri::command]
async fn run_memory_clean() -> Result<u64, String> {
cleaner::run_memory_clean().await
}
#[tauri::command]
async fn run_deep_memory_clean() -> Result<u64, String> {
cleaner::run_deep_memory_clean().await
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
@@ -78,7 +95,10 @@ pub fn run() {
clean_thumbnails,
disable_hibernation,
start_browser_scan,
start_browser_clean
start_browser_clean,
get_memory_stats,
run_memory_clean,
run_deep_memory_clean
])
.run(tauri::generate_context!())
.expect("error while running tauri application");