How to measure memory consumption?  
Author Message
nysubmarine





PostPosted: Visual C++ General, How to measure memory consumption? Top

 

Hi, I have a very complex C++ object (with many class members and some of them are shared between different objects).

What is the best way to measure how much memory this object takes

 

I tried the following:

before and after creating this object I call

GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))

and then use difference in pmc.WorkingSetSize between two calls.

 

However it doesn't work well since WorkingSetSize increased in balks. So sometimes I get a big memory increase and other times it is zero.

 

Is there any system function that allow to measure exactly how much memory was allocated between two calls

 

Thanks in advance for help!

 

PS. I develop on Windows using Visual Studio

 

PS2: Simple example of what I'm trying to do:

 

 

class A {

   char a[100];

}

 

class B {

   char b[100];

}

 

static B* GlobalListOfB = new B[10];

 

 

class C {

    A* myA;

    B* myB;

    C::Init(int i)

    {

       myA = new A[1000];

       myB = &GlobalListOfB [ i ];

    }

}

 

main

{

   C myC;

   

   myC.Init(5);

 

  how to find out what is the size of myC

}

 

 

 

 

 



Visual C++6  
 
 
Jonathan Caves - MSFT





PostPosted: Visual C++ General, How to measure memory consumption? Top

I assuming you want the size in memory of C and everything that C references - because if you just want the size of C then sizeof will work.

For the total size in memory there isn't a single function that will give you the answer you are going to have to use sizeof on each of the sub components and then add them up.



 
 
nysubmarine





PostPosted: Visual C++ General, How to measure memory consumption? Top

Here is my problem and maybe someone will suggest what can I sue for my investigation:

I need to hold in the memory around 100,000 objects. Right now I'm runing out of memory (my PC has 2G RAM) when I create around 15,000 objects. Each object consists of many many complex components. There is no way I can write SizeOf for all the classes ( many of them are containers that point to some shared memory ), it would be a huge project.

What I want is before and after I create some class member measure my process memory and take a delta. I don't need very presise numbers, I need an estimation of which class members take most of the memory and maybe cache or share them.


 
 
Jonathan Caves - MSFT





PostPosted: Visual C++ General, How to measure memory consumption? Top

Inside the Visual C++ compiler we have the facility to track the number of objects of a specific type that we are allocating. It isn't the best system and it depends on the preprocessor. What we do it to define our own operator new

void* operator new(size_t size, const char* type)
{
// ...
}

We then define a macro

#define NEW(T) new (#T) T

Which we use as follows:

class X {
public:
X(int);
// ...
};

void f()
{
X* pX = NEW(X)(42);
}



 
 
Viorel.





PostPosted: Visual C++ General, How to measure memory consumption? Top

I think you can try run-time debug routines. For instance, with _CrtMemCheckpoint function you can obtain the total bytes allocated.

 

There are other many useful functions related to heap area. For more information, see http://msdn2.microsoft.com/en-us/library/1666sb98.aspx.