C#实现进程内存信息获取
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
staticclassNat
{
[StructLayout(LayoutKind.Sequential]
struct IO_COUNTERS
{
public ulong ReadOperationCount;
public ulong WriteOperationCount;
public ulong OtherOperationCount;
public ulong ReadTransferCount;
public ulong WriteTransferCount;
public ulong OtherTransferCount;
}
[DllImport("kernel32.dll")]
unsafe static extern bool GetProcessIoCounters(IntPtrProcessHandle,out IO_COUNTERS IoCounters);
[StructLayout(LayoutKind.Sequential,Size=40)]
privatestruct PROCESS_MEMORY_COUNTERS
{
public uint cb;
public uint PageFaultCount;
public uint PeakWorkingSetSize;
public uint WorkingSetSize;
public uint QuotaPeakPagedPoolUsage;
public uint QuotaPagedPoolUsage;
public uint QuotaPeakNonPagedPoolUsage;
public uint QuotaNonPagedPoolUsage;
public uint PagefileUsage;
public uint PeakPagefileUsage;
}
[DllImport("psapi.dll",SetLastError=true)]
unsafe static extern bool GetProcessMemoryInfo(IntPtr* hProcess,out PROCESS_MEMORY_COUNTERS*Memcounters,int size);
publicstaticclass IO
{
unsafepublicstaticDictionary<string,ulong>GetALLIO(Process procToRtrivIO)
{
IO_COUNTERS counters;
Dictionary<string,ulong> retCountIoDict =newDictionary<string,ulong>();
IntPtr ptr =System.Diagnostics.Process.GetCurrentProcess().Handle;
GetProcessIoCounters(ptr,out counters);
retCountIoDict.Add("ReadOperationCount", counters.ReadOperationCount);
retCountIoDict.Add("WriteOperationCount", counters.WriteOperationCount);
retCountIoDict.Add("OtherOperationCount", counters.OtherOperationCount);
retCountIoDict.Add("ReadTransferCount", counters.ReadTransferCount);
retCountIoDict.Add("WriteTransferCount", counters.WriteTransferCount);
retCountIoDict.Add("OtherTransferCount", counters.OtherTransferCount);
return retCountIoDict;
//return "This process has read " + ((counters.ReadTransferCount/1024)/1024).ToString("N0") +
// " Mb of data.";
}
}
publicstaticclassMem
{
unsafe public staticDictionary<string,uint>GetAllMem(Process procToRtrivMem)
{
PROCESS_MEMORY_COUNTERS*MemCounters;
Dictionary<string,uint> retCountMemDict =newDictionary<string,uint>();
IntPtr ptr =System.Diagnostics.Process.GetCurrentProcess().Handle;
GetProcessMemoryInfo(&ptr,outMemCounters,Marshal.SizeOf(typeof(PROCESS_MEMORY_COUNTERS)));//MemCounters.cb);
retCountMemDict.Add("cb",MemCounters->cb);
retCountMemDict.Add("PageFaultCount",MemCounters->PageFaultCount);
retCountMemDict.Add("PeakWorkingSetSize",MemCounters->PeakWorkingSetSize);
retCountMemDict.Add("WorkingSetSize",MemCounters->WorkingSetSize);
retCountMemDict.Add("QuotaPeakPagedPoolUsage",MemCounters->QuotaPeakPagedPoolUsage);
retCountMemDict.Add("QuotaPagedPoolUsage",MemCounters->QuotaPagedPoolUsage);
retCountMemDict.Add("QuotaPeakNonPagedPoolUsage",MemCounters->QuotaPeakNonPagedPoolUsage);
retCountMemDict.Add("QuotaNonPagedPoolUsage",MemCounters->QuotaNonPagedPoolUsage);
retCountMemDict.Add("PagefileUsage",MemCounters->PagefileUsage);
retCountMemDict.Add("PeakPagefileUsage",MemCounters->PeakPagefileUsage);
return retCountMemDict;
//return "This process has read " + ((counters.ReadTransferCount/1024)/1024).ToString("N0") +
// " Mb of data.";
}
}
}
参考:using unsafe code in C# asp.net http://stackoverflow.com/questions/17207310/using-unsafe-code-in-c-sharp-asp-net
collectiong memory usage information for a processhttp://msdn.microsoft.com/en-us/library/windows/desktop/ms682050(v=vs.85).aspx
在C#中调用psapi.dll内置的GetProcessMemoryInfo函数http://social.microsoft.com/Forums/it-IT/650197e0-a21a-4f5e-a974-23f074f52a55/cpsapidllgetprocessmemoryinfo?forum=visualcshartzhchs
ASP.NET(C#)获取当前计算机CPU内存使用率等相关信息http://luzinwbing.blog.163.com/blog/static/113805840201031093415658/
不安全代码只会在使用/unsafe编译情况下使用 http://lixiaorong223.blog.163.com/blog/static/44011629200993181241924/
wmi获得进程的虚拟内存与任务管理器中显示的不一致 http://bbs.csdn.net/topics/260033107
C#实现进程内存信息获取的更多相关文章
- swift的类型系统及类型(内存)信息获取:接口、编译运行时、反射、内存布局
swift是静态语言,没有在运行时保存类型的结构信息(isa.class). 一.self.Self.Type.typeof extension Collection where Self.Eleme ...
- Android中获取系统内存信息以及进程信息-----ActivityManager的使用(一)
本节内容主要是讲解ActivityManager的使用,通过ActivityManager我们可以获得系统里正在运行的activities,包括 进程(Process)等.应用程序/包.服务(Serv ...
- Linux系统下输出某进程内存占用信息的c程序实现
在实际工作中有时需要程序打印出某个进程的内存占用情况以作参考, 下面介绍一种通过Linux下的伪文件系统/proc 计算某进程内存占用的程序实现方法. 首先, 为什么会有所谓的 伪文件 呢. Linu ...
- 获取系统中所有进程&线程信息
读书笔记--[计算机病毒解密与对抗] 目录: 遍历进程&线程程序 终止进程 获取进程信息 获取进程内模块信息 获取进程命令行参数 代码运行环境:Win7 x64 VS2012 Update3 ...
- 显示所有APP的进程详细信息(进程ID、进程所在UID、进程占用内存、进程名)
main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...
- 使用python获取CPU和内存信息的思路与实现(linux系统)
linux里一切皆为文件,在linux/unix的根文件夹下,有个/proc文件夹,这个/proc 是一种内核和内核模块用来向进程(process)发送信息的机制(所以叫做"/proc&qu ...
- 【.net 深呼吸】启动一个进程并实时获取状态信息
地球人和火星人都知道,Process类既可以获取正在运行的进程,也可以启动一个新的进程.在79.77%应用场合,我们只需要让目标进程顺利启动就完事了,至于它执行了啥,有没有出错,啥时候退出就不管了. ...
- Android获取cpu和内存信息、网址的代码
android获取手机cpu并判断是单核还是多核 /** * Gets the number of cores available in this device, across all proce ...
- PHP检测获取内存信息
PHP也可以检测获取到Windows的内存信息,而且代码还挺简单,无意发现的,觉得以后能用上,在此与大家分享. 本代码将得到总内存.初始使用等内存信息: <?php echo "初始: ...
随机推荐
- 2015第30周四Java日志组件
Java 日志 API 从功能上来说,日志 API 本身所需求的功能非常简单,只需要能够记录一段文本即可.API 的使用者在需要进行记录时,根据当前的上下文信息构造出相应的文本信息,调用 API 完成 ...
- php实现 坐标移动
php实现 坐标移动 一.总结 一句话总结:伪代码,带函数逻辑,函数这样的方式写算法程序会节约超多的时间. 1.为什么算法题数据输入最好用多组数据输入的方式? 因为都是多组数据测试,而且多组数据输入 ...
- Uncaught SyntaxError: Invalid regular expression flags(看页面源代码)
Uncaught SyntaxError: Invalid regular expression flags(看页面源代码) 一.总结 js或者jquery方面的错误看页面源代码,一下子错误就很清晰了 ...
- 某整形数组中除了两个单身整数外, 其余的整数都是成对出现的, 利用C/C++代码求出这两个单身整数。 要求: 时间复杂度o(n), 空间复杂度o(1)------某公司招聘试题
先看看这个题目:某整形数组中除了两个单身整数外, 其余的整数都是成对出现的, 利用C代码求出这两个单身整数. 要求: 时间复杂度o(n), 空间复杂度o(1). 我们先用最傻瓜的方式来做吧: #inc ...
- 【42%】【hdu1166】排兵布阵(树状数组解法&&线段树解法)
Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任 ...
- [NPM] Use a shorthand syntax for running multiple npm scripts with npm-run-all
Running multiple scripts in series or in parallel can become very verbose. Using a tool such as npm- ...
- 深度学习框架 —— tflearn 的学习
1. tflearn.data_utils from tflearn.data_utils import to_categorical one_hot 编码: 第一个参数为属性列,第二个参数接受类别个 ...
- 【t064】最勇敢的机器人
Time Limit: 1 second Memory Limit: 128 MB [问题描述] [背景] Wind设计了很多机器人.但是它们都认为自己是最强的,于是,一场比赛开始了~ [问题描述] ...
- 【codeforces 782D】 Innokenty and a Football League
[题目链接]:http://codeforces.com/contest/782 [题意] 每个队名有两种选择, 然后第一个选择队名相同的那些队只能选第二种; 让你安排队名 [题解] 首先全都选成第一 ...
- Browser security standards via access control
A computing system is operable to contain a security module within an operating system. This securit ...