Game Development Community

dev|Pro Game Development Curriculum

Memory Usage reported by OS

by Timmy01 · 01/30/2015 (5:17 am) · 13 comments

This will add a new Torque Script metrics string called mem and it will display/get the memory usage (in KB) as reported by your OS.

Script Usage:

metrics(mem);
%memMB = getMemoryUsageOS() / 1024;

Source Modifications

File: Engine/source/platform/platform.h
After Line 174 add:
// return memory usage in KB for application as reported by OS
U32 getMemoryUsage();

File: Engine/source/platformWin32/winMemory.cpp
After Line 23 add:
#include "console/engineAPI.h"
#include "console/console.h"
#include <psapi.h>

After line 77 add:
//--------------------------------------
// return memory usage in KB for application as reported by OS
U32 Platform::getMemoryUsage()
{
   PROCESS_MEMORY_COUNTERS pmc;
   GetProcessMemoryInfo(GetCurrentProcess(),pmc,sizeof(pmc));
   U32 memUsage = (U32)pmc.WorkingSetSize;

   return memUsage / 1024;
}

DefineConsoleFunction( getMemoryUsageOS, int, ( ),,"@brief Returns memory usage in KB as reported by the OS")
{
   U32 mem = Platform::getMemoryUsage();
   return mem;
}

File: your_game/game/core/scripts/client/metrics.cs
function memMetricsCallback()
{
   return "  | MEM |" @ " OS Memory Used: " @ ( getMemoryUsageOS() /1024 ) @ " MB";
}

This is for Windows OS but it would honestly be very trivial for Linux/OS X support

About the author

Recent Blogs

• Normal Map Formats

#1
01/30/2015 (5:21 am)
haha oops wrong place, how do i move this to be a resource?
#2
01/30/2015 (6:14 am)
Maybe a site bug, other people had this issue before.
#3
01/30/2015 (7:32 am)
Got devoured by the website edit bug rather badly there ...
GetProcessMemoryInfo(GetCurrentProcess(), &amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;pmc, sizeof(pmc));
#4
01/30/2015 (8:52 am)
Beside the site and edit bugs this is really a useful resource. Thanks Timmy, I was looking for something like this for a long time.
#5
01/30/2015 (11:03 am)
Thanks Timmy. I will give it a try tonight. Should be in resources section.
#6
01/30/2015 (12:35 pm)
suggest using Cocoa for OSX, I will probably do it over the next couple of days. someone on IRC kick me if I forget.
#7
01/30/2015 (3:38 pm)
Fixed up the formatting, sorry guys. I am so sure i clicked on resource when posting this :/
#8
01/30/2015 (6:45 pm)
@Jeff:
you can use "task_info" function in OSX.
#9
01/30/2015 (6:54 pm)
Untested(don't have a mac):

#include <mach/mach.h>
//--------------------------------------  
// return memory usage in KB for application as reported by OS  
U32 Platform::getMemoryUsage()  
{  
   struct task_basic_info info;
   mach_msg_type_number_t size = sizeof(info);
   kern_return_t err = task_info(mach_task_self(),
                               TASK_BASIC_INFO,
                               (task_info_t)&info,
                               &size);

   if( err == KERN_SUCCESS )
      return info.resident_size / 1024;
   else
      return 0;
  
}
#10
02/01/2015 (5:49 am)
instead of a resource, shouldn't this be added as one of the metrics options?
#11
02/01/2015 (10:50 am)
might be a good idea to at least PR the windows one timmy :P
#12
02/02/2015 (2:36 am)
I wasn't really sure if this was worthy of a PR, that's why I released it as a resource (well tried too but it ended up here in the blog section :/ ).
#13
02/03/2015 (12:02 am)
Nice resource... uhhh I mean blog ;-)