在C#中获取内存空间 [英] get memory space in c#

查看:428
本文介绍了在C#中获取内存空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何在c#中执行此操作,在c ++中的示例是:

How would you do this in c#, an example in c++ is:

void PrintMemoryInfo( DWORD processID )
{
     std::ofstream fs("d:\\processInfo.txt"); 
     fs<<"Information of Process:\n";

    HANDLE hProcess;
    PROCESS_MEMORY_COUNTERS pmc;
    fs<<"\nProcess ID: %u\n"<<processID;

    hProcess = OpenProcess(  PROCESS_QUERY_INFORMATION |
                                    PROCESS_VM_READ,
                                    FALSE, processID );
  if (NULL == hProcess) return;

    if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )    {



        fs<< "\tPageFaultCount: 0x%08X\n" << pmc.PageFaultCount;
        fs<< "\tYour app's PEAK MEMORY CONSUMPTION: 0x%08X\n"<<pmc.PeakWorkingSetSize;
        fs<< "\tYour app's CURRENT MEMORY CONSUMPTION: 0x%08X\n"<< pmc.WorkingSetSize;
        fs<< "\tQuotaPeakPagedPoolUsage: 0x%08X\n"<< 
                  pmc.QuotaPeakPagedPoolUsage;
        fs<< "\tQuotaPagedPoolUsage: 0x%08X\n"<< 
                  pmc.QuotaPagedPoolUsage;
        fs<< "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n"<< 
                  pmc.QuotaPeakNonPagedPoolUsage;
        fs<< "\tQuotaNonPagedPoolUsage: 0x%08X\n"<< 
                  pmc.QuotaNonPagedPoolUsage;
        fs<< "\tPagefileUsage: 0x%08X\n"<< pmc.PagefileUsage; 
        fs<< "\tPeakPagefileUsage: 0x%08X\n"<< 
                  pmc.PeakPagefileUsage;                  
    }
    fs.close();
    CloseHandle( hProcess);
}

int main( )
{
  PrintMemoryInfo( GetCurrentProcessId() );

    return 0;
}

但是在C#中吗?...

but in c#?...

推荐答案

这里还有其他几篇文章描述了如何获取正在运行的应用程序的内存足迹:

Here are a few other articles that describe getting a running application's memory footprint:

在运行时轮询C#应用程序的内存使用情况?

C#中的内存使用情况

TL; DR;

// get the current process
Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();

// get the physical mem usage
long totalBytesOfMemoryUsed = currentProcess.WorkingSet64;

这篇关于在C#中获取内存空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆