How does C + + get the memory occupied by the current process? "Go"

Source: Internet
Author: User

Using the SDK's Psapi (Process Status Helper)
In the
BOOL
Getprocessmemoryinfo (
HANDLE Process,

Pprocess_memory_counters Ppsmemcounters,

DWORD CB
);

typedef
struct _process_memory_counters {
DWORD CB;
DWORD Pagefaultcount;

size_t PeakWorkingSetSize;
Peak Memory usage
size_t WorkingSetSize;
Memory usage
size_t Quotapeakpagedpoolusage;

size_t QuotaPagedPoolUsage;

size_t QuotaPeakNonPagedPoolUsage;

size_t Quotanonpagedpoolusage;

size_t PageFileUsage;
Virtual Memory usage
size_t PeakPageFileUsage;
Peak Virtual Memory usage
} process_memory_counters,
*pprocess_memory_counters;

#include <iostream>
#include <windows.h>
#include <psapi.h>
#pragma comment (lib, "PSAPI.lib")
using namespace Std;
void Showmemoryinfo (void)
{
HANDLE handle=getcurrentprocess ();
Process_memory_counters PMC;
Getprocessmemoryinfo (handle,&pmc,sizeof (PMC));
cout<< "Memory usage:" &LT;&LT;PMC. workingsetsize/1000 << "k/" &LT;&LT;PMC. peakworkingsetsize/1000<< "K +" &LT;&LT;PMC. pagefileusage/1000 << "k/" &LT;&LT;PMC. peakpagefileusage/1000 << "K" <<endl;
}
int main (int argc,char* argv)
{
Showmemoryinfo ();
cout<< "Reclaim all recyclable Memory" <<endl;
Emptyworkingset (GetCurrentProcess ());
Showmemoryinfo ();
cout<< "Start allocating memory dynamically" <<endl;
char* Buf[5];
for (int i=0;i<sizeof (BUF)/sizeof (char*); i++)
{
Buf[i]=new char[102400];
Showmemoryinfo ();
}
cout<< "Start freeing Memory" <<endl;
for (int i=0;i<sizeof (BUF)/sizeof (char*); i++)
{
Delete Buf[i];
Buf[i]=null;
Showmemoryinfo ();
}
cout<< "Reclaim all recyclable Memory" <<endl;
Emptyworkingset (GetCurrentProcess ());
Showmemoryinfo ();
return 0;
}

Output:
Memory Usage: 1339k/1339k +
356k/356k
Reclaim All recyclable memory
Memory Usage: 114k/1425k +
356k/356k
Start allocating memory dynamically
Memory usage: 430k/1425k + 466k/466k
Memory Usage: 438k/1425k +
573k/573k
Memory usage: 446k/1425k + 679k/679k
Memory Usage: 454k/1425k +
786k/786k
Memory usage: 462k/1425k + 892k/892k
Start freeing memory
Memory Usage: 462k/1425k +
794k/892k
Memory usage: 454k/1425k + 692k/892k
Memory Usage: 446k/1425k +
589k/892k
Memory usage: 438k/1425k + 487k/892k
Memory Usage: 425k/1425k +
360k/892k
Reclaim All recyclable memory
Memory usage: 110k/1425k + 360k/892k

How does C + + get the memory occupied by the current process? "Go"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.