Heap count calculation made general for all modes.