Thread: How to measure memory consumption?

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    2

    Question How to measure memory consumption?

    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

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Create the object and print the result of sizeof.
    Code:
    struct harbl {
       int lol;
    };
    
    cout << sizeof(struct harbl) << endl;
    That's all it takes to do this as every harbl will be this size.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    2
    I don't think sizeof will not work if I have pointers to dinamically allocated members.

    Lets look at this example:

    Code:
    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?
      I think sizeof(C) will return just size of two pointers.
    
    }

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well to be completely honest, that doesn't make a difference because the size of C is two pointers. Pointers point to memory that is elsewhere, so if you want the size of one of C's members you have to use sizeof on that member.
    Code:
    sizeof myC->myA / sizeof myC->myA[0];

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    sizeof() will still work with pointers. Despite holding the address of another object, a pointer is still an object. Assuming your pointers take 4 bytes of memory, myC takes 8 bytes.
    Last edited by Mario F.; 06-23-2006 at 03:15 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Or you could create your own allocatinghandler. The handler would keep track of how many bytes allocated and stuff like that. You could also have it keep track of memoryleaks and stuff like that. Of course im not sure how that would work with STL (if its even possible to make it work, i doubt it) but its definately good to have your own ruitines for that just for the sole purpose of keeping track of memoryleaks.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> sizeof() will still work with pointers.
    The point is that the OP wants the total amount of memory used by the object, including both the pointers and what they point to, and sizeof won't help with that unless you add a ton of code in a ton of places including other libraries.

    I'm not sure of the right solution myself, but your compiler or a third party program might have tools to help you do this.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    One possible, albeit ugly way, is to define a vector (because it promises objects will be in sequence) of 2 objects and read the address of each object. Then just do the math.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Again, that won't help, since these objects apparently include pointers to other heap allocated memory, and only the pointers will counted in your solution. The OP wants to include the size of all the memory used by the object.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > http://www.devx.com/tips/Tip/5608
    Simply write new/delete for each class and keep a count of how many live instances of each of them you have.
    The rest is simple maths from then on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    EDIT: Salem nailed it above, didn't read properly at first. Sorry
    Last edited by jafet; 06-24-2006 at 10:06 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Simply write new/delete for each class and keep a count of how many live instances of each of them you have.

    Will that work if your class uses objects from other libraries, like vector or string?

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Will that work if your class uses objects from other libraries, like vector or string?
    Vector for example is declared as
    vector<T, Alloc>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    void C::operator delete (void *p){	
      C* pc = static_cast<C*>(p); 
      free(p);	
    }
    Why the cast? Pointless, typo or contains some implicit operation?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No implicit operation there. It's pointless - may be a leftover from some previous code.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM