port of https://code.google.com/p/memory-measurer/