Jump to content
  • Advertisement
Sign in to follow this  

Getting the memory usage

This topic is 2863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone, im having a problem in getting the memory usage of my program. I want to know how much memory was used (in bytes or kb ) in this function:

eg:

int test(int a , int b)
{

// Start
while ( a != b ) {
//..
//..
//..

b++;


// end
}

}

Is it possible to get the memory usage from Start to End as shown in the code.
I heard about GetProcessMemoryInfo() but i dont know how to use it. Help please? Anyone?


Thanks :)

Share this post


Link to post
Share on other sites
Advertisement
The way I've seen most games do it is to keep track of it yourself. That is, you replace malloc/new/etc with your own versions which increment counters as well as performing an allocation.

Share this post


Link to post
Share on other sites
If you didn't want to do custom allocation, you could use GetProcessMemoryInfo as you said.

Example:

#include <iostream>
#include "windows.h"
#include "Psapi.h"
using namespace std;

int getTotalMemory() {
PROCESS_MEMORY_COUNTERS mem;
GetProcessMemoryInfo(GetCurrentProcess(), &mem, sizeof(mem));
return mem.WorkingSetSize;
}

int main() {
int startMemory = getTotalMemory();
char* bytes = new char[12345678]; // Allocate some memory
int endMemory = getTotalMemory();
cout << "Memory used = " << endMemory - startMemory;
}

/* Compiled with: cl <file> /link PsApi.lib */



Not sure if that suits your purpose, but it should get the total working set size of your process

Share this post


Link to post
Share on other sites
Thx Plerion.

btw

i have an error

>>error LNK2019: unresolved external symbol _GetProcessMemoryInfo@12 referenced in function "int __cdecl getTotalMemory(void)" (?getTotalMemory@@YAHXZ)
>>fatal error LNK1120: 1 unresolved externals

im using MSVS2008


Share this post


Link to post
Share on other sites
Quote:
Original post by podferio
Already linked the Psapi.lib but i still got the same error. Any more suggestions? :)


Looks like you need to link kernel.lib on windows 7, are you using windows 7? If not, what method did you use to link psapi.lib? If all else fails perhaps try a rebuild instead of just a regular build.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!