add memory measurement
fix race condition