improve memory instrumentation code