Thread: How to measure memory consumption?

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Vector for example is declared as vector<T, Alloc>

    Is that a yes, or are you saying you can use other tricks (your own allocator) to identify how much memory is used by the vector? If so, what about objects from other libraries, either third party or from within your group, that cannot be modified to keep track of their specific memory usage?

  2. #17
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    [hijack]
    @Shakti: If using MS VC++, there are excellent tools available for tracking memory leaks (including info on the specific line the memory was allocated on). There is a how-to section for detecting memory leaks in the link below, from codeguru.
    http://www.codeguru.com/forum/showthread.php?t=312742
    [/hijack]
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #18
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    I am aware that VS has good tools for that, but by creating your own system you have the options to create your own garbage-handler (if you want to, and have the knowledge to), dump all the info to a file (I dont know if VS's tools can, they probably can but i find it easier to just write my own handler and use that rather than go out and search through 10 pages of msdn to find out how).

    You can also incorporate alot of other debug stuff needed/wanted by your application.

    Im not saying VS's version is bad, im just saying that there are benefits to have by writing your own means to keep track of heap-declared variables.

    And the last thing the user benefits from; the code is not tied to using MS VS
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  4. #19
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    VS debugging tools in their current version are not that great. I would overload new and delete relative to the desired object(s).

    Salem nailed it and it's the only sure fire way of guaranteeing correct values as well as tracking memory using your own methods. Since every object created on the heap must be allocated using new and must be de-allocated using delete, you have a 100% foolproof way to manage dynamic memory.

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Again, does that include classes from other libraries that you didn't write and that you cannot change?

  6. #21
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    @Shakti: Fair enough.

    >>VS debugging tools in their current version are not that great.
    Only ones I've tried so far, so I can't judge that.

    >>Again, does that include classes from other libraries that you didn't write and that you cannot change?
    Probably not. And what about code using malloc()?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #22
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Again, does that include classes from other libraries that you didn't write and that you cannot change?
    No, it does not, unless you overload new and delete globally instead of relative to a class.

    If you override them globally then your new and delete will be called and as long as the object is created dynamically it does not matter who coded it, you can still keep track of it's memory consumption.

    I'm not sure what happens if that object calls new and delete itself. It would make sense that it would call your new and delete which would allow you to do memory management on the object as well. Perhaps Prelude or Salem could interject here.
    Last edited by VirtualAce; 06-26-2006 at 11:12 PM.

  8. #23
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by Bubba
    VS debugging tools in their current version are not that great. I would overload new and delete relative to the desired object(s).

    Salem nailed it and it's the only sure fire way of guaranteeing correct values as well as tracking memory using your own methods. Since every object created on the heap must be allocated using new and must be de-allocated using delete, you have a 100% foolproof way to manage dynamic memory.
    So are you suggestion that the best way is to overload new and delete operator and perhaps to use one global variable to count number of new and delete calls to sure that at the end there's no memory leak?
    Last edited by Micko; 06-26-2006 at 11:33 PM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  9. #24
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I'm not saying it's the best way, I'm saying it's one way of doing it.

  10. #25
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by Bubba
    I'm not saying it's the best way, I'm saying it's one way of doing it.
    Ok, and what way do you use?
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  11. #26
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I've been doing MFC code and it overloads new and delete relative to CObject which is what everything is derived from. So it's done for me in MFC.

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