• Advertisement
Sign in to follow this  

Getting the memory usage

This topic is 2776 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
Thanks you guys for ur help..:D

btw Xtal, what is the unit of measurement? is it in bytes or kb or something?

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
Already linked the Psapi.lib but i still got the same error. Any more suggestions? :)

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
im using windows vista. I linked the Psapi.lib in Project Propertes>Linker>General>Add Addition Library Directories

Also tried Clean and Build but its still the same.

Share this post


Link to post
Share on other sites
Do you want to know the runtime memory use or the static size of the function as inside the executable?

Remember that operating systems talk about memory in terms of pages usually, so IIRC you are going to have a granularity of ~4KB or so. This also seems like it might be premature micro-optimisation.

Share this post


Link to post
Share on other sites
Quote:
Original post by podferio
im using windows vista. I linked the Psapi.lib in Project Propertes>Linker>General>Add Addition Library Directories

Also tried Clean and Build but its still the same.


A .lib file isn't a directory. You need to add it to the linker input settings in the list of "Additional Dependencies".

Share this post


Link to post
Share on other sites
Quote:
Original post by _moagstar_
You need to link with Psapi.lib

That's why i included the compiler options "cl <file> /link PsApi.lib".[smile]
Also, you can check MSDN for what header to #include and what lib to link with.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Remember that operating systems talk about memory in terms of pages usually, so IIRC you are going to have a granularity of ~4KB or so.
This, and in addition, you have no really reliable value in there.

You can get the number of reserved pages, which is likely much higher than what you actually use, since the runtime will reserve memory in big chunks for less overhead.

Or you can get the size of the working set, which is more close to the memory actually used (within the 4k granularity), but not necessarily so. It is possible that some pages were purged from the working set to make room for other memory that you used. You can only be more or less sure that this isn't the case if you set your minimum working set size to some huge value first, so all your pages will stay in memory (and even then, you are strictly not guaranteed that it works as desired, because the OS has no obligation to actually do what you ask for).

The only reliable way of knowing how much memory a given piece of code is using is to override allocators (... and to add the maximum stack size).

Share this post


Link to post
Share on other sites
Thank you so much for all your help guys. Finally build my program without errors. ;)

-james

Share this post


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

  • Advertisement