Introducing the Batch Utility for Windows - mem.cmd 想知道CHROME到底有多占内存么?

in #programming6 years ago (edited)

I use HPZ800 server at home, which is almost never turned off or restarted. And I see this today:

我家里用的是HPZ800的服务器,所以长年不关机不重启,今天我看了CHROME好吃内存:

image.png

And the Proceses Tab gives you rough idea how big memory is consumed by Chrome.exe

在 Processes 页,操作系统列出了每个程序的吃内存情况,我们可以看到CHROME很吃内存。

image.png

However, I'd like to have a command line/tool that prints the memory usage for a given application.

然而,我想着写一个小命令行工具,练练手。

Github:

@echo off
REM Calculate Total Memory Consumption for a Process

setlocal enabledelayedexpansion
set prog=%1

if [%1]==[] (
  echo Usage: %0 Process
  goto end
)
set sum=0
@for /F "tokens=5" %%i in ('tasklist ^| grep !prog!') do (
    set mem=%%i
    set mem=!mem:,=!
    set /a sum=sum+!mem!
)

echo Total Memory for !prog! is !sum! K
set /a sum=sum/1024
echo Total Memory for !prog! is !sum! MB
set /a sum=sum/1024
echo Total Memory for !prog! is !sum! GB

:end

The idea is to use for /f in windows command line shell that will split the lines by default delimiter space and with tokens=5 that will extract the memory usage for that process.

原理就是用 for /f 来对每一行的输出进行字符串分割,tokens=5 会只选择第五列,也就是内存用量。

And we can use !variable:,=! to remove the , from the numbers and use set /a do sum up the number strings. However, you need to setlocal enabledelayedexpansion to allow variables updated at runtime (instead of pre-interpreted)

我们需要用 !variable:,=! 来把数字中的逗号去掉,然后用 set /a 来进行数字叠加,最后面我们需要启用 setlocal enabledelayedexpansion 在WINDOWS批处理中开启变量支持 - 否则变量只会在批处理启动的时候替换一次。

However, at Linux, you wouldn't need this at all, because you can use wc, awk, cut etc existing tools to achieve the same task by piping these commands one by one i.e. Where there is a shell, there is a way!

在LINUX下我们就不需要这么麻烦了,因为已经有很多现成好用的工具,比如 wc, awk, cut 这些命令可以通过管道来完成同样的任务。

image.png

Support me and my work as a witness - witness thread by

  1. voting me here, or
  2. voting me as a proxy.

Thank you! Some of My Contributions: SteemIt Tutorials, Robots, Tools and APIs

Reposted to: https://helloacm.com/introducing-the-batch-utility-for-windows-mem-cmd/

支持我的工作 支持我成为 见证人

  1. 请在 这里 投我一票, 或者
  2. 设置我 为代理.
    谢谢您! 我的贡献:SteemIt 工具、API接口、机器人和教程

股东工具

  1. 成为股东:代理5 SP 即可 (退出股东 输入 0即可)
  2. 查询当前股东
    请注意:每次代理都是以最后一次输入的SP数量为标准,比如已经代理10 SP,想多代理5 SP则需要输入 最后的数字 15 SP(而不是 5!)

博文刚刚同步到了:https://justyy.com/archives/6413

Sort:  

Congratulations @justyy! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of posts published
You published a post every day of the week

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Do not miss the last post from @steemitboard:
SteemitBoard World Cup Contest - Final results coming soon

You can upvote this notification to help all Steemit users. Learn why here!

Coin Marketplace

STEEM 0.26
TRX 0.11
JST 0.033
BTC 64006.33
ETH 3077.08
USDT 1.00
SBD 3.87