Jump to content

Get Memory Usage?


Josh
 Share

Recommended Posts

I am using "->" because the compiler complains if I use ".". No idea why. This code causes a buffer overrun:

unsigned long GetMemoryUsage()
{
#ifdef WINDOWS
PPROCESS_MEMORY_COUNTERS process_memory_counters;
Print(String(sizeof(process_memory_counters)));
if (GetProcessMemoryInfo(GetCurrentProcess(),process_memory_counters,process_memory_counters->cb))
{
	return process_memory_counters->WorkingSetSize;
}
else
{
	Print(String(GetLastError()));
	return 0;
}
#endif
return 0;
}

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Ah, here is the working code. TypeDef is evil:

unsigned long GetMemoryUsage()
{
unsigned long result = 0;	
#ifdef WINDOWS
PROCESS_MEMORY_COUNTERS process_memory_counters;
if (GetProcessMemoryInfo(GetCurrentProcess(),&process_memory_counters,sizeof(process_memory_counters)))
{
	result = process_memory_counters.WorkingSetSize;
}
#endif
return result;
}

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

In Framewerk (LE2.27) there is MemFree and MemUsed:

// Draw useful information onscreen
void Renderer::DrawStats( int mode )
{
const int spacing = 16;
const int indent  =  1;
SetBlend(BLEND_ALPHA);
DrawShadowText( indent, indent, "FPS: %3.0f", UPS() );
if( 2 == mode )
{
	DrawShadowText( indent, indent+spacing*1, "%d polys"        , TrisRendered(0) );
	DrawShadowText( indent, indent+spacing*2, "%d shadow polys" , TrisRendered(1) );
	MEMORYSTATUSEX statex;
	statex.dwLength = sizeof( statex );
	GlobalMemoryStatusEx( &statex );
	DrawShadowText( indent, indent+spacing*3, "Mem Free: %0.2f MB", (double)statex.ullAvailPhys/1024.0/1024.0 );
	HANDLE hProcess;
	hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
							PROCESS_VM_READ,
							FALSE, GetCurrentProcessId() );
	if (NULL != hProcess)
	{
		PROCESS_MEMORY_COUNTERS pmc;
		if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
		{
			DrawShadowText( indent, indent+spacing*4, "Mem Used: %0.2f MB", (double)pmc.WorkingSetSize/1024.0/1024.0 );
		}
		CloseHandle( hProcess );
	}
}
SetBlend(0);
}

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

Here was me about to say something like:

 

"wrap malloc and free inside your own (de)allocation functions, which have a variable of how much memory you have allocated. Increased on allocation, decreased on deallocation"

 

A bit boring by comparison isn't it?

LE Version: 2.50 (Eventually)

Link to comment
Share on other sites

I am using "->" because the compiler complains if I use ".". No idea why. This code causes a buffer overrun:

 

Because you are using a pointer - PPROCESS_MEMORY_COUNTERS - the first P stands for Pointer (general ms naming convention), if you look-up the definition of it you'll probably see it's defined as a pointer *PPROCES_....

Link to comment
Share on other sites

Here was me about to say something like:

 

"wrap malloc and free inside your own (de)allocation functions, which have a variable of how much memory you have allocated. Increased on allocation, decreased on deallocation"

 

A bit boring by comparison isn't it?

And that works with precompiled third party libraries? B)

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...