- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我没有在仅使用 W MIC 的批处理脚本中获得内存利用率百分比的结果。我只获得总内存和空闲内存(以字节为单位)。
那么如何获得以兆字节为单位的数据以及如何计算内存利用率百分比?
代码:-
@echo off
setlocal enabledelayedexpansion
set Times=0
for /f "skip=1" %%p in ('wmic cpu get loadpercentage') do (
set Cpusage!Times!=%%p
set /A Times+=1
)
echo CPU Percentage = %Cpusage0%%%
set Times=0
for /f "skip=1" %%p in ('wmic ComputerSystem get TotalPhysicalMemory') do (
set totalMem!Times!=%%p
set /A Times+=1
)
set Times=0
for /f "skip=1" %%p in ('wmic OS get FreePhysicalMemory') do (
set availableMem!Times!=%%p
set /A Times+=1
)
set Times=0
for /f "skip=1" %%p in ('wmic OS get FreePhysicalMemory ^|findstr physical') do (
set /a UsedMem= totalMem - availableMem
set usedMem!Times!=%%p
set /A Times+=1
)
set /a usedpercent=(usedMem*100)/totalMem
echo Free MEMORY = %availableMem0% Bytes
echo Total MEMORY = %totalMem0% Bytes
echo Used MEMORY = %UsedMem0% Bytes
echo Memory Utilization = %usedpercent0%%%
pause
最佳答案
如果没有必要多次使用 setlocal
,
setlocal enabledelayedexpansion
通常只在一个批处理文件中使用一次。此命令不仅启用延迟扩展模式。它总是还复制整个当前环境表(最大可达 64 MB)、命令扩展和延迟扩展的当前状态以及堆栈(内存)上的当前目录路径。这种环境压栈的次数不是无限的。至少在两者之间使用 endlocal
以避免由于堆栈溢出而提前退出批处理。有关详细信息,请参阅以下答案:
即使是 64 位 Windows 命令处理器 (cmd.exe
) 也使用 32 位有符号整数。因此,取值范围限制在 -2.147.483.648 到 +2.147.483.647 之间。换句话说,如果没有整数溢出产生错误结果,则无法完成 2 GB 或更多的算术运算。
这是一个注释批处理文件,它不适用于所有可能安装的 RAM 配置,但适用于 2016 年的典型配置:2 GB、4 GB、8 GB、16 GB 和 32 GB。
@echo off
rem Note: KB = KiB, MB = MiB and GB = GiB in this batch file, see
rem https://en.wikipedia.org/wiki/Byte for details on GiB.
rem Create a copy of current environment variables. Enabling additionally
rem delayed environment variable expansion is not required for this task.
setlocal EnableExtensions DisableDelayedExpansion
rem The command WMIC with the parameters CPU GET LoadPercentage outputs
rem one line per processor. The output of WMIC is in UTF-16 LE with BOM.
rem The output is redirected to a temporary file which is printed by
rem command TYPE to STDOUT which makes a better job on UNICODE to ASCII
rem conversion as command FOR. Note: 1 processor can have 1 or more cores.
set "CpuUsage=0"
set "Processors=0"
%SystemRoot%\System32\wbem\wmic.exe CPU GET LoadPercentage >"%TEMP%\cpu_usage.tmp"
for /F "skip=1" %%P in ('type "%TEMP%\cpu_usage.tmp"') do (
set /A CpuUsage+=%%P
set /A Processors+=1
)
del "%TEMP%\cpu_usage.tmp"
rem Calculate the CPU usage as percentage value of all processors.
set /A CpuUsage/=Processors
goto GetTotalMemory
rem Output of WMIC is in UTF-16 LE with BOM. The interpretation of this
rem output in ASCII/OEM can result in processing three lines instead of
rem just two with third line being just a carriage return. Therefore exit
rem each loop after assigning the value of second line to the variable.
:GetTotalMemory
for /F "skip=1" %%M in ('%SystemRoot%\System32\wbem\wmic.exe ComputerSystem GET TotalPhysicalMemory') do set "TotalMemory=%%M" & goto GetAvailableMemory
:GetAvailableMemory
for /F "skip=1" %%M in ('%SystemRoot%\System32\wbem\wmic.exe OS GET FreePhysicalMemory') do set "AvailableMemory=%%M" & goto ProcessValues
rem Total physical memory is in bytes which can be greater 2^31 (= 2 GB)
rem Windows command processor performs arithmetic operations always with
rem 32-bit signed integer. Therefore 2 GB or more installed physical
rem memory exceeds the bit width of a 32-bit signed integer and arithmetic
rem calculations are wrong on 2 GB or more installed memory. To avoid
rem the integer overflow, the last 6 characters are removed from bytes
rem value and the remaining characters are divided by 1073 to get the
rem number of GB. This workaround works only for physical RAM being
rem an exact multiple of 1 GB, i.e. for 1 GB, 2 GB, 4 GB, 8 GB, ...
rem 1 GB = 1.073.741.824 bytes = 2^30
rem 2 GB = 2.147.483.648 bytes = 2^31
rem 4 GB = 4.294.967.296 bytes = 2^32
rem 8 GB = 8.589.934.592 bytes = 2^33
rem 16 GB = 17.179.869.184 bytes = 2^34
rem 32 GB = 34.359.738.368 bytes = 2^35
rem But there is one more problem at least on Windows XP x86. About 50 MB
rem of RAM is subtracted as used by Windows itself. This can be seen in
rem system settings when 1.95 GB is displayed although 2 GB is installed.
rem Therefore add 50 MB before dividing by 1073.
:ProcessValues
set "TotalMemory=%TotalMemory:~0,-6%"
set /A TotalMemory+=50
set /A TotalMemory/=1073
rem The total memory in GB must be multiplied by 1024 to get the
rem total physical memory in MB which is always small enough to
rem be calculated with a 32-bit signed integer.
set /A TotalMemory*=1024
rem The available memory is in KB and therefore there is
rem no problem with value range of 32-bit signed integer.
set /A AvailableMemory/=1024
rem So the used memory in MB can be determined easily.
set /A UsedMemory=TotalMemory - AvailableMemory
rem It is necessary to calculate the percentage value in MB instead of
rem KB to avoid a 32-bit signed integer overflow on 32 GB RAM and nearly
rem entire RAM is available because used is just a small amount of RAM.
set /A UsedPercent=(UsedMemory * 100) / TotalMemory
if "%Processors%" == "1" (
set "ProcessorInfo="
) else (
set "ProcessorInfo= of %Processors% processors"
)
echo CPU percentage: %CpuUsage% %%%ProcessorInfo%
echo Free memory: %AvailableMemory% MB
echo Total memory: %TotalMemory% MB
echo Used memory: %UsedMemory% MB
echo Memory usage: %UsedPercent% %%
rem Discard the current environment variable table and restore previous
rem environment variables. The states of command processor extension
rem (default: ON) and delayed expansion (default: OFF) as well as the
rem original current directory are restored by this command although
rem not modified at all by the commands above.
endlocal
要了解使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读为每个命令显示的所有帮助页面。
del/?
echo/?
endlocal/?
for/?
goto/?
rem/?
setlocal/?
设置/?
输入/?
wmic/?
wmic CPU get/?
wmic 操作系统获取/?
wmic ComputerSystem get/?
关于windows - 如何在批处理脚本中使用 WMIC 获取内存利用率百分比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37338476/
我的函数概念上都返回相同的东西,但结果可以采用不同的形式: function GetThingy() 有四个不同的函数,每个可以返回不同的东西: 0.071(代表增长 7.1% 的 float 值)
这个问题在这里已经有了答案: Int division: Why is the result of 1/3 == 0? (19 个回答) 关闭 4 年前。 有什么方法可以计算(例如)120 的 50
我四处寻找这个,它看起来很简单,但我无法让它工作。 我有一张表格,其中一列需要格式化为百分比。下面是我的代码,但它没有格式化单元格,它只是将它们保留为小数。 我想这是因为 cell ,即使声明为范围,
我刚刚开始使用 WPF。从那以后,我开始关注造型系统。我来自 CSS 背景,我想以百分比设置边距。 当前值以像素为单位
我有一个表,其中每一行都有一个描述字段和一个 bool 值。我正在尝试编写一个查询,我可以在其中按每个相应的描述进行分组,并查看 bool 值为真的次数百分比。 示例表: PID Gen
我从文档中发现,考虑到 orientdb 100% 使用磁盘缓存,它使用的最大大小为 70% 用于读取缓存,30% 用于写入缓存 ( http://orientdb.com/docs/last/plo
有什么方法可以获取 docker 容器内部而不是外部的 cpu 百分比吗?! docker stats DOCKER_ID 显示的百分比正是我需要的,但我需要它作为变量。我需要获取容器本身内部的 cp
我正在尝试计算数据集每列中类别的比例(百分比)。 示例数据: df <- data.frame( "Size" = c("Y","N","N","Y","Y"), "Type" =
我应该使用小数还是 float 在数据库中存储比率?特别是在 SQL2005 中。 最佳答案 这取决于您对准确性的需求。如果您可以容忍来自存储 float 的 IEEE 方法的典型错误,则使用 flo
我正在创建一个游戏,目前必须处理一些math.random问题。 我的Lua能力不是那么强,你觉得怎么样 您能制定一个使用 math.random 和给定百分比的算法吗? 我的意思是这样的函数: fu
如何在SQL中动态计算百分比? 假设您有一个名为 Classes 的下表: ClassSession StudentName -------------------------------
如何通过 jQuery 创建具有百分比的数字掩码输入?我是否让输入仅接受三个数字,并在用户完成输入时在数字后添加百分号(keyup)? 我不使用插件。 示例:1% 或 30% 或 99% 或 100%
我正在尝试构建一个工具,可以突出显示具有最高介数中心性的社交网络节点。我将所有网络节点的测量值计算到字典中,按顺序对字典进行排序,然后仅保留前 3 对。 我希望这个工具是可扩展的,所以我想保留前 10
MYSQL 中的人员如何将一个日期条目和分数的用户百分比与另一个日期条目和分数进行比较,从而有效地返回从一个日期到另一个日期的用户百分比增加情况? 几天来我一直在试图解决这个问题,但我已经没有想法了,
我需要进行查询,结果是百分比。 我现在的查询如下所示: select COUNT(CREATE_WEEKDAY), CREATE_WEEKDAY, COUNT(CREATE
我有一个图像上传功能,其工作原理如下: $('.update-insertimage-form').submit(function() { $(".submit-newupdate-btn").add
我的问题很简单,但我仍然找不到这个问题的答案... 假设我们有两个包含图像的容器。 我们有类似的东西 #containera { width: 50%; height: 50%; backgr
是否可以将 CSS 尺寸指定为除其父元素之外的另一个元素的百分比?例如,我想将 div 的 border-radius 指定为 div 宽度的 10%。但是,border-radius: 10% 在
我正在尝试设置按钮的大小并以百分比进行编辑 但是这个的线性大小是不同的。为什么? 最佳答案 您好,问题出在属性 box-sizing 上.默认为 input type
我将它用于我的页眉,该页眉在一页上下滚动页面中发生变化。我注意到它没有响应,所以我想问你是否知道一种使它响应的方法。就像将 0-690 更改为百分比,以便它可以在移动设备和电视屏幕上使用。 HTML
我是一名优秀的程序员,十分优秀!