Apply some of @jpignata's patches to reduce leaked memory.