Josh Posted October 16, 2011 Share Posted October 16, 2011 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; } Quote 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 More sharing options...
Josh Posted October 16, 2011 Author Share Posted October 16, 2011 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; } Quote 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 More sharing options...
Canardia Posted October 16, 2011 Share Posted October 16, 2011 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); } Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Mumbles Posted October 16, 2011 Share Posted October 16, 2011 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? Quote LE Version: 2.50 (Eventually) Link to comment Share on other sites More sharing options...
paramecij Posted October 16, 2011 Share Posted October 16, 2011 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_.... Quote Link to comment Share on other sites More sharing options...
Josh Posted October 17, 2011 Author Share Posted October 17, 2011 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) Quote 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 More sharing options...
Josh Posted August 2 Author Share Posted August 2 Hi, I am from the future. This is what I did: //Exact memory usage, but only works in debug mode: _CrtMemState memstate; _CrtMemCheckpoint(&memstate); //_CrtMemDumpStatistics(&memstate); return memstate.lSizes[0] + memstate.lSizes[1] + memstate.lSizes[2] + memstate.lSizes[3] + memstate.lSizes[4]; Quote 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 More sharing options...
Recommended Posts
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.