Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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:
";

    HANDLE hProcess;
    PROCESS_MEMORY_COUNTERS pmc;
    fs<<"
Process ID: %u
"<<processID;

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

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



        fs<< "	PageFaultCount: 0x%08X
" << pmc.PageFaultCount;
        fs<< "	Your app's PEAK MEMORY CONSUMPTION: 0x%08X
"<<pmc.PeakWorkingSetSize;
        fs<< "	Your app's CURRENT MEMORY CONSUMPTION: 0x%08X
"<< pmc.WorkingSetSize;
        fs<< "	QuotaPeakPagedPoolUsage: 0x%08X
"<< 
                  pmc.QuotaPeakPagedPoolUsage;
        fs<< "	QuotaPagedPoolUsage: 0x%08X
"<< 
                  pmc.QuotaPagedPoolUsage;
        fs<< "	QuotaPeakNonPagedPoolUsage: 0x%08X
"<< 
                  pmc.QuotaPeakNonPagedPoolUsage;
        fs<< "	QuotaNonPagedPoolUsage: 0x%08X
"<< 
                  pmc.QuotaNonPagedPoolUsage;
        fs<< "	PagefileUsage: 0x%08X
"<< pmc.PagefileUsage; 
        fs<< "	PeakPagefileUsage: 0x%08X
"<< 
                  pmc.PeakPagefileUsage;                  
    }
    fs.close();
    CloseHandle( hProcess);
}

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

    return 0;
}

but in c#?...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
151 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

Poll C# app's memory usage at runtime?

Memory usage in C#

TL;DR;

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...