Stemming the leak.. A C tool I can’t live without (memwatch).2017-06-10 16:47:08
Working on porting our Perl code to C has been educational and grindingly slow. I can’t tell you how many times I’ve had to re-invent the wheel and develop libraries for Perl functions I take for granted.
Work on the new web based Portal project is coming along very slow and steady. The tool to publish your corporate directory to the web is starting to take form, until…
It was by shear coincidence that I was following the largest oil spill/disaster in known history, that I had run into a bug in the test code. Somewhere I had spawned a memory leak. A memory leak happens when you grab memory from the system to be used to store data and don’t give it back. The operating memory grows until your program stops or the computer runs out of memory. In Perl this is a no-brainer, it has an aptly named “garbage collection” technology that releases the unused spent bits of memory.
C does not have this option. All memory is directly controlled by the developer, down to the single byte. Ever wonder why so many programs crashed for no reason in the ‘80s & '90s Memory Leak.
Enter a charming open source tool called memwatch. It shims itself between the program and the system, every memory call that is made passes through it and is recorded. At the end of the program execution, a report is generated showing which bits of memory were not released to the system. It located no less than 20 memory leaks! Within about 10 minutes I had cleaned up all the bone-headed mistakes and now the software runs clean. Portal now comfortably runs in under 10meg (MEG!) of memory.
I’m sure there are hundreds of such tools out on the market but this was the easiest to setup and run. Since the whole thing is done in ANSI C, it will work in virtually any environment. Thankfully it runs on my MinGW development environment in Windows.