找回密码
 加入
搜索
查看: 11615|回复: 16

[系统综合] tasklist 中获取某进程的内存占用为什么和任务管理器中的不一样?

 火.. [复制链接]
发表于 2012-2-23 13:09:23 | 显示全部楼层 |阅读模式
大家好:

    我想使用autoit监控单一进程的内存使用率,但是一直没有找到好的解决方法。于是想到了用tasklist这种方式,但是发现 tasklist返回的内存使用率和任务管理器中的不一致? 在网上也找不到答案,请大侠帮我解惑,不甚感激!!
 楼主| 发表于 2012-2-23 13:13:21 | 显示全部楼层




这是一个notepad.exe例子,我也没有找到换算关系..



请大家帮忙看看

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2012-2-25 23:34:32 | 显示全部楼层
怎么 没人 回答?

哎。。。。。。。。。。
发表于 2012-2-25 23:40:32 | 显示全部楼层
我这测试是一样的
发表于 2012-2-26 09:47:30 | 显示全部楼层
好像是内存占用和虚拟内存占用的总和
 楼主| 发表于 2012-2-27 09:00:57 | 显示全部楼层
回复 3# MicroBlue


   Still Thanks 阿凡........
 楼主| 发表于 2012-2-27 09:03:00 | 显示全部楼层
回复 4# afan


    afan, 我想监控一个程序的 内存使用率,想用 API 去做,但是对API 不熟悉。 afan 给个链接,或给个明路呗...........我已经搜索过论坛了,没有找到好的方法啊。
 楼主| 发表于 2012-2-27 09:04:15 | 显示全部楼层
回复 4# afan


thanks, 为何还有虚拟内存啊?
发表于 2012-2-27 10:07:39 | 显示全部楼层
回复  afan


    afan, 我想监控一个程序的 内存使用率,想用 API 去做,但是对API 不熟悉。 afan 给个 ...
MicroBlue 发表于 2012-2-27 09:03
#include <WinAPIEx.au3>
#include <Array.au3>

Local $aPidL = ProcessList()
If @error Then Exit
For $i = 1 To $aPidL[0][0]
        $aPidL[$i][1] = _GetProcessMemory($aPidL[$i][1])
Next
_ArrayDisplay($aPidL, '进程内存占用')

Func _GetProcessMemory($iPid, $NFormat = 1)
        ;afan提示:默认返回以千位逗号分割的内存占用KB值;$NFormat 参数为0则返回字节数
        Local $Data = _WinAPI_GetProcessMemoryInfo($iPid)
        If Not IsArray($Data) Then Return SetError(1, '', '')
        If Not $NFormat Then Return $Data[2]
        Return StringRegExpReplace($Data[2] / 1024, '(\d+?)(?=(?:\d{3})+\Z)', '$1,') & ' (K)'
EndFunc   ;==>_GetProcessMemory
 楼主| 发表于 2012-2-27 10:44:48 | 显示全部楼层
回复 9# afan


    Thanks  a lot .
发表于 2013-4-11 15:28:13 | 显示全部楼层
afan 发表于 2012-2-27 10:07



    大大啊~~为毛 我这边

_GetProcessMemory 是Return SetError(1, '', '')

求帮助啊!
发表于 2013-4-11 15:32:11 | 显示全部楼层
回复 11# jasmine


    查看 _WinAPI_GetProcessMemoryInfo() 找原因
; #FUNCTION# ====================================================================================================================
; Name...........: _WinAPI_GetProcessMemoryInfo
; Description....: Retrieves information about the memory usage of the specified process.
; Syntax.........: _WinAPI_GetProcessMemoryInfo ( [$PID] )
; Parameters.....: $PID    - The PID of the process. Default (0) is the current process.
; Return values..: Success - The array that contains the following information.
;
;                            [0] - The number of page faults.
;                            [1] - The peak working set size, in bytes.
;                            [2] - The current working set size, in bytes.
;                            [3] - The peak paged pool usage, in bytes.
;                            [4] - The current paged pool usage, in bytes.
;                            [5] - The peak nonpaged pool usage, in bytes.
;                            [6] - The current nonpaged pool usage, in bytes.
;                            [7] - The current space allocated for the pagefile, in bytes.
;                            [8] - The peak space allocated for the pagefile, in bytes.
;                            [9] - The current amount of memory that cannot be shared with other processes, in bytes.
;
;                  Failure - 0 and sets the @error flag to non-zero.
; Author.........: Yashied
; Modified.......:
; Remarks........: None
; Related........:
; Link...........: @@MsdnLink@@ GetProcessMemoryInfo
; Example........: Yes
; ===============================================================================================================================

Func _WinAPI_GetProcessMemoryInfo($PID = 0)

        If Not $PID Then
                $PID = _WinAPI_GetCurrentProcessID()
                If Not $PID Then
                        Return SetError(1, 0, 0)
                EndIf
        EndIf

        Local $hProcess = DllCall('kernel32.dll', 'ptr', 'OpenProcess', 'dword', 0x00000410, 'int', 0, 'dword', $PID)

        If (@error) Or (Not $hProcess[0]) Then
                Return SetError(1, 0, 0)
        EndIf

        Local $tPMC_EX = DllStructCreate('dword;dword;ulong_ptr;ulong_ptr;ulong_ptr;ulong_ptr;ulong_ptr;ulong_ptr;ulong_ptr;ulong_ptr;ulong_ptr')
        Local $Ret = DllCall(@SystemDir & '\psapi.dll', 'int', 'GetProcessMemoryInfo', 'ptr', $hProcess[0], 'ptr', DllStructGetPtr($tPMC_EX), 'int', DllStructGetSize($tPMC_EX))

        If (@error) Or (Not $Ret[0]) Then
                $Ret = 0
        EndIf
        _WinAPI_CloseHandle($hProcess[0])
        If Not IsArray($Ret) Then
                Return SetError(1, 0, 0)
        EndIf

        Local $Result[10]

        For $i = 0 To 9
                $Result[$i] = DllStructGetData($tPMC_EX, $i + 2)
        Next
        Return $Result
EndFunc   ;==>_WinAPI_GetProcessMemoryInfo
发表于 2013-4-11 16:12:31 | 显示全部楼层
本帖最后由 jasmine 于 2013-4-11 16:26 编辑

回复 12# afan
Local $hProcess = DllCall('kernel32.dll', 'ptr', 'OpenProcess', 'dword', 0x00000410, 'int', 0, 'dword', $PID)

        If (@error) Or (Not $hProcess[0]) Then
                Return SetError(1, 0, 0)
        EndIf
这里 @error 为0
$hProcess[0] 为0
发表于 2013-4-11 16:17:23 | 显示全部楼层
求大大 帮助
发表于 2013-11-25 12:59:43 | 显示全部楼层
受教了。不错记号。一下。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-29 03:21 , Processed in 0.125185 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表