Click here to Skip to main content
16,017,907 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionhow to find where the memory leaking occurred according to the vc's output? Pin
kcynic24-Jan-08 21:35
kcynic24-Jan-08 21:35 
AnswerRe: how to find where the memory leaking occurred according to the vc's output? [modified] Pin
CPallini24-Jan-08 21:40
mveCPallini24-Jan-08 21:40 
GeneralRe: how to find where the memory leaking occurred according to the vc's output? Pin
kcynic24-Jan-08 22:02
kcynic24-Jan-08 22:02 
GeneralRe: how to find where the memory leaking occurred according to the vc's output? Pin
CPallini24-Jan-08 22:14
mveCPallini24-Jan-08 22:14 
GeneralRe: how to find where the memory leaking occurred according to the vc's output? Pin
kcynic24-Jan-08 22:22
kcynic24-Jan-08 22:22 
AnswerRe: how to find where the memory leaking occurred according to the vc's output? Pin
ShilpiP24-Jan-08 22:14
ShilpiP24-Jan-08 22:14 
GeneralRe: how to find where the memory leaking occurred according to the vc's output? Pin
kcynic24-Jan-08 22:28
kcynic24-Jan-08 22:28 
GeneralBug of performance counter API or perfmon tool Pin
George_George24-Jan-08 20:58
George_George24-Jan-08 20:58 
Hello everyone,


I found using performance counter API and using perfmon counter will result in different numeric result. Here is an example, and in the example code, virtual bytes is always larger than working set, but in perfmon, working set is always larger than working set.

Any ideas?

#include <windows.h>
#include <stdio.h> 
#include <pdh.h>
#include <atltime.h>

#pragma comment(lib, "pdh")

PDH_STATUS ps;
CHAR wsPath[256];
CHAR vbPath[256];
DWORD wsItems = 0;
DWORD wsSize  = 0;
DWORD vbItems = 0;
DWORD vbSize  = 0;

FILE* fLog = fopen("perfcount.log", "w+");

CHAR processModule[MAX_PATH] = {0};

CHAR* GetProcModuleName()
{
	if (GetModuleFileName(GetModuleHandle(NULL), processModule, sizeof(processModule)))
	{
		CHAR* p = strrchr(processModule, '.');
		if (p)
			*p = '\0';
		else
			return NULL;
		p = strrchr(processModule, '\\');
		if (p)
			return (p + 1);
	}

	return NULL;
}

void GetCounters(DWORD sectionIndex)
{
	HQUERY hQuery;
	HCOUNTER hWsCount;
	HCOUNTER hVbCount;

	ps = PdhOpenQuery(NULL, 0, &hQuery);
	ps = PdhAddCounter(hQuery, wsPath, 0, &hWsCount);
	ps = PdhAddCounter(hQuery, vbPath, 0, &hVbCount);

	ps = PdhCollectQueryData(hQuery);

	wsItems = 0;
	wsSize  = 0;
	vbItems = 0;
	vbSize  = 0;
	PDH_RAW_COUNTER_ITEM* pWS = NULL;
	PDH_RAW_COUNTER_ITEM* pVB = NULL;

	ps = PdhGetRawCounterArray(hWsCount, &wsSize, &wsItems, NULL);
	pWS = (PDH_RAW_COUNTER_ITEM*)LocalAlloc(LPTR, wsSize);
	ps = PdhGetRawCounterArray(hVbCount, &vbSize, &vbItems, NULL);
	pVB = (PDH_RAW_COUNTER_ITEM*)LocalAlloc(LPTR, vbSize);
	ps = PdhGetRawCounterArray(hWsCount, &wsSize, &wsItems, pWS);
	ps = PdhGetRawCounterArray(hVbCount, &vbSize, &vbItems, pVB);
	ps = PdhCloseQuery(hQuery);

	CTime t = pWS->RawValue.TimeStamp;
	fprintf(fLog, "[%s] SI: %6d\t WS: %.2f Mb\tVB: %.2f Mb\n", 
		t.Format("%c"),
		sectionIndex,
		double(pWS->RawValue.FirstValue) /1024 /1024, 
		double(pVB->RawValue.FirstValue) /1024 /1024);
	LocalFree(pWS);
	LocalFree(pVB);
	fflush(fLog);
}


int main(int argc, char* argv[]) 
{ 
	LARGE_INTEGER start,end; 
	LARGE_INTEGER freq; 
	QueryPerformanceCounter(&start); 
	QueryPerformanceFrequency(&freq); 

	MEMORYSTATUS memstat; 
	void** map;
	int sectionIndex = 0;
	memstat.dwLength = sizeof(memstat); 
	GlobalMemoryStatus(&memstat); 

	// basic file mapping test (512 MB) 
	long long size = 512*1024*1024; 

	
	DWORD nsize = 0;
	CHAR poName[256];
	CHAR wsName[256];
	CHAR vbName[256];

	nsize = sizeof(poName);
	ps = PdhLookupPerfNameByIndex(NULL, 230, poName, &nsize);  // Process object
	nsize = sizeof(wsName);
	ps = PdhLookupPerfNameByIndex(NULL, 180, wsName, &nsize);  // Working Set counter
	nsize = sizeof(vbName);
	ps = PdhLookupPerfNameByIndex(NULL, 174, vbName, &nsize);  // Virtual Bytes counter

	PDH_COUNTER_PATH_ELEMENTS pcpe = {0};
	pcpe.szObjectName = poName;
	pcpe.szInstanceName = GetProcModuleName();
	
	pcpe.szCounterName = wsName;
	nsize = sizeof(wsPath);
	ps = PdhMakeCounterPath(&pcpe, wsPath, &nsize, 0);

	pcpe.szCounterName = vbName;
	nsize = sizeof(vbPath);
	ps = PdhMakeCounterPath(&pcpe, vbPath, &nsize, 0);

	HANDLE mapping = 
	CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE|SEC_COMMIT,(DWORD)(size >> 32),DWORD(size),NULL); 
	if (mapping) 
	{ 
		// create and destroy temporary views 
		SYSTEM_INFO sysInfo; 
		GetSystemInfo(&sysInfo); 
		const int allocSize = sysInfo.dwAllocationGranularity; 

		GlobalMemoryStatus(&memstat); 

		void *mem = new char[allocSize]; 
		memset(mem,0x11,allocSize); 

		map = (void**) new char [sizeof(void*) * size / allocSize];

		for (int i=0; i<1; i++) 
		{ 

			sectionIndex = 0;
			for (long long offset=0; offset <= size-allocSize; offset+=allocSize) 
			{ 
				map [sectionIndex] = 
				MapViewOfFile(mapping,FILE_MAP_WRITE,(DWORD)(offset << 32),(DWORD)offset,allocSize); 
				if (map [sectionIndex]) 
				{ 
					memcpy(map [sectionIndex],mem,allocSize); 

					GetCounters(sectionIndex);

					// UnmapViewOfFile(map);
				}

				sectionIndex++;
			} // for (long long offset=0; offset <= size-allocSize; offset+=allocSize) 
			
			// close mapped files to avoid leak
			for (sectionIndex = 0; sectionIndex < size/allocSize; sectionIndex++)
			{
				if (map [sectionIndex])
				{ 
					UnmapViewOfFile(map [sectionIndex]);
				}
			}

			GlobalMemoryStatus(&memstat); 

			sectionIndex = 0;
			for (long long offset=0; offset<=size-allocSize; offset+=allocSize) 
			{ 
				map [sectionIndex] = 
				MapViewOfFile(mapping,FILE_MAP_READ,(DWORD)(offset << 32),(DWORD)offset,allocSize); 
				if (map [sectionIndex]) 
				{ 
					for (int t=0; t < allocSize; t++) 
					{ 
						if (((char *)(map [sectionIndex]))[t]!=0x11) 
						{ 
							OutputDebugString("Memory read failed\n"); 
						} 
					} 

					GetCounters(sectionIndex);
				} 

				UnmapViewOfFile(map [sectionIndex]);
				sectionIndex++;
			} 

			// close mapped files to avoid leak
			/*
			for (sectionIndex = 0; sectionIndex < size/allocSize; sectionIndex++)
			{
				if (map [sectionIndex])
				{ 
					UnmapViewOfFile(map [sectionIndex]);
				}
			}
			*/

			GlobalMemoryStatus(&memstat); 
		} // for (int i=0; i<10; i++)

		QueryPerformanceCounter(&end); 

		GlobalMemoryStatus(&memstat); 

		printf("Time %.3f\n", 
		double(end.QuadPart-start.QuadPart)/double(freq.QuadPart)); 
		CloseHandle(mapping); 
		delete[] mem; 
		GlobalMemoryStatus(&memstat); 
	} //if (mapping)

	fclose(fLog);

	return 0;
} 
</atltime.h></pdh.h></stdio.h></windows.h>



thanks in advance,
George
GeneralRe: Bug of performance counter API or perfmon tool Pin
Don Box24-Jan-08 21:42
Don Box24-Jan-08 21:42 
GeneralRe: Bug of performance counter API or perfmon tool Pin
George_George24-Jan-08 21:54
George_George24-Jan-08 21:54 
GeneralRe: Bug of performance counter API or perfmon tool Pin
David Crow25-Jan-08 2:59
David Crow25-Jan-08 2:59 
GeneralRe: Bug of performance counter API or perfmon tool Pin
George_George25-Jan-08 3:02
George_George25-Jan-08 3:02 
Generalc ++ coding Pin
joni24-Jan-08 20:56
joni24-Jan-08 20:56 
GeneralRe: c ++ coding Pin
Cedric Moonen24-Jan-08 21:03
Cedric Moonen24-Jan-08 21:03 
GeneralRe: c ++ coding Pin
CPallini24-Jan-08 21:12
mveCPallini24-Jan-08 21:12 
GeneralRe: c ++ coding Pin
Rajesh R Subramanian24-Jan-08 21:37
professionalRajesh R Subramanian24-Jan-08 21:37 
QuestionRe: c ++ coding Pin
David Crow25-Jan-08 3:01
David Crow25-Jan-08 3:01 
GeneralParallel Pragramming Pin
vikramlinux24-Jan-08 20:15
vikramlinux24-Jan-08 20:15 
QuestionRe: Parallel Pragramming Pin
CPallini24-Jan-08 22:21
mveCPallini24-Jan-08 22:21 
GeneralRe: Parallel Pragramming Pin
vikramlinux24-Jan-08 22:24
vikramlinux24-Jan-08 22:24 
QuestionRe: Parallel Pragramming Pin
CPallini24-Jan-08 22:29
mveCPallini24-Jan-08 22:29 
GeneralRe: Parallel Pragramming Pin
vikramlinux24-Jan-08 22:35
vikramlinux24-Jan-08 22:35 
GeneralRe: Parallel Pragramming Pin
CPallini24-Jan-08 22:42
mveCPallini24-Jan-08 22:42 
GeneralRe: Parallel Pragramming Pin
David Crow25-Jan-08 3:05
David Crow25-Jan-08 3:05 
GeneralUse inline member function to refer different levels of classes. [modified] Pin
CodingLover24-Jan-08 19:45
CodingLover24-Jan-08 19:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.