-
November 20th, 2010, 08:36 PM
#1
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.
-
November 20th, 2010, 10:21 PM
#2
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
-
November 21st, 2010, 11:30 AM
#3
Re: Any tool to help trace where memory is allocated?
Originally Posted by Red Squirrel
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
-
November 21st, 2010, 01:03 PM
#4
Re: Any tool to help trace where memory is allocated?
Originally Posted by Paul McKenzie
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.
-
November 21st, 2010, 03:10 PM
#5
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.
-
November 23rd, 2010, 07:57 PM
#6
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|