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

121
src-tauri/Cargo.lock generated
View File

@@ -2112,6 +2112,15 @@ version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
[[package]]
name = "ntapi"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3b335231dfd352ffb0f8017f3b6027a4917f7df785ea2143d8af2adc66980ae"
dependencies = [
"winapi",
]
[[package]]
name = "num-conv"
version = "0.2.0"
@@ -3491,6 +3500,20 @@ dependencies = [
"syn 2.0.117",
]
[[package]]
name = "sysinfo"
version = "0.33.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fc858248ea01b66f19d8e8a6d55f41deaf91e9d495246fd01368d99935c6c01"
dependencies = [
"core-foundation-sys",
"libc",
"memchr",
"ntapi",
"rayon",
"windows 0.57.0",
]
[[package]]
name = "system-deps"
version = "6.2.2"
@@ -3538,7 +3561,7 @@ dependencies = [
"tao-macros",
"unicode-segmentation",
"url",
"windows",
"windows 0.61.3",
"windows-core 0.61.2",
"windows-version",
"x11-dl",
@@ -3609,7 +3632,7 @@ dependencies = [
"webkit2gtk",
"webview2-com",
"window-vibrancy",
"windows",
"windows 0.61.3",
]
[[package]]
@@ -3710,7 +3733,7 @@ dependencies = [
"tauri-plugin",
"thiserror 2.0.18",
"url",
"windows",
"windows 0.61.3",
"zbus",
]
@@ -3736,7 +3759,7 @@ dependencies = [
"url",
"webkit2gtk",
"webview2-com",
"windows",
"windows 0.61.3",
]
[[package]]
@@ -3762,7 +3785,7 @@ dependencies = [
"url",
"webkit2gtk",
"webview2-com",
"windows",
"windows 0.61.3",
"wry",
]
@@ -3929,11 +3952,25 @@ dependencies = [
"bytes",
"libc",
"mio",
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"tokio-macros",
"windows-sys 0.61.2",
]
[[package]]
name = "tokio-macros"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c55a2eff8b69ce66c84f85e1da1c233edc36ceb85a2058d11b0d6a3c7e7569c"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.117",
]
[[package]]
name = "tokio-util"
version = "0.7.18"
@@ -4527,10 +4564,10 @@ checksum = "7130243a7a5b33c54a444e54842e6a9e133de08b5ad7b5861cd8ed9a6a5bc96a"
dependencies = [
"webview2-com-macros",
"webview2-com-sys",
"windows",
"windows 0.61.3",
"windows-core 0.61.2",
"windows-implement",
"windows-interface",
"windows-implement 0.60.2",
"windows-interface 0.59.3",
]
[[package]]
@@ -4551,7 +4588,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "381336cfffd772377d291702245447a5251a2ffa5bad679c99e61bc48bacbf9c"
dependencies = [
"thiserror 2.0.18",
"windows",
"windows 0.61.3",
"windows-core 0.61.2",
]
@@ -4563,10 +4600,13 @@ dependencies = [
"jwalk",
"serde",
"serde_json",
"sysinfo",
"tauri",
"tauri-build",
"tauri-plugin-opener",
"tokio",
"walkdir",
"windows-sys 0.59.0",
]
[[package]]
@@ -4615,6 +4655,16 @@ dependencies = [
"windows-version",
]
[[package]]
name = "windows"
version = "0.57.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12342cb4d8e3b046f3d80effd474a7a02447231330ef77d71daa6fbc40681143"
dependencies = [
"windows-core 0.57.0",
"windows-targets 0.52.6",
]
[[package]]
name = "windows"
version = "0.61.3"
@@ -4637,14 +4687,26 @@ dependencies = [
"windows-core 0.61.2",
]
[[package]]
name = "windows-core"
version = "0.57.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2ed2439a290666cd67ecce2b0ffaad89c2a56b976b736e6ece670297897832d"
dependencies = [
"windows-implement 0.57.0",
"windows-interface 0.57.0",
"windows-result 0.1.2",
"windows-targets 0.52.6",
]
[[package]]
name = "windows-core"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0fdd3ddb90610c7638aa2b3a3ab2904fb9e5cdbecc643ddb3647212781c4ae3"
dependencies = [
"windows-implement",
"windows-interface",
"windows-implement 0.60.2",
"windows-interface 0.59.3",
"windows-link 0.1.3",
"windows-result 0.3.4",
"windows-strings 0.4.2",
@@ -4656,8 +4718,8 @@ version = "0.62.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb"
dependencies = [
"windows-implement",
"windows-interface",
"windows-implement 0.60.2",
"windows-interface 0.59.3",
"windows-link 0.2.1",
"windows-result 0.4.1",
"windows-strings 0.5.1",
@@ -4674,6 +4736,17 @@ dependencies = [
"windows-threading",
]
[[package]]
name = "windows-implement"
version = "0.57.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9107ddc059d5b6fbfbffdfa7a7fe3e22a226def0b2608f72e9d552763d3e1ad7"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.117",
]
[[package]]
name = "windows-implement"
version = "0.60.2"
@@ -4685,6 +4758,17 @@ dependencies = [
"syn 2.0.117",
]
[[package]]
name = "windows-interface"
version = "0.57.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29bee4b38ea3cde66011baa44dba677c432a78593e202392d1e9070cf2a7fca7"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.117",
]
[[package]]
name = "windows-interface"
version = "0.59.3"
@@ -4718,6 +4802,15 @@ dependencies = [
"windows-link 0.1.3",
]
[[package]]
name = "windows-result"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8"
dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-result"
version = "0.3.4"
@@ -5155,7 +5248,7 @@ dependencies = [
"webkit2gtk",
"webkit2gtk-sys",
"webview2-com",
"windows",
"windows 0.61.3",
"windows-core 0.61.2",
"windows-version",
"x11-dl",

View File

@@ -25,4 +25,14 @@ serde_json = "1"
chrono = "0.4.44"
walkdir = "2.5.0"
jwalk = "0.8.1"
sysinfo = "0.33.1"
tokio = { version = "1", features = ["full"] }
windows-sys = { version = "0.59", features = [
"Win32_System_Memory",
"Win32_Foundation",
"Win32_System_ProcessStatus",
"Win32_System_Threading",
"Win32_Security",
"Win32_System_SystemInformation"
] }

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");

View File

@@ -23,6 +23,13 @@ interface FileNode {
level: number; isOpen: boolean; isLoading: boolean;
}
interface MemoryStats {
total: number;
used: number;
free: number;
percent: number;
}
// --- 状态管理 ---
const fastState = ref({
isScanning: false,
@@ -33,6 +40,13 @@ const fastState = ref({
cleanResult: null as CleanResult | null,
});
const memoryState = ref({
stats: null as MemoryStats | null,
isCleaning: false,
lastFreed: "",
isDone: false,
});
const chromeState = ref({
isScanning: false,
isCleaning: false,
@@ -349,6 +363,55 @@ function splitSize(sizeStr: string | number) {
}
return { value: str, unit: '' };
}
// --- 内存清理逻辑 ---
async function getMemoryStats() {
try {
const stats = await invoke<MemoryStats>("get_memory_stats");
memoryState.value.stats = stats;
} catch (err) {
console.error("Failed to fetch memory stats", err);
}
}
async function startMemoryClean(deep = false) {
if (memoryState.value.isCleaning) return;
memoryState.value.isCleaning = true;
memoryState.value.isDone = false;
try {
const cmd = deep ? "run_deep_memory_clean" : "run_memory_clean";
const freedBytes = await invoke<number>(cmd);
memoryState.value.lastFreed = formatItemSize(freedBytes);
memoryState.value.isDone = true;
await getMemoryStats();
} catch (err) {
showAlert("清理失败", String(err), 'error');
} finally {
memoryState.value.isCleaning = false;
}
}
// 自动刷新内存
import { onMounted, onUnmounted, watch } from "vue";
let memoryInterval: any = null;
onMounted(() => {
getMemoryStats();
memoryInterval = setInterval(() => {
if (activeTab.value === 'clean-memory') {
getMemoryStats();
}
}, 3000);
});
onUnmounted(() => {
if (memoryInterval) clearInterval(memoryInterval);
});
watch(activeTab, (newTab) => {
if (newTab === 'clean-memory') getMemoryStats();
});
</script>
<template>
@@ -856,7 +919,73 @@ function splitSize(sizeStr: string | number) {
</div>
</section>
<!-- 4. 其他占位 -->
<!-- 4. 内存清理页面 -->
<section v-else-if="activeTab === 'clean-memory'" class="page-container">
<div class="page-header">
<div class="header-info">
<h1>内存加速</h1>
<p>通过压缩进程工作集释放物理内存占用</p>
</div>
</div>
<div class="main-action">
<div class="memory-dashboard">
<div class="memory-gauge" :style="{ '--percent': memoryState.stats?.percent || 0 }">
<svg viewBox="0 0 100 100">
<circle class="gauge-bg" cx="50" cy="50" r="45"></circle>
<circle class="gauge-fill" cx="50" cy="50" r="45" :style="{ strokeDashoffset: 283 - (283 * (memoryState.stats?.percent || 0)) / 100 }"></circle>
</svg>
<div class="gauge-content">
<span class="gauge-value">{{ Math.round(memoryState.stats?.percent || 0) }}<small>%</small></span>
<span class="gauge-label">内存占用</span>
</div>
</div>
</div>
</div>
<div class="memory-stats-grid">
<div class="stat-box shadow-card">
<span class="label">已用内存</span>
<span class="value">{{ formatItemSize(memoryState.stats?.used || 0) }}</span>
</div>
<div class="stat-box shadow-card">
<span class="label">可用内存</span>
<span class="value">{{ formatItemSize(memoryState.stats?.free || 0) }}</span>
</div>
<div class="stat-box shadow-card">
<span class="label">内存总量</span>
<span class="value">{{ formatItemSize(memoryState.stats?.total || 0) }}</span>
</div>
</div>
<div class="memory-actions">
<div class="action-card shadow-card" :class="{ cleaning: memoryState.isCleaning }">
<div class="action-info">
<h3>普通加速</h3>
<p>压缩所有进程的内存占用不影响程序运行</p>
</div>
<button class="btn-primary btn-sm" @click="startMemoryClean(false)" :disabled="memoryState.isCleaning">
{{ memoryState.isCleaning ? '清理中...' : '立即加速' }}
</button>
</div>
<div class="action-card shadow-card secondary" :class="{ cleaning: memoryState.isCleaning }">
<div class="action-info">
<h3>深度加速</h3>
<p>清空系统备用列表待机列表释放更多物理空间</p>
</div>
<button class="btn-secondary btn-sm" @click="startMemoryClean(true)" :disabled="memoryState.isCleaning">
深度加速
</button>
</div>
</div>
<div class="clean-feedback" v-if="memoryState.isDone">
<span class="success-icon"></span>
已为您释放 <span class="freed-amount">{{ memoryState.lastFreed }}</span> 内存空间
</div>
</section>
<!-- 5. 其他占位 -->
<section v-else class="placeholder-page">
<div class="empty-state">
<span class="empty-icon">🛠</span>
@@ -1502,4 +1631,163 @@ body {
background: #F5F5F7;
margin: 4px 0;
}
/* --- 内存清理特有样式 --- */
.memory-dashboard {
margin: 20px 0 40px;
display: flex;
justify-content: center;
}
.memory-gauge {
position: relative;
width: 240px;
height: 240px;
}
.memory-gauge svg {
transform: rotate(-90deg);
width: 100%;
height: 100%;
}
.gauge-bg {
fill: none;
stroke: #F2F2F7;
stroke-width: 8;
}
.gauge-fill {
fill: none;
stroke: var(--primary-color);
stroke-width: 8;
stroke-linecap: round;
stroke-dasharray: 283;
transition: stroke-dashoffset 1s cubic-bezier(0.4, 0, 0.2, 1), stroke 0.3s;
}
/* 根据百分比改变颜色 */
.memory-gauge[style*="--percent: 7"], .memory-gauge[style*="--percent: 8"] { --gauge-color: #FF9500; }
.memory-gauge[style*="--percent: 9"] { --gauge-color: #FF3B30; }
.gauge-content {
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%, -50%);
text-align: center;
display: flex;
flex-direction: column;
}
.gauge-value {
font-size: 56px;
font-weight: 800;
color: var(--text-main);
line-height: 1;
letter-spacing: -2px;
}
.gauge-value small {
font-size: 24px;
margin-left: 2px;
letter-spacing: 0;
}
.gauge-label {
font-size: 14px;
color: var(--text-sec);
font-weight: 600;
margin-top: 4px;
}
.memory-stats-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
margin-bottom: 40px;
}
.stat-box {
background: white;
padding: 20px;
border-radius: 20px;
text-align: center;
display: flex;
flex-direction: column;
gap: 8px;
}
.stat-box .label {
font-size: 13px;
color: var(--text-sec);
font-weight: 500;
}
.stat-box .value {
font-size: 18px;
font-weight: 700;
color: var(--text-main);
}
.memory-actions {
display: flex;
flex-direction: column;
gap: 16px;
}
.action-card {
background: white;
padding: 24px 32px;
border-radius: 24px;
display: flex;
align-items: center;
justify-content: space-between;
transition: all 0.3s;
}
.action-card.secondary {
background-color: #FBFBFD;
border: 1px dashed var(--border-color);
box-shadow: none;
}
.action-info h3 {
font-size: 17px;
font-weight: 700;
margin-bottom: 4px;
}
.action-info p {
font-size: 13px;
color: var(--text-sec);
}
.clean-feedback {
margin-top: 32px;
text-align: center;
padding: 16px;
background-color: #E8F5E9;
color: #2E7D32;
border-radius: 16px;
font-weight: 600;
font-size: 14px;
animation: slideUp 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.freed-amount {
font-size: 18px;
font-weight: 800;
text-decoration: underline;
}
@keyframes slideUp {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
.action-card.cleaning {
filter: grayscale(1);
opacity: 0.7;
pointer-events: none;
}
</style>