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 "初始: ...
随机推荐
- [PReact] Handle Simple Routing with preact-router
Some applications only need a very minimal routing solution. This lesson will cover a practical exam ...
- php 百度地图 腾讯地图 转换坐标
/* * 中国正常GCJ02坐标---->百度地图BD09坐标 * 腾讯地图用的也是GCJ02坐标 * @param double $lat 纬度 * @param double $lng 经度 ...
- HDU 1045 Fire Net(行列匹配变形+缩点建图)
题意:n*n的棋盘上放置房子.同一方同一列不能有两个,除非他们之间被墙隔开,这种话. 把原始图分别按行和列缩点 建图:横竖分区.先看每一列.同一列相连的空地同一时候看成一个点,显然这种区域不可以同一时 ...
- [Angular] Observable.catch error handling in Angular
import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/map'; import 'rxjs/add/opera ...
- 小强的HTML5移动开发之路(43)——JqueryMobile页眉、工具栏和标签栏导航
一.页眉 1.添加页眉和页脚 <div data-role="header"> <h1>第 1 页</h1> </div> < ...
- [Angular] USING ZONES IN ANGULAR FOR BETTER PERFORMANCE
Link to the artical. Zone detects any async opreations. Once an async oprations happens in Angular, ...
- [Postgres] Filter Data in a Postgres Table with Query Statements
We have all this data, but how do we answer questions about it? In this lesson we’ll learn how to fi ...
- epoll 和select
epoll 水平触发和边缘触发的区别 EPOLLLT——水平触发EPOLLET——边缘触发 epoll有EPOLLLT和EPOLLET两种触发模式,LT是默认的模式,ET是“高速”模式.LT模式下,只 ...
- AR Drone系列之:使用ROS catkin创建package并使用cv_bridge实现对ar drone摄像头数据的处理
1 开发环境 Ubuntu 12.04 ROS Hydro 2 前提 可參考这篇blog:http://blog.csdn.net/yake827/article/details/44564057 b ...
- 【b404】虫食算
Time Limit: 1 second Memory Limit: 50 MB [问题描述] 所谓虫食算,就是原先的算式中有一部分被虫子啃掉了,需要我们根据剩下的数字来判定被啃掉的字母.来看一个简单 ...