You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过PowerShell获取exe程序运行过程中的内存消耗峰值

获取PowerShell启动程序的全程内存峰值方案

下面提供三种适用不同场景的原生Windows方案,无需安装第三方工具:

方案1:启动程序时同步统计(最准确)

直接通过PowerShell脚本启动目标程序,从进程创建开始全程跟踪内存,程序退出后自动输出峰值:

# 替换为你的程序路径和启动参数
$executablePath = "C:\your\app\path.exe"
$launchArgs = "your-args-here"

# 启动进程并获取句柄
$targetProcess = Start-Process -FilePath $executablePath -ArgumentList $launchArgs -PassThru
$maxMemory = 0

# 轮询进程内存直到退出
while (!$targetProcess.HasExited) {
    # PrivateMemorySize64对应程序专属的已分配虚拟内存,符合你要的最大分配内存需求
    # 若需要统计物理内存占用,替换为WorkingSet64即可
    $currentMem = $targetProcess.PrivateMemorySize64
    if ($currentMem -gt $maxMemory) {
        $maxMemory = $currentMem
    }
    # 1秒轮询间隔对1-2小时运行的程序完全够用,资源消耗可忽略
    Start-Sleep -Seconds 1
}

# 输出结果,单位转换为MB,保留2位小数
$maxMemMB = [math]::Round($maxMemory / 1MB, 2)
Write-Host "程序全程内存分配峰值:$maxMemMB MB"
  • 如果你的程序内存波动极快,可以把轮询间隔改成500毫秒,对应修改为Start-Sleep -Milliseconds 500即可。

方案2:已启动程序的附加统计

如果程序已经在运行,不想中断重启,可以用下面的脚本附加到现有进程统计后续峰值:

# 替换为你的程序进程名(不含.exe后缀)
$processName = "your-process-name"
$targetProcess = Get-Process -Name $processName -ErrorAction Stop
$maxMemory = 0

while (!$targetProcess.HasExited) {
    $currentMem = $targetProcess.PrivateMemorySize64
    if ($currentMem -gt $maxMemory) {
        $maxMemory = $currentMem
    }
    Start-Sleep -Seconds 1
}

$maxMemMB = [math]::Round($maxMemory / 1MB, 2)
Write-Host "统计周期内程序内存分配峰值:$maxMemMB MB"
  • 注意:该方案只能统计脚本运行之后的内存峰值,进程启动后到脚本运行前出现的峰值无法记录,追求完整数据优先用方案1。

方案3:性能监视器交叉验证

需要可视化验证或多指标统计时可以用Windows自带工具:

  1. 按Win+R输入perfmon回车打开性能监视器
  2. 点击左侧「性能监视器」,点击顶部工具栏的加号按钮
  3. 在计数器列表找到「Process」分类,选择「Private Bytes」(分配内存)或「Working Set」(物理内存占用),在实例列表选中你的程序添加
  4. 程序运行结束后,直接查看图表的最大值即可。

内容的提问来源于stack exchange,提问作者Samer Aamar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 07:57:03