CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2007
    Posts
    609

    Any tool to help trace where memory is allocated?

    I'm debugging a weird issue in a program of mine, valgrind returns 0 errors so it's not a memory leak, per say, but definitely somewhere in my program I'm allocating huge chunks and they arn't being returned.

    Is there a way to map out a log, line per line of where memory is allocated? Basically I'd like to see each allocation with a backtrace. This would be a huge log I can imagine though...

    Just wondering if a tool out there can do that. I want to at least be able to pin point which class is using lot of ram.
    http://www.uovalor.com :: Free UO Server

  2. #2
    Join Date
    Mar 2004
    Posts
    235

    Re: Any tool to help trace where memory is allocated?

    For a case like that I use my own memory allocater which allocates extra memory and uses a linked list to keep track of all memory allocated. You can do something like
    Code:
    void* my_alloc(int N, int line_n,  const char* filename ... and other params){
    data = new char[N + header_size]
    // fill the data header with and return data + header_size 
    // I think a map<void*,  header> will be easier next time I need to do this I'm going to use a map<>
    where N is how much you need and header_size is extra info you want to add to the memory location e.g. bytes allocated, class name, __FILE__, __LINE__, prev memory allocation, reserved bytes for next memory allocation to make linked list

    then
    object_pointer = new (data) Object(some_params);

    now you can keep track of your data.
    01101000011001010110110001101100011011110010000001110011011001010111100001111001

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Any tool to help trace where memory is allocated?

    Quote Originally Posted by Red Squirrel View Post
    I'm debugging a weird issue in a program of mine, valgrind returns 0 errors so it's not a memory leak, per say, but definitely somewhere in my program I'm allocating huge chunks and they arn't being returned.
    And how did you determine this? You cannot use OS tools to determine this accurately, as the C heap manager/ C++ free-store manager is the data structure that ultimately controls memory management in a C/C++ program.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Jul 2007
    Posts
    609

    Re: Any tool to help trace where memory is allocated?

    Quote Originally Posted by Paul McKenzie View Post
    And how did you determine this? You cannot use OS tools to determine this accurately, as the C heap manager/ C++ free-store manager is the data structure that ultimately controls memory management in a C/C++ program.

    Regards,

    Paul McKenzie
    I do ps aux and the memory usage grows exponentially.

    Ended up finding the problem manually though, at least I think. My network queue is growing faster then my app can process the data. I need to add some kind of "ack" into my protocol so it can tell the sender to stop for a bit until the buffer catches up.
    http://www.uovalor.com :: Free UO Server

  5. #5
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Any tool to help trace where memory is allocated?

    Valgrind should tell you if you have a memory leak. Are you using a Mac, or have access to one? Run it through Instruments' memory leak detector. If there are any there, that should find it.

    How many places in your program allocate huge amounts of memory? You shouldn't have that many, go look and make sure that you are cleaning them up.

  6. #6
    Join Date
    Jul 2007
    Posts
    609

    Re: Any tool to help trace where memory is allocated?

    That was the tricky part, it was not really a memory leak per say, it was more or less a code error that caused too much memory to be used up. From valgrind's point of view it was not a memory leak as the memory was eventually freed.... after reaching into the TBs' :P

    Fixed the issue though.
    http://www.uovalor.com :: Free UO Server

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured