There are some open-source heap managers out there. It might be interesting to replace malloc with one of those and see if they don't do a better job of keeping the heap defragmented. Quite honestly, its a problem that's been done before. I sure hope they don't go reinventing the wheel.