What Memory Leak Detector Do People Use?
funnymalloc asks: "A quick search for 'leak' on FreshMeat yields a whole slew of projects/products which claim to help one find and kill memory bugs of various types when using C/C++. ccmalloc, debauch, dmalloc, LeakTracer, libyama, MCheck, MemProf, memwatch and mpr show up on the first page alone. Ideally a product would detect all of the common problems: array over/under runs, write to unallocated memory, write to 'freed' memory, unfreed memory (a leak) etc. And would work with both new/delete and (m)(c)(re)alloc/free. My question is which of these projects/products do people use and like? (and no, Java is not an answer)"
I'm surprised nobody's mentioned fortify - its an excellent set of stuff that traps all of the problems mentioned above - and can be enabled/disabled quickly and easily (lets not forget the performance hit that these things have...) I can't find a URL offhand (why isnt it in the source files i wonder..) but it should turn up in searches... ...or email me and I'll send it to ya.
Electric Fence tends to work quite well and will check for most types of memory violations. It actually segv's your code when you perform a bad memory access, this lets you run in the debugger of your choice and see exactly where you made the mistake. Your code does run slower and take more memory but most memory debugger behave this way.
Binder
Java does a wonderful job with the simple stuff that you mention. It checks array bounds, does not let you write to unallocated or freed memory, and a slew of other runtime checks. Java however does not eliminate memory leaks.
To be able to be freed (by the garbage collector) an object must have no references. Accidentally keeping references around is a lot easier than you might think and can be a huge source of memory leakage.
Dr. Dobb's has an article about this.
I love Java and I prefer using it for most of my projects, however anybody who tells you that it solves all your memory leak problems is blowing smoke out their ass.
I hate it when people say stuff like that.
"My programs *never* fail cuz I'm l33t!"
My god. Have you ever programmed anything?
Programmers make mistakes.
Experienced programmers make fewer, but more subtle, mistakes. In response to the *asked question*, which you *don't* answer, dmalloc has been my favorite for years. it's free, it works, and it's not difficult to use. checkergcc gccchecker or whatever it goes by today was always crashy and problematic, and electric fence is neat, but I like dmalloc more. Always had trouble getting efence to do what I wanted.
well purify is, afaik, _the_ best tool for this (and more) but unfortunately it's not free and the people of rational are too stupid to support linux:/
Leaky, from the mozilla project, works really well. It works with any C or C++ project.
Oh, I understand now, should have been an extra comma in there, right?
Bleh!
When you insert a malloc() in your code, be sure to make it clear to yourself "who" or "what module" is responsible for that memory, and be sure to hold it responsible for freeing it again. If you are object oriented, this is even easier.
Programmers that write programs that allocate memory, and then get back to them to insert free() statements when the programs are to be used in practice, should be fired ASAP.
Don't program by coincidence, Think!
-larsch
Hans Boehm's garbage collecting malloc() can be built in a leak-detection mode. It works really well once you figure it out.
http://reality.sgi.com/boehm_mti/gc.html
ElectricFence is good at finding over/underflow problems.
rage, rage against the dying of the light
You didn't mention a platform. I'm assuming some sort of Unix, such as Solaris or Windows.
Rational Software (I think) makes a product called Purify. If you ever work in a large shop, you'll use purify to help write your software.
Click here to go to their website
It not only catches memory leaks, but also uninitialized pointers, using uninitialized RAM, stack overflows, etc. It is, in my opinion, the best tool on the market. Yes, it does cost some $$$ (about $2500 per license). If you can't afford it for whatever reason (your a student, business doesn't have the money, etc), or you need a Linux solution (not purify - yet), you'll have to go open source. I would be thrilled if Rational released their tools on Linux, too, although I have to wonder how many Linux developers could afford to buy them (probably the reason they haven't released for Linux yet).
If you're working on your own, then you may want to use routines zalloc, zfree, etc. that call the usual malloc, free, etc. , except that it asks for a larger chunk of memory and have at the edges magic numbers that you can check don't change. You can also get these routines to keep track of how much has been asked for and freed.
It may mean your code needs changing before being released into the world, but at least it is platform / compiler independent. And if it's a toy / prototype / uni assignment then efficiency isn't an issue and you can leave it in.
People may ask how much M$ is paying me to say this. Let me tell you: nothing.
I get options instead.
I use a memory-tracker library I found at www.flipcode.com in the "ask midnight section". It works pretty good...
...You are over-qualified and under-paid. If we give you a raise, we will break the cosmic balance of the universe.
Blue screen of death. Works almost everytime!
I just had to put those $0.02 in...
Laugh - it's kind of funny...
I donate all spillover Karma to the charity of my choice... Ada was still a babe despite what people may say...
I tried LeakTracer--works great, but ONLY checks new/delete and ONLY on i386. mpr checks new/delete AND *alloc/free, but doesn't seem to like my multi-threaded app. Neither of these check access violations. I found another one that did EVERYTHING (including telling you where you should put the free/delete) but it required re-writing the app to use their macros.
Powerful vs simple. Pick one.
--
MailOne
Non-meta-modded "Overrated" mods are killing Slashdot
(Hey Ryan! Here's your proof!)
...if you think you need a leak detector, what you probably need is a language with real garbage collection. There are lots to choose from nowadays, most of which can either be compiled natively or translated to some dialect of C and complied, so you can use good ol' gcc if that's your heart's desire.
C is great for writing kernels and drivers. Java is okay for JSPs and the like. Other choices exist that are far, far better for one-person or large-scale projects. Eiffel, Dylan, Lisp, and ML dialects can work well for big projects and provide very good runtime performance. Python, Ruby, and Smalltalk are great for individual use and are usually fast enough for a given purpose.
I used ElectricFence, mainly because I happened to already have it installed. It helped. There are a bunch of others, some of them look interesting:
MallocDebug
Thu Dec 21 13:26:01 CST 2000 - overview of malloc debugging
tools. looks good.
mpatrol
Thu Dec 21 13:37:30 CST 2000 - didn't try it out,
but the documentation actually lists
"related software", which indicates to me they did their
reseach.
glibc builtin
Thu Dec 21 13:43:54 CST 2000 - evidently glibc has debugging
stuff built-in.
No memory checking tool for C and C++ do that.
Yes, Insure does. While costing quite a lot, it does detect all(?) memory leaks, and all invalid memory accesses. Slows down the program to hell, but produces all the valuable output you may need.
I wrote mpatrol, which attempts to combine as many features of the other malloc debuggers/leak detectors that were mentioned on as many platforms as possible. It also adds a few features of its own. I also collected a list of related software and wrote an overview of their features at: http://www.cbmamiga.demon.co.uk/mpatrol/ If anyone's got any other software that they can recommend for solving such problems then let me know. Incidentally, I agree with the poster who wrote about GNU Checker. Unfortunately, the run-time library for it only works on a few platforms. I'm aiming to add Checker run-time support in mpatrol (initially for heap memory) to extend this to all the platforms that mpatrol runs on. Graeme.
I first run the program to create a baseline on memory utilization. I run it again after a period of time (1 day, for example) to see what has increased. A sysadmin would want to run this on a box that is losing virtual memory over time, and without a good explanation.
I used it to find some problems with some java code, and with the Sybase Replication Server. Their heaps were growing and growing over time.
Of course, this won't catch *everything*. Like short running processes or ever increasing number of processes. Or kernel memory bloat. But it's helped me figure out a few weird problems here and there.
Probably of marginal use to programmer types. This is more for the SA trying to find the bad guy. Cheers.
Pretty much the only place we have to care about resource and reference cleanup is with JDBC. Those instances when we have had problems (always with JDBC ResultSets not being closed), a $500 tool called OptimizeIt has been able to show us what line of code caused the problem with less than five minutes of total time devoted to the problem.
So, while Java is not perfect, in my experience it cuts down by 99% the total amount of time and effort you have to devote to memory related issues. With a dozen Java newbie developers adding 60,000 lines of code to an application over a year period, as the lead programmer who deals with such problems, I've spent two hours on memory management issues. No memory checking tool for C and C++ do that.
The best memory state analyzer available for Linux is GNU Checker. Far, far more than a malloc library, GNU Checker is GCC based. Checker automatically instruments every memory access in your code when you compile your code with it. It detects bad calls to malloc/free, memory leaks, uninitialized data structures, and all sorts of other memory problems. I've used almost all the memory debugging tools and libraries on Linux at one time or another. Most of them are pretty good and each has its place. However, the Swiss Army Knife of memory debugging tools for Linux has to be GNU Checker. It is the most sophisticated of the lot, and it's the nearest to Purify in power. Unfortunately it is also one of the best kept secrets when it comes to Linux development tools.