leftangle
leftangle

Reputation: 128

Memory Debugging

Currently I analyze a C++ application and its memory consumption. Checking the memory consumption of the process before and after a certain function call is possible. However, it seems that, for technical reasons or for better efficiency the OS (Linux) assigns not only the required number of bytes but always a few more which can be consumed later by the application. This makes it hard to analyze the memory behavior of the application.

Is there a workaround? Can one switch Linux to a mode where it assigns just the required number of bytes/pages?

Upvotes: 0

Views: 516

Answers (2)

Martin Rosenau
Martin Rosenau

Reputation: 18493

I wanted to check a process for memory leeks some years ago.

What I did was the following: I wrote a very small debugger (it is easier than it sounds) that simply set breakpoints to malloc(), free(), mmap(), ... and similar functions (I did that under Windows but under Linux it is simpler - I did it in Linux for another purpose!).

Whenever a breakpoint was reached I logged the function arguments and continued program execution...

By processing the logfile (semi-automated) I could find memory leaks.

Disadvantage: It is not possible to debug the program using another debugger in parallel.

Upvotes: 0

Louis Hugues
Louis Hugues

Reputation: 596

if you use malloc/new, the allocator will always alloc a little more bytes than you requested , as it needs some room to do its housekeeping, also it may need to align the bytes on pages boundaries. The amount of supplementary bytes allocated is implementation dependent. you can consider to use tools such as gperftools (google) to monitor the memory used.

Upvotes: 1

Related Questions